aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kernel/src
diff options
context:
space:
mode:
authorSiri Hansen <[email protected]>2018-06-05 12:35:37 +0200
committerSiri Hansen <[email protected]>2018-06-08 14:41:51 +0200
commit437d46f34b141af8e6361bfc5b2fe8dab9d88efd (patch)
tree83e3cc4d4df0d4a8d6ed56c7b0bcecf290d0f34d /lib/kernel/src
parent983a945b84e15f58c3205c37b7bfcad18537325c (diff)
downloadotp-437d46f34b141af8e6361bfc5b2fe8dab9d88efd.tar.gz
otp-437d46f34b141af8e6361bfc5b2fe8dab9d88efd.tar.bz2
otp-437d46f34b141af8e6361bfc5b2fe8dab9d88efd.zip
[logger] Add if-exist functionality for metadata keys in formatter template
* Nested metadata keys are now expressed as list of atoms (was earlier tuples). * If-exist is expressed as: {Key,IfExist,Else} Key :: atom() | [atom()] IfExist :: template() Else :: template()
Diffstat (limited to 'lib/kernel/src')
-rw-r--r--lib/kernel/src/logger_formatter.erl100
-rw-r--r--lib/kernel/src/logger_internal.hrl2
2 files changed, 71 insertions, 31 deletions
diff --git a/lib/kernel/src/logger_formatter.erl b/lib/kernel/src/logger_formatter.erl
index 4d727b3da0..6eeac994ea 100644
--- a/lib/kernel/src/logger_formatter.erl
+++ b/lib/kernel/src/logger_formatter.erl
@@ -53,8 +53,8 @@ format(#{level:=Level,msg:=Msg0,meta:=Meta},Config0)
[msg|Rest] -> {true,Rest};
_ ->{false,AT0}
end,
- B = do_format(Level,"",Meta1,BT,Config),
- A = do_format(Level,"",Meta1,AT,Config),
+ B = do_format(Level,Meta1,BT,Config),
+ A = do_format(Level,Meta1,AT,Config),
MsgStr =
if DoMsg ->
Config1 =
@@ -84,26 +84,37 @@ format(#{level:=Level,msg:=Msg0,meta:=Meta},Config0)
end,
truncate(B ++ MsgStr ++ A,maps:get(max_size,Config)).
-do_format(Level,Msg,Data,[level|Format],Config) ->
- [to_string(level,Level,Config)|do_format(Level,Msg,Data,Format,Config)];
-do_format(Level,Msg,Data,[Key|Format],Config) when is_atom(Key); is_tuple(Key) ->
- Value = value(Key,Data),
- [to_string(Key,Value,Config)|do_format(Level,Msg,Data,Format,Config)];
-do_format(Level,Msg,Data,[Str|Format],Config) ->
- [Str|do_format(Level,Msg,Data,Format,Config)];
-do_format(_Level,_Msg,_Data,[],_Config) ->
+do_format(Level,Data,[level|Format],Config) ->
+ [to_string(level,Level,Config)|do_format(Level,Data,Format,Config)];
+do_format(Level,Data,[{Key,IfExist,Else}|Format],Config) ->
+ String =
+ case value(Key,Data) of
+ {ok,Value} -> do_format(Level,Data#{Key=>Value},IfExist,Config);
+ error -> do_format(Level,Data,Else,Config)
+ end,
+ [String|do_format(Level,Data,Format,Config)];
+do_format(Level,Data,[Key|Format],Config)
+ when is_atom(Key) orelse
+ (is_list(Key) andalso is_atom(hd(Key))) ->
+ String =
+ case value(Key,Data) of
+ {ok,Value} -> to_string(Key,Value,Config);
+ error -> ""
+ end,
+ [String|do_format(Level,Data,Format,Config)];
+do_format(Level,Data,[Str|Format],Config) ->
+ [Str|do_format(Level,Data,Format,Config)];
+do_format(_Level,_Data,[],_Config) ->
[].
-value(Key,Meta) when is_atom(Key), is_map(Meta) ->
- maps:get(Key,Meta,"");
-value(Key,_) when is_atom(Key) ->
- "";
-value(Keys,Meta) when is_tuple(Keys) ->
- value(tuple_to_list(Keys),Meta);
-value([Key|Keys],Meta) ->
- value(Keys,value(Key,Meta));
+value(Key,Meta) when is_map_key(Key,Meta) ->
+ {ok,maps:get(Key,Meta)};
+value([Key|Keys],Meta) when is_map_key(Key,Meta) ->
+ value(Keys,maps:get(Key,Meta));
value([],Value) ->
- Value.
+ {ok,Value};
+value(_,_) ->
+ error.
to_string(time,Time,Config) ->
format_time(Time,Config);
@@ -343,17 +354,10 @@ do_check_config([{legacy_header,LH}|Config]) when is_boolean(LH) ->
do_check_config(Config);
do_check_config([{report_cb,RCB}|Config]) when is_function(RCB,1) ->
do_check_config(Config);
-do_check_config([{template,T}|Config]) when is_list(T) ->
- case lists:all(fun(X) when is_atom(X) -> true;
- (X) when is_tuple(X), is_atom(element(1,X)) -> true;
- (X) when is_list(X) -> io_lib:printable_unicode_list(X);
- (_) -> false
- end,
- T) of
- true ->
- do_check_config(Config);
- false ->
- {error,{invalid_formatter_template,?MODULE,T}}
+do_check_config([{template,T}|Config]) ->
+ case check_template(T) of
+ ok -> do_check_config(Config);
+ error -> {error,{invalid_formatter_template,?MODULE,T}}
end;
do_check_config([{time_offset,Offset}|Config]) ->
case check_offset(Offset) of
@@ -381,6 +385,42 @@ check_limit(unlimited) ->
check_limit(_) ->
error.
+check_template([Key|T]) when is_atom(Key) ->
+ check_template(T);
+check_template([Key|T]) when is_list(Key), is_atom(hd(Key)) ->
+ case lists:all(fun(X) when is_atom(X) -> true;
+ (_) -> false
+ end,
+ Key) of
+ true ->
+ check_template(T);
+ false ->
+ error
+ end;
+check_template([{Key,IfExist,Else}|T])
+ when is_atom(Key) orelse
+ (is_list(Key) andalso is_atom(hd(Key))) ->
+ case check_template(IfExist) of
+ ok ->
+ case check_template(Else) of
+ ok ->
+ check_template(T);
+ error ->
+ error
+ end;
+ error ->
+ error
+ end;
+check_template([Str|T]) when is_list(Str) ->
+ case io_lib:printable_unicode_list(Str) of
+ true -> check_template(T);
+ false -> error
+ end;
+check_template([]) ->
+ ok;
+check_template(_) ->
+ error.
+
check_offset(I) when is_integer(I) ->
ok;
check_offset(Tz) when Tz=:=""; Tz=:="Z"; Tz=:="z" ->
diff --git a/lib/kernel/src/logger_internal.hrl b/lib/kernel/src/logger_internal.hrl
index a6648d2b2c..a0962cb8e0 100644
--- a/lib/kernel/src/logger_internal.hrl
+++ b/lib/kernel/src/logger_internal.hrl
@@ -32,7 +32,7 @@
-define(DEFAULT_FORMAT_CONFIG,#{legacy_header=>true,
single_line=>false}).
-define(DEFAULT_FORMAT_TEMPLATE_HEADER,
- [{logger_formatter,header},"\n",msg,"\n"]).
+ [[logger_formatter,header],"\n",msg,"\n"]).
-define(DEFAULT_FORMAT_TEMPLATE_SINGLE,
[time," ",level,": ",msg,"\n"]).
-define(DEFAULT_FORMAT_TEMPLATE,