Skip to content
This repository
Browse code

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...
commit 7b051642498ff16c04ac3f7138f1cb651119d751 1 parent 0415d21
Andrew Thompson Vagabond authored

Showing 2 changed files with 44 additions and 11 deletions. Show diff stats Hide diff stats

  1. +10 7 src/lager.erl
  2. +34 4 src/lager_default_formatter.erl
17 src/lager.erl
@@ -53,14 +53,13 @@ start_ok(App, {error, Reason}) ->
53 53 erlang:error({app_start_failed, App, Reason}).
54 54
55 55
56   --spec dispatch_log(log_level(), list(), string(), list() | none, integer()) -> ok | {error, lager_not_running}.
  56 +-spec dispatch_log(log_level(), list(), string(), list() | none, pos_integer()) -> ok | {error, lager_not_running}.
57 57 dispatch_log(Severity, Metadata, Format, Args, Size) when is_atom(Severity)->
58 58 case whereis(lager_event) of
59 59 undefined ->
60 60 %% lager isn't running
61 61 {error, lager_not_running};
62 62 Pid ->
63   -
64 63 {LevelThreshold,TraceFilters} = lager_mochiglobal:get(loglevel,{?LOG_NONE,[]}),
65 64 SeverityAsInt=lager_util:level_to_num(Severity),
66 65 Destinations = case TraceFilters of
@@ -72,7 +71,7 @@ dispatch_log(Severity, Metadata, Format, Args, Size) when is_atom(Severity)->
72 71 true ->
73 72 Timestamp = lager_util:format_time(),
74 73 Msg=case Args of
75   - A when is_list(A) ->safe_format_chop(Format,Args, Size);
  74 + A when is_list(A) ->safe_format_chop(Format,Args,Size);
76 75 _ -> Format
77 76 end,
78 77 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)->
87 86
88 87 %% @doc Manually log a message into lager without using the parse transform.
89 88 -spec log(log_level(), pid(), list()) -> ok | {error, lager_not_running}.
90   -log(Level, Pid, Message) ->
91   - dispatch_log(Level, [{pid,Pid}], Message,none, 4096).
  89 +log(Level, Pid, Message) when is_pid(Pid) ->
  90 + dispatch_log(Level, [{pid,Pid}], Message, none, 4096);
  91 +log(Level, Metadata, Message) when is_list(Metadata) ->
  92 + dispatch_log(Level, Metadata, Message, none, 4096).
92 93
93 94 %% @doc Manually log a message into lager without using the parse transform.
94 95 -spec log(log_level(), pid(), string(), list()) -> ok | {error, lager_not_running}.
95   -log(Level, Pid, Format, Args) ->
96   - dispatch_log(Level, [{pid,Pid}], Format, Args, 4096).
  96 +log(Level, Pid, Format, Args) when is_pid(Pid) ->
  97 + dispatch_log(Level, [{pid,Pid}], Format, Args, 4096);
  98 +log(Level, Metadata, Format, Args) when is_list(Metadata) ->
  99 + dispatch_log(Level, Metadata, Format, Args, 4096).
97 100
98 101 trace_file(File, Filter) ->
99 102 trace_file(File, Filter, debug).
38 src/lager_default_formatter.erl
@@ -37,7 +37,15 @@
37 37 %% @end
38 38 -spec format(#lager_log_message{},list()) -> any().
39 39 format(#lager_log_message{}=Msg,[]) ->
40   - format(Msg,[date, " ", time," [",severity,"] ",pid, " ", message, "\n"]);
  40 + format(Msg,
  41 + [date, " ", time, " [", severity, "] ",
  42 + {pid, ""},
  43 + {module, [
  44 + {pid, ["@"], ""},
  45 + module,
  46 + {function, [":", function], ""},
  47 + {line, [":",line], ""}], ""},
  48 + " ", message, "\n"]);
41 49 format(Message,Config) ->
42 50 [ output(V,Message) || V <- Config ].
43 51
@@ -46,9 +54,20 @@ format(Message,Config) ->
46 54 output(message,#lager_log_message{message=M}) -> M;
47 55 output(date,#lager_log_message{timestamp={D,_T}}) -> D;
48 56 output(time,#lager_log_message{timestamp={_D,T}}) -> T;
49   -output(severity,#lager_log_message{severity_as_int=S}) -> atom_to_list(lager_util:num_to_level(S));
50   -output(Prop,#lager_log_message{metadata=Metadata}) when is_atom(Prop) -> make_printable(proplists:get_value(Prop,Metadata,<<"Undefined">>));
51   -output({Prop,Default},#lager_log_message{metadata=Metadata}=L) when is_atom(Prop) -> make_printable(proplists:get_value(Prop,Metadata,output(Default,L)));
  57 +output(severity,#lager_log_message{severity_as_int=S}) ->
  58 + atom_to_list(lager_util:num_to_level(S));
  59 +output(Prop,#lager_log_message{metadata=Metadata}) when is_atom(Prop) ->
  60 + make_printable(get_metadata(Prop,Metadata,<<"Undefined">>));
  61 +output({Prop,Default},#lager_log_message{metadata=Metadata}=L) when is_atom(Prop) ->
  62 + make_printable(get_metadata(Prop,Metadata,output(Default,L)));
  63 +output({Prop, Present, Absent}, #lager_log_message{metadata=Metadata}=L) when is_atom(Prop) ->
  64 + %% sort of like a poor man's ternary operator
  65 + case get_metadata(Prop, Metadata) of
  66 + undefined ->
  67 + [ output(V, L) || V <- Absent];
  68 + _ ->
  69 + [ output(V, L) || V <- Present]
  70 + end;
52 71 output(Other,_) -> make_printable(Other).
53 72
54 73 -spec make_printable(any()) -> iolist().
@@ -57,6 +76,17 @@ make_printable(P) when is_pid(P) -> pid_to_list(P);
57 76 make_printable(L) when is_list(L) orelse is_binary(L) -> L;
58 77 make_printable(Other) -> io_lib:format("~p",[Other]).
59 78
  79 +get_metadata(Key, Metadata) ->
  80 + get_metadata(Key, Metadata, undefined).
  81 +
  82 +get_metadata(Key, Metadata, Default) ->
  83 + case lists:keyfind(Key, 1, Metadata) of
  84 + false ->
  85 + Default;
  86 + {Key, Value} ->
  87 + Value
  88 + end.
  89 +
60 90 -ifdef(TEST).
61 91 basic_test_() ->
62 92 [{"Default formatting test",

0 comments on commit 7b05164

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