Skip to content

Commit

Permalink
modified: Makefile
Browse files Browse the repository at this point in the history
	modified:   ebin/log4erl.app
	modified:   include/log4erl.hrl
	modified:   priv/log4erl.conf
	modified:   src/Makefile
	modified:   src/file_appender.erl
	modified:   src/log4erl.erl
	modified:   src/log4erl_conf.erl
	modified:   src/log4erl_lex.erl
	modified:   src/log4erl_lex.xrl
	modified:   src/log4erl_parser.erl
	modified:   src/log4erl_parser.yrl
	modified:   src/log4erl_sup.erl
	new file:   src/log_filter.erl
	new file:   src/log_filter_codegen.erl
	modified:   src/log_manager.erl
	modified:   src/syslog_appender.erl
  • Loading branch information
ahmednawras committed Jan 31, 2010
1 parent fe1ced6 commit 8051eb0
Show file tree
Hide file tree
Showing 17 changed files with 406 additions and 376 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -12,6 +12,6 @@ clean:
rm -f src/*~
rm -f ebin/*~
rm -f include/*~
# cd dir1; make clean
cd ${SRC}; make clean
#install:
# cp -f ebin/* ../../www/ebin
4 changes: 2 additions & 2 deletions ebin/log4erl.app
Expand Up @@ -2,10 +2,10 @@
%% application.
{application, log4erl,
[{description, "Logger for erlang in the spirit of Log4J"},
{vsn, "0.8.6"},
{vsn, "0.9.0"},
{modules, [log4erl]},
{registered,[log4erl]},
{applications, [kernel,stdlib]},
{mod, {log4erl,[default_logger]}},
{mod, {log4erl,[]}},
{start_phases, []}
]}.
2 changes: 1 addition & 1 deletion include/log4erl.hrl
Expand Up @@ -12,7 +12,7 @@
-define(FILE_OPTIONS,[write, raw, binary, append]).
-define(FILE_OPTIONS_ROTATE,[write, raw, binary]).

%%-define(DEBUG, true).
%-define(DEBUG, true).

-ifdef(DEBUG).
-define(LOG(X), io:format("~p: " ++ X,[?MODULE])).
Expand Down
2 changes: 2 additions & 0 deletions priv/log4erl.conf
@@ -1,3 +1,5 @@
cutoff_level = warn

%% Default logger
%% it includes a file appender and a console appender
logger{
Expand Down
27 changes: 13 additions & 14 deletions src/Makefile
Expand Up @@ -2,7 +2,7 @@ EBIN_DIR=../ebin
INCLUDE_DIR=../include
ERLS=$(wildcard *.erl)
ERLC=erlc -o $(EBIN_DIR) -I ${INCLUDE_DIR}
BEAMS=$(ERLS:.erl=.beam)
BEAMS=$(ERLS:%.erl=$(EBIN_DIR)/%.beam)

# leave these lines alone
.SUFFIXES: .erl .beam .yrl
Expand All @@ -13,23 +13,22 @@ BEAMS=$(ERLS:.erl=.beam)
#.yrl.erl:
# $(ERLC)-W $<

all: compile

# Here's a list of the erlang modules you want compiling
# If the modules don't fit onto one line add a \ character
# to the end of the line and continue on the next line
# Edit the lines below
#MODS = log4erl_sup log4erl file_appender console_appender log_manager logger_guard log4erl_utils dummy_appender log_formatter smtp_fsm.erl email_msg.erl
#MODS = $(ERLS)
BEAMS = $(ERLS:.erl=.beam)
MODS = $(BEAMS:.beam=)

all: compile

%.beam: %.erl
$(EBIN_DIR)/%.beam: %.erl
@echo ">>" compiling: $<
@$(ERLC) $<

compile: ${MODS:%=%.beam}
generate_parser:
@echo ">>" generating log4erl_parser.erl from log4erl_parser.yrl $<
erlc log4erl_parser.yrl

# export LEEX_PATH env var for this to work
generate_lex:
@echo ">>" generating log4erl_lex.erl from log4erl_lex.xrl $<
erl -noinput -noshell -pa $$LEEX_PATH/ebin -s leex file log4erl_lex.xrl -s init stop

compile: ${BEAMS}

clean:
rm -rf ../ebin/*.beam erl_crash.dump
53 changes: 48 additions & 5 deletions src/file_appender.erl
Expand Up @@ -79,9 +79,9 @@ handle_event({change_level, Level}, State) ->
{ok, State2};
handle_event({log,LLog}, State) ->
?LOG2("handl_event:log = ~p~n",[LLog]),
do_log(LLog, State),
Res = check_rotation(State),
{ok, Res}.
ResState = check_rotation(State),
do_log(LLog, ResState),
{ok, ResState}.


handle_call({change_format, Format}, State) ->
Expand Down Expand Up @@ -145,6 +145,38 @@ rotate(#file_appender{fd = Fd, dir=Dir, file_name=Fn, counter=Cntr, rotation=Ro
State2 = #file_appender{dir = Dir, file_name = Fn, fd = Fd2, counter=C, log_type = Ltype, rotation = Rot, suffix=Suf, level=Level, format=Format},
{ok, State2}.

rotate_daily(#file_appender{fd = Fd, dir=Dir, file_name=Fn, counter=Cntr, rotation=Rot, suffix=Suf, log_type=Ltype, level=Level, format=Format} = _S, {Year, Month, Day}) ->
file:close(Fd),
?LOG("Starting daily rotation~n"),
Date = lists:flatten(io_lib:format("~4.10.0B-~2.10.0B-~2.10.0B", [Year, Month, Day])),
Folder = Dir ++ "/" ++ Fn ++ "." ++ lists:flatten(io_lib:format("~4.10.0B-~2.10.0B", [Year, Month])),
Src = Dir ++ "/" ++ Fn ++ "." ++ Suf,
Fname = Folder ++ "/" ++ Fn ++ "." ++ Date ++ "." ++ Suf,
?LOG2("Moving file from ~p to ~p~n", [Src, Fname]),

% each month has it's own log dir
file:make_dir(Folder),
file:rename(Src, Fname),

case erlang:date() of
{Year, Month, _} -> ok;
_ ->
% compress to zip and remove all previous month log files
RemoveOld = fun() ->
{ok, _} = zip:create(Folder ++ ".zip", [Folder]),
{ok, Files} = file:list_dir(Folder),
lists:foreach(fun(F) -> file:delete(Folder ++ "/" ++ F) end, Files),
file:del_dir(Folder)
end,
spawn(RemoveOld)
end,

{ok, Fd2} = file:open(Src, ?FILE_OPTIONS_ROTATE),
State2 = #file_appender{dir = Dir, file_name = Fn, fd = Fd2, counter=Cntr, log_type = Ltype, rotation = Rot, suffix=Suf, level=Level, format=Format},
{ok, State2}.



% Check if the file needs to be rotated
% ignore in case of if log type is set to time instead of size
check_rotation(State) ->
Expand All @@ -161,8 +193,19 @@ check_rotation(State) ->
true ->
State
end;
%% time-based rotation is not implemented yet

daily ->
File = Dir ++ "/" ++ Fname ++ "." ++ Suf,
{ok, Finfo} = file:read_file_info(File),
{CDate, _CTime} = Finfo#file_info.ctime,
case erlang:date() of
CDate ->
State;
_ ->
{ok, State2} = rotate_daily(State, CDate),
State2
end;

_ ->
State
end.

0 comments on commit 8051eb0

Please sign in to comment.