./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 364 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 11 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 364 NewMap = process_metadata(E),
53 364 NewReport = maps:merge(Report, NewMap),
54 364 NewConfig = maps:merge(default_config(), config_correct_depth(FConfig)),
55 364 Formatted = format_item(NewReport, NewConfig),
56 364 IoData = jiffy:encode(Formatted),
57 364 [IoData, <<"\n">>];
58 do_format(Map = #{msg := {Format, Terms}}, FConfig) ->
59 10 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 1195 ML = [{all_to_binary(Key, FConfig),
69 1195 format_item(Val, FConfig#{depth := D - 1})} || {Key, Val} <- maps:to_list(Item)],
70 1195 maps:from_list(ML);
71 format_item(Item, FConfig = #{depth := Depth}) when is_list(Item) ->
72 1330 case io_lib:printable_unicode_list(Item) of
73 true ->
74 % We want to print strings as strings
75 762 format_str(Item, FConfig);
76 false ->
77 % And pass lists of objects as lists of objects
78 568 [format_item(I, FConfig#{depth := Depth - 1}) || I <- Item]
79 end;
80 format_item(Item, FConfig) ->
81 6143 all_to_binary(Item, FConfig).
82
83 format_time(T) ->
84 364 TimeString = calendar:system_time_to_rfc3339(T, [{unit, microsecond}]),
85 364 unicode:characters_to_binary(TimeString).
86
87 all_to_binary(Full, FConfig) when is_binary(Full) ->
88 575 Short = shorten_binary(Full, FConfig),
89 575 ShortUnicode = unicode:characters_to_binary(Short, utf8, utf8),
90 575 FullUnicode = unicode:characters_to_binary(Short, utf8, utf8),
91 575 case {ShortUnicode, FullUnicode} of
92 572 {<<_/binary>>, <<_/binary>>} -> ShortUnicode;
93
:-(
{{incomplete,Incomplete,_}, <<_/binary>>} -> Incomplete;
94 3 _ -> format_non_unicode(Full, FConfig)
95 end;
96 all_to_binary(Something, FConfig) ->
97 11110 format_non_unicode(Something, FConfig).
98
99 format_non_unicode(Something, FConfig) ->
100 11113 Chars = format_str(Something, FConfig),
101 11113 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 8538 binary_part(S, 0, min(size(S), L)).
107
108
:-(
format_chars_limit_to_opts(unlimited) -> [];
109 3912 format_chars_limit_to_opts(CharsLimit) -> [{chars_limit, CharsLimit}].
110
111 format_str(S, FConfig) when is_list(S) ->
112 7963 case io_lib:printable_unicode_list(S) of
113 true ->
114 7963 B = unicode:characters_to_binary(S, utf8),
115 7963 shorten_binary(B, FConfig);
116 false ->
117
:-(
do_format_str(S, FConfig)
118 end;
119 format_str(S, FConfig) when is_atom(S) ->
120 7201 format_str(atom_to_list(S), FConfig);
121 format_str(S, FConfig) ->
122 3912 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 3912 io_lib:format("~0tP", [S, D], format_chars_limit_to_opts(L)).
128
129 process_metadata(#{meta := #{time := T} = M, level := L}) ->
130 364 DiscardKeys = [time, gl, report_cb],
131 364 #{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 364 C.
139
140 default_config() ->
141 364 #{format_chars_limit => unlimited,
142 format_depth => unlimited,
143 depth => -1
144 }.
Line Hits Source