Skip to content
Browse files

Change the default formatter to be backwards compatible with old beha…

…viour

Had to add a pseudo-ternary operator to the formatter to support this.
Also allowed lager:log to log metadata.
  • Loading branch information...
1 parent 0415d21 commit 7b051642498ff16c04ac3f7138f1cb651119d751 @Vagabond Vagabond committed Feb 27, 2012
Showing with 44 additions and 11 deletions.
  1. +10 −7 src/lager.erl
  2. +34 −4 src/lager_default_formatter.erl
View
17 src/lager.erl
@@ -53,14 +53,13 @@ start_ok(App, {error, Reason}) ->
erlang:error({app_start_failed, App, Reason}).
--spec dispatch_log(log_level(), list(), string(), list() | none, integer()) -> ok | {error, lager_not_running}.
+-spec dispatch_log(log_level(), list(), string(), list() | none, pos_integer()) -> ok | {error, lager_not_running}.
dispatch_log(Severity, Metadata, Format, Args, Size) when is_atom(Severity)->
case whereis(lager_event) of
undefined ->
%% lager isn't running
{error, lager_not_running};
Pid ->
-
{LevelThreshold,TraceFilters} = lager_mochiglobal:get(loglevel,{?LOG_NONE,[]}),
SeverityAsInt=lager_util:level_to_num(Severity),
Destinations = case TraceFilters of
@@ -72,7 +71,7 @@ dispatch_log(Severity, Metadata, Format, Args, Size) when is_atom(Severity)->
true ->
Timestamp = lager_util:format_time(),
Msg=case Args of
- A when is_list(A) ->safe_format_chop(Format,Args, Size);
+ A when is_list(A) ->safe_format_chop(Format,Args,Size);
_ -> Format
end,
gen_event:sync_notify(Pid, #lager_log_message{destinations=Destinations,
@@ -87,13 +86,17 @@ dispatch_log(Severity, Metadata, Format, Args, Size) when is_atom(Severity)->
%% @doc Manually log a message into lager without using the parse transform.
-spec log(log_level(), pid(), list()) -> ok | {error, lager_not_running}.
-log(Level, Pid, Message) ->
- dispatch_log(Level, [{pid,Pid}], Message,none, 4096).
+log(Level, Pid, Message) when is_pid(Pid) ->
+ dispatch_log(Level, [{pid,Pid}], Message, none, 4096);
+log(Level, Metadata, Message) when is_list(Metadata) ->
+ dispatch_log(Level, Metadata, Message, none, 4096).
%% @doc Manually log a message into lager without using the parse transform.
-spec log(log_level(), pid(), string(), list()) -> ok | {error, lager_not_running}.
-log(Level, Pid, Format, Args) ->
- dispatch_log(Level, [{pid,Pid}], Format, Args, 4096).
+log(Level, Pid, Format, Args) when is_pid(Pid) ->
+ dispatch_log(Level, [{pid,Pid}], Format, Args, 4096);
+log(Level, Metadata, Format, Args) when is_list(Metadata) ->
+ dispatch_log(Level, Metadata, Format, Args, 4096).
trace_file(File, Filter) ->
trace_file(File, Filter, debug).
View
38 src/lager_default_formatter.erl
@@ -37,7 +37,15 @@
%% @end
-spec format(#lager_log_message{},list()) -> any().
format(#lager_log_message{}=Msg,[]) ->
- format(Msg,[date, " ", time," [",severity,"] ",pid, " ", message, "\n"]);
+ format(Msg,
+ [date, " ", time, " [", severity, "] ",
+ {pid, ""},
+ {module, [
+ {pid, ["@"], ""},
+ module,
+ {function, [":", function], ""},
+ {line, [":",line], ""}], ""},
+ " ", message, "\n"]);
format(Message,Config) ->
[ output(V,Message) || V <- Config ].
@@ -46,9 +54,20 @@ format(Message,Config) ->
output(message,#lager_log_message{message=M}) -> M;
output(date,#lager_log_message{timestamp={D,_T}}) -> D;
output(time,#lager_log_message{timestamp={_D,T}}) -> T;
-output(severity,#lager_log_message{severity_as_int=S}) -> atom_to_list(lager_util:num_to_level(S));
-output(Prop,#lager_log_message{metadata=Metadata}) when is_atom(Prop) -> make_printable(proplists:get_value(Prop,Metadata,<<"Undefined">>));
-output({Prop,Default},#lager_log_message{metadata=Metadata}=L) when is_atom(Prop) -> make_printable(proplists:get_value(Prop,Metadata,output(Default,L)));
+output(severity,#lager_log_message{severity_as_int=S}) ->
+ atom_to_list(lager_util:num_to_level(S));
+output(Prop,#lager_log_message{metadata=Metadata}) when is_atom(Prop) ->
+ make_printable(get_metadata(Prop,Metadata,<<"Undefined">>));
+output({Prop,Default},#lager_log_message{metadata=Metadata}=L) when is_atom(Prop) ->
+ make_printable(get_metadata(Prop,Metadata,output(Default,L)));
+output({Prop, Present, Absent}, #lager_log_message{metadata=Metadata}=L) when is_atom(Prop) ->
+ %% sort of like a poor man's ternary operator
+ case get_metadata(Prop, Metadata) of
+ undefined ->
+ [ output(V, L) || V <- Absent];
+ _ ->
+ [ output(V, L) || V <- Present]
+ end;
output(Other,_) -> make_printable(Other).
-spec make_printable(any()) -> iolist().
@@ -57,6 +76,17 @@ make_printable(P) when is_pid(P) -> pid_to_list(P);
make_printable(L) when is_list(L) orelse is_binary(L) -> L;
make_printable(Other) -> io_lib:format("~p",[Other]).
+get_metadata(Key, Metadata) ->
+ get_metadata(Key, Metadata, undefined).
+
+get_metadata(Key, Metadata, Default) ->
+ case lists:keyfind(Key, 1, Metadata) of
+ false ->
+ Default;
+ {Key, Value} ->
+ Value
+ end.
+
-ifdef(TEST).
basic_test_() ->
[{"Default formatting test",

0 comments on commit 7b05164

Please sign in to comment.
Something went wrong with that request. Please try again.