Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

time-based file rotation

  • Loading branch information...
commit 30f46a3f7413458124f22e11b7d86bbb3d922841 1 parent 0a51dbf
fox authored
View
6 include/log4erl.hrl
@@ -15,8 +15,8 @@
%-define(DEBUG, true).
-ifdef(DEBUG).
--define(LOG(X), io:format("~p: " ++ X,[?MODULE])).
--define(LOG2(X,D), io:format("~p: " ++ X,[?MODULE | D])).
+-define(LOG(X), io:format("[~p:~p] " ++ X ++ "~n",[?MODULE, ?LINE])).
+-define(LOG2(X,D), io:format("[~p:~p] " ++ X ++ "~n",[?MODULE, ?LINE | D])).
-else.
-define(LOG(_X), ok).
-define(LOG2(_X,_D), ok).
@@ -25,7 +25,7 @@
%% type = time | size
%% max = seconds (for time) | or kiloBytes (for size)
--record(log_type,{type, max, timer}).
+-record(log_type,{type, max}).
%% file_name = the name of the file without counter
%% fd = the descriptior for the file
View
8 priv/l4e.conf
@@ -5,11 +5,11 @@ logger{
dir = ".",
level = info,
file = my_app,
- type = size,
- max = 1000,
+ type = time,
+ max = 60,
suffix = log,
- rotation = 5,
- format = '[%L]: %I, %l;%n'
+ rotation = 10,
+ format = '[%L]: %I, %l%n'
}
console_appender app1{
View
5 priv/log4erl.conf
@@ -7,8 +7,9 @@ logger{
dir = ".",
level = info,
file = my_app,
- type = size,
- max = 100000,
+ type = time,
+ %max = 100000,
+ max = 5,
suffix = log,
rotation = 5,
format = '[%L] %I %l%n'
View
26 src/file_appender.erl
@@ -42,6 +42,14 @@ init({Dir, Fname, {Type, Max}, Rot, Suf, Level, Pattern} = _Conf) ->
File = Dir ++ "/" ++ Fname ++ "." ++ Suf,
{ok, Fd} = file:open(File, ?FILE_OPTIONS),
Ltype = #log_type{type = Type, max = Max},
+
+ case Type of
+ time ->
+ start_file_timer(Max);
+ _ ->
+ ok
+ end,
+
% Check Rot >= 0
Rot1 = case Rot < 0 of
true ->
@@ -49,6 +57,7 @@ init({Dir, Fname, {Type, Max}, Rot, Suf, Level, Pattern} = _Conf) ->
false ->
Rot
end,
+
?LOG2("To parse format with customer format ~p~n",[Pattern]),
{ok, Format} = log_formatter:parse(Pattern),
?LOG2("Adding format of ~p~n",[Format]),
@@ -104,6 +113,11 @@ handle_call(_Request, State) ->
Reply = ok,
{ok, Reply, State}.
+handle_info(rotate_timer, #file_appender{log_type=#log_type{max=Max}} = State) ->
+ ?LOG2("rotate_timer msg is received", []),
+ {ok, State2} = rotate(State),
+ start_file_timer(Max),
+ {ok, State2};
handle_info(_Info, State) ->
?LOG2("~w received unknown message: ~p~n", [?MODULE, _Info]),
{ok, State}.
@@ -164,8 +178,18 @@ check_rotation(State) ->
true ->
State
end;
- %% time-based rotation is not implemented yet
+ %% time-based rotation is implemented in a seperate process
_ ->
State
end.
+start_file_timer(Max) ->
+ Self = self(),
+ ?LOG("starting file timer"),
+ spawn_link(fun() ->
+ %% time is in seconds
+ timer:sleep(Max*1000),
+ Self ! rotate_timer
+ end),
+ ok.
+
Please sign in to comment.
Something went wrong with that request. Please try again.