./ct_report/coverage/mongoose_json_formatter.COVER.html

1 %%%-------------------------------------------------------------------
2 %%% @copyright 2020, Erlang Solutions Ltd.
3 %%% @doc
4 %%% A module formatting logger reports into JSON objects.
5 %%%
6 %%% @end
7 %%%-------------------------------------------------------------------
8 -module(mongoose_json_formatter).
9
10 %% API
11 -export([format/2]).
12
13 -ignore_xref([format/2]).
14
15 -spec format(logger:log_event(), logger:formatter_config()) -> unicode:chardata().
16 format(Event, FConfig) ->
17 8680 try do_format(Event, FConfig)
18 catch
19 %% Errors during log formatting can lead to a death spiral of recursive error logging, so
20 %% format the formatter error in a safe way and don't allow the exception to propagate.
21
:-(
error:Reason:Stacktrace -> format_log_formatter_error(error, Reason, Stacktrace, Event, FConfig)
22 end.
23
24
25 format_log_formatter_error(Class, Reason, Stacktrace, #{meta := #{time := Time}} = Event, FConfig) ->
26
:-(
IoData = jiffy:encode(
27 #{
28 'when' => format_time(Time),
29 level => error,
30 what => log_format_failed,
31 class => Class,
32 reason => Reason,
33 stacktrace => format_item(Stacktrace, FConfig),
34 formatter_module => ?MODULE,
35 original_event => unicode:characters_to_binary(io_lib:format("~0p", [Event]))
36 }
37 ),
38
:-(
[IoData, <<"\n">>].
39
40
41 -spec do_format(logger:log_event(), logger:formatter_config()) -> unicode:chardata().
42 do_format(E = #{msg := {report, #{label := {error_logger, _}, format := Format, args := Terms}}}, FConfig) ->
43 25 do_format(E#{msg := {report,
44 #{unstructured_log =>
45 unicode:characters_to_binary(io_lib:format(Format, Terms))}}},
46 FConfig);
47 do_format(E = #{msg := {string, String}}, FConfig) ->
48
:-(
do_format(E#{msg := {report,
49 #{unstructured_log =>
50 unicode:characters_to_binary(io_lib:format(String, []))}}}, FConfig);
51 do_format(E = #{msg := {report, Report}}, FConfig) when is_map(Report) ->
52 8680 NewMap = process_metadata(E),
53 8680 NewReport = maps:merge(Report, NewMap),
54 8680 NewConfig = maps:merge(default_config(), config_correct_depth(FConfig)),
55 8680 Formatted = format_item(NewReport, NewConfig),
56 8680 IoData = jiffy:encode(Formatted),
57 8680 [IoData, <<"\n">>];
58 do_format(Map = #{msg := {Format, Terms}}, FConfig) ->
59 612 do_format(Map#{msg := {report,
60 #{unstructured_log =>
61 unicode:characters_to_binary(io_lib:format(Format, Terms))}}},
62 FConfig).
63
64
65 format_item(_Item, _FConfig = #{depth := 0}) ->
66
:-(
<<"...">>;
67 format_item(Item, FConfig = #{depth := D}) when is_map(Item) ->
68 19522 ML = [{all_to_binary(Key, FConfig),
69 19522 format_item(Val, FConfig#{depth := D - 1})} || {Key, Val} <- maps:to_list(Item)],
70 19522 maps:from_list(ML);
71 format_item(Item, FConfig = #{depth := Depth}) when is_list(Item) ->
72 16886 case io_lib:printable_unicode_list(Item) of
73 true ->
74 % We want to print strings as strings
75 13132 format_str(Item, FConfig);
76 false ->
77 % And pass lists of objects as lists of objects
78 3754 [format_item(I, FConfig#{depth := Depth - 1}) || I <- Item]
79 end;
80 format_item(Item, FConfig) ->
81 95627 all_to_binary(Item, FConfig).
82
83 format_time(T) ->
84 8680 TimeString = calendar:system_time_to_rfc3339(T, [{unit, microsecond}]),
85 8680 unicode:characters_to_binary(TimeString).
86
87 all_to_binary(Full, FConfig) when is_binary(Full) ->
88 25226 Short = shorten_binary(Full, FConfig),
89 25226 ShortUnicode = unicode:characters_to_binary(Short, utf8, utf8),
90 25226 FullUnicode = unicode:characters_to_binary(Short, utf8, utf8),
91 25226 case {ShortUnicode, FullUnicode} of
92 25205 {<<_/binary>>, <<_/binary>>} -> ShortUnicode;
93
:-(
{{incomplete,Incomplete,_}, <<_/binary>>} -> Incomplete;
94 21 _ -> format_non_unicode(Full, FConfig)
95 end;
96 all_to_binary(Something, FConfig) ->
97 182670 format_non_unicode(Something, FConfig).
98
99 format_non_unicode(Something, FConfig) ->
100 182691 Chars = format_str(Something, FConfig),
101 182691 unicode:characters_to_binary(Chars, utf8).
102
103 shorten_binary(S, #{format_chars_limit := unlimited}) ->
104
:-(
S;
105 shorten_binary(S, #{format_chars_limit := L}) ->
106 176368 binary_part(S, 0, min(size(S), L)).
107
108
:-(
format_chars_limit_to_opts(unlimited) -> [];
109 44681 format_chars_limit_to_opts(CharsLimit) -> [{chars_limit, CharsLimit}].
110
111 format_str(S, FConfig) when is_list(S) ->
112 151142 case io_lib:printable_unicode_list(S) of
113 true ->
114 151142 B = unicode:characters_to_binary(S, utf8),
115 151142 shorten_binary(B, FConfig);
116 false ->
117
:-(
do_format_str(S, FConfig)
118 end;
119 format_str(S, FConfig) when is_atom(S) ->
120 138010 format_str(atom_to_list(S), FConfig);
121 format_str(S, FConfig) ->
122 44681 do_format_str(S, FConfig).
123
124 do_format_str(S, #{format_depth := unlimited, format_chars_limit := L}) ->
125
:-(
io_lib:format("~0tp", [S], format_chars_limit_to_opts(L));
126 do_format_str(S, #{format_depth := D, format_chars_limit := L}) ->
127 44681 io_lib:format("~0tP", [S, D], format_chars_limit_to_opts(L)).
128
129 process_metadata(#{meta := #{time := T} = M, level := L}) ->
130 8680 DiscardKeys = [time, gl, report_cb],
131 8680 #{level => L,
132 'when' => format_time(T),
133 meta => maps:without(DiscardKeys, M)}.
134
135 config_correct_depth(C = #{depth := unlimited}) ->
136
:-(
C#{depth := -1};
137 config_correct_depth(C) ->
138 8680 C.
139
140 default_config() ->
141 8680 #{format_chars_limit => unlimited,
142 format_depth => unlimited,
143 depth => -1
144 }.
Line Hits Source