Permalink
Browse files

Various bugfixes for tracing to work

Added test for per module loglevels
  • Loading branch information...
1 parent b43bb74 commit 7f40838b83fe840f0faeeacb9e26b0988bad3d8f @fenek committed Aug 24, 2012
Showing with 90 additions and 40 deletions.
  1. +1 −1 include/lager.hrl
  2. +1 −1 src/lager.erl
  3. +15 −16 src/lager_console_backend.erl
  4. +9 −10 src/lager_file_backend.erl
  5. +64 −12 test/lager_test_backend.erl
View
@@ -55,7 +55,7 @@
lager_util:level_to_num(Level) =< element(1, lager_mochiglobal:get(loglevel, {?LOG_NONE, []}))).
-define(NOTIFY(Level, Pid, Format, Args),
- gen_event:notify(lager_event, {log, lager_util:level_to_num(Level),
+ gen_event:notify(lager_event, {log, '_', lager_util:level_to_num(Level),
lager_util:format_time(), [io_lib:format("[~p] ", [Level]),
io_lib:format("~p ", [Pid]), io_lib:format(Format, Args)]})).
View
@@ -99,7 +99,7 @@ log_dest(Level, Module, Function, Line, Pid, Time, Dest, Format, Args) ->
Msg = [["[", atom_to_list(Level), "] "],
io_lib:format("~p@~p:~p:~p ", [Pid, Module, Function, Line]),
safe_format_chop(Format, Args, 4096)],
- safe_notify({log, {Dest, Module}, lager_util:level_to_num(Level), Timestamp, Msg}).
+ safe_notify({log_dest, Dest, lager_util:level_to_num(Level), Timestamp, Msg}).
%% @doc Manually log a message into lager without using the parse transform.
@@ -24,7 +24,7 @@
-export([init/1, handle_call/2, handle_event/2, handle_info/2, terminate/2,
code_change/3]).
--record(state, {level, verbose, mod_levels}).
+-record(state, {level, verbose, mod_levels = []}).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
@@ -52,7 +52,7 @@ init([Level, Verbose]) ->
%% @private
handle_call(get_loglevel, #state{level=GenLevel, mod_levels = ModLvls} = State) ->
- Level = erlang:tl(lists:sort([GenLevel | [ ModLvl || {_, ModLvl} <- ModLvls ]])),
+ Level = erlang:hd(lists:reverse(lists:sort([GenLevel | [ ModLvl || {_, ModLvl} <- ModLvls ]]))),
{ok, Level, State};
handle_call({get_mod_loglevel, Module}, #state{mod_levels = ModLvls} = State) ->
case lists:keysearch(Module, 1, ModLvls) of
@@ -88,20 +88,19 @@ handle_call(_Request, State) ->
{ok, ok, State}.
%% @private
-handle_event({log, {Dest, Module}, Level, {Date, Time}, [LevelStr, Location, Message]},
- #state{level=GenLevel, verbose=Verbose, mod_levels = ModLvls} = State) ->
- case lists:member(lager_console_backend, Dest)
- andalso lager_util:check_loglevel(GenLevel, ModLvls, Module, Level) of
- true ->
- case Verbose of
- true ->
- io:put_chars(user, [Date, " ", Time, " ", LevelStr, Location, Message, "\r\n"]);
- _ ->
- io:put_chars(user, [Time, " ", LevelStr, Message, "\r\n"])
- end,
- {ok, State};
- false ->
- {ok, State}
+handle_event({log_dest, Dest, Level, {Date, Time}, [LevelStr, Location, Message]},
+ #state{level=L, verbose=Verbose} = State) when Level > L ->
+ case lists:member(lager_console_backend, Dest) of
+ true ->
+ case Verbose of
+ true ->
+ io:put_chars(user, [Date, " ", Time, " ", LevelStr, Location, Message, "\r\n"]);
+ _ ->
+ io:put_chars(user, [Time, " ", LevelStr, Message, "\r\n"])
+ end,
+ {ok, State};
+ false ->
+ {ok, State}
end;
handle_event({log, Module, Level, {Date, Time}, [LevelStr, Location, Message]},
#state{level=GenLevel, verbose=Verbose, mod_levels = ModLvls} = State) ->
View
@@ -44,7 +44,7 @@
-record(state, {
name :: string(),
level :: integer(),
- mod_levels :: list(),
+ mod_levels = [],
fd :: file:io_device(),
inode :: integer(),
flap=false :: boolean(),
@@ -100,7 +100,7 @@ handle_call({clear_loglevel, []}, State) ->
handle_call({clear_loglevel, Module}, #state{mod_levels = ModLvls} = State) ->
{ok, ok, State#state{ mod_levels = lists:keydelete(Module, 1, ModLvls) }};
handle_call(get_loglevel, #state{level=GenLevel, mod_levels = ModLvls} = State) ->
- Level = erlang:tl(lists:sort([GenLevel | [ ModLvl || {_, ModLvl} <- ModLvls ]])),
+ Level = erlang:hd(lists:reverse(lists:sort([GenLevel | [ ModLvl || {_, ModLvl} <- ModLvls ]]))),
{ok, Level, State};
handle_call({get_mod_loglevel, Module}, #state{mod_levels = ModLvls} = State) ->
case lists:keysearch(Module, 1, ModLvls) of
@@ -113,14 +113,13 @@ handle_call(_Request, State) ->
{ok, ok, State}.
%% @private
-handle_event({log, {Dest, Module}, Level, {Date, Time}, Message},
- #state{name=Name, level=GenLevel, mod_levels=ModLvls} = State) ->
- case lists:member({lager_file_backend, Name}, Dest)
- andalso lager_util:check_loglevel(GenLevel, ModLvls, Module, Level)of
- true ->
- {ok, write(State, Level, [Date, " ", Time, " ", Message, "\n"])};
- false ->
- {ok, State}
+handle_event({log_dest, Dest, Level, {Date, Time}, Message},
+ #state{name=Name, level=L} = State) when Level > L ->
+ case lists:member({lager_file_backend, Name}, Dest) of
+ true ->
+ {ok, write(State, Level, [Date, " ", Time, " ", Message, "\n"])};
+ false ->
+ {ok, State}
end;
handle_event({log, Module, Level, {Date, Time}, Message},
#state{level=GenLevel, mod_levels=ModLvls} = State) ->
@@ -23,7 +23,7 @@
-export([init/1, handle_call/2, handle_event/2, handle_info/2, terminate/2,
code_change/3]).
--record(state, {level, buffer, ignored}).
+-record(state, {level, mod_levels, buffer, ignored}).
-compile([{parse_transform, lager_transform}]).
-ifdef(TEST).
@@ -32,7 +32,7 @@
-endif.
init(Level) ->
- {ok, #state{level=lager_util:level_to_num(Level), buffer=[], ignored=[]}}.
+ {ok, #state{level=lager_util:level_to_num(Level), buffer=[], ignored=[], mod_levels=[]}}.
handle_call(count, #state{buffer=Buffer} = State) ->
{ok, length(Buffer), State};
@@ -47,20 +47,48 @@ handle_call(pop, #state{buffer=Buffer} = State) ->
[H|T] ->
{ok, H, State#state{buffer=T}}
end;
-handle_call(get_loglevel, #state{level=Level} = State) ->
+handle_call(get_loglevel, #state{level=GenLevel, mod_levels=ModLvls} = State) ->
+ Level = erlang:hd(lists:reverse(lists:sort([GenLevel | [ ModLvl || {_, ModLvl} <- ModLvls ]]))),
{ok, Level, State};
-handle_call({set_loglevel, Level}, State) ->
- {ok, ok, State#state{level=lager_util:level_to_num(Level)}};
-handle_call(_Request, State) ->
+handle_call({get_mod_loglevel, Module}, #state{mod_levels = ModLvls} = State) ->
+ case lists:keysearch(Module, 1, ModLvls) of
+ {value, {_, Level}} ->
+ {ok, Level, State};
+ false ->
+ {ok, false, State}
+ end;
+handle_call({set_loglevel, Level, Module}, #state{mod_levels = ModLvls} = State) ->
+ case Module of
+ '_' ->
+ {ok, ok, State#state{level=lager_util:level_to_num(Level)}};
+ _ ->
+ case lists:keymember(Module, 1, ModLvls) of
+ true ->
+ {ok, ok, State#state{mod_levels =
+ lists:keyreplace(Module, 1, ModLvls,
+ {Module, lager_util:level_to_num(Level)})}};
+ false ->
+ {ok, ok, State#state{mod_levels = [{Module, lager_util:level_to_num(Level)}|ModLvls]}}
+ end
+ end;
+handle_call({clear_loglevel, '_'}, State) ->
+ {ok, ok, State#state{ mod_levels = [] }};
+handle_call({clear_loglevel, Module}, #state{mod_levels = ModLvls} = State) ->
+ {ok, ok, State#state{ mod_levels = lists:keydelete(Module, 1, ModLvls) }};
+handle_call(Request, State) ->
+ io:format("Request: ~p~n", [Request]),
{ok, ok, State}.
-handle_event({log, [?MODULE], Level, Time, Message}, #state{buffer=Buffer} = State) ->
- {ok, State#state{buffer=Buffer ++ [{Level, Time, Message}]}};
-handle_event({log, Level, Time, Message}, #state{level=LogLevel,
- buffer=Buffer} = State) when Level =< LogLevel ->
+handle_event({log_dest, [?MODULE], Level, Time, Message}, #state{buffer=Buffer} = State) ->
{ok, State#state{buffer=Buffer ++ [{Level, Time, Message}]}};
-handle_event({log, _Level, _Time, _Message}, #state{ignored=Ignored} = State) ->
- {ok, State#state{ignored=Ignored ++ [ignored]}};
+handle_event({log, Module, Level, Time, Message}, #state{level=GenLevel, mod_levels=ModLvls,
+ buffer=Buffer, ignored=Ignored} = State) ->
+ case lager_util:check_loglevel(GenLevel, ModLvls, Module, Level) of
+ true ->
+ {ok, State#state{buffer=Buffer ++ [{Level, Time, Message}]}};
+ false ->
+ {ok, State#state{ignored=Ignored ++ [ignored]}}
+ end;
handle_event(_Event, State) ->
{ok, State}.
@@ -222,6 +250,30 @@ lager_test_() ->
?assertEqual(1, count()),
ok
end
+ },
+ {"per module loglevel works",
+ fun() ->
+ lager:set_loglevel(?MODULE, info),
+ ok = lager:info("Will be logged"),
+ ?assertEqual(1, count()),
+ ?assertEqual(0, count_ignored()),
+ ok = lager:debug("Won't be sent to backend"),
+ ?assertEqual(1, count()),
+ ?assertEqual(0, count_ignored()),
+ lager:set_mod_loglevel(?MODULE, error, ?MODULE),
+ ok = lager:info("Will be ignored because of higher loglevel"),
+ ?assertEqual(1, count()),
+ ?assertEqual(1, count_ignored()),
+ lager:set_mod_loglevel(?MODULE, debug, ?MODULE),
+ ok = lager:debug("Will be logged because of lower loglevel"),
+ ?assertEqual(2, count()),
+ ?assertEqual(1, count_ignored()),
+ lager:clear_mod_loglevel(?MODULE, ?MODULE),
+ ok = lager:debug("Won't be sent to backend"),
+ ?assertEqual(2, count()),
+ ?assertEqual(1, count_ignored()),
+ ok
+ end
}
]
}.

0 comments on commit 7f40838

Please sign in to comment.