1 |
|
-module(mongoose_flatlog_formatter). |
2 |
|
-export([format/2]). |
3 |
|
|
4 |
|
-ignore_xref([format/2]). |
5 |
|
|
6 |
|
-spec format(logger:log_event(), logger:formatter_config()) -> unicode:chardata(). |
7 |
|
format(Event, FConfig) -> |
8 |
15618 |
try do_format(Event, FConfig) |
9 |
|
catch |
10 |
|
%% Errors during log formatting can lead to a death spiral of recursive error logging, so |
11 |
|
%% format the formatter error in a safe way and don't allow the exception to propagate. |
12 |
:-( |
error:Reason:Stacktrace -> format_log_formatter_error(error, Reason, Stacktrace, Event, FConfig) |
13 |
|
end. |
14 |
|
|
15 |
|
|
16 |
|
format_log_formatter_error(Class, Reason, Stacktrace, #{meta := Meta} = Event, FConfig) -> |
17 |
:-( |
flatlog:format( |
18 |
|
#{ |
19 |
|
level => error, |
20 |
|
msg => {report, #{ |
21 |
|
class => Class, reason => Reason, stacktrace => Stacktrace, |
22 |
|
formatter_module => ?MODULE, |
23 |
|
original_event => unicode:characters_to_binary(io_lib:format("~0p", [Event])) |
24 |
|
}}, |
25 |
|
meta => Meta#{what => log_format_failed} |
26 |
|
}, |
27 |
|
FConfig#{template => template()} |
28 |
|
). |
29 |
|
|
30 |
|
|
31 |
|
do_format(Map, UsrConfig) -> |
32 |
15618 |
Map2 = mongoose_log_filter:fill_metadata_filter(Map, fields()), |
33 |
15618 |
flatlog:format(Map2, UsrConfig#{template => template()}). |
34 |
|
|
35 |
|
fields() -> |
36 |
15618 |
[what, text, user, from_jid, to_jid, |
37 |
|
class, reason, stacktrace]. |
38 |
|
|
39 |
|
template() -> |
40 |
15618 |
[colored_start, "when=", time, " level=", level, |
41 |
|
{what, [" what=", what], ""}, |
42 |
|
{reason, [" reason=", reason], ""}, |
43 |
|
{pid, [" pid=", pid], ""}, " at=", mfa, ":", line, colored_end, |
44 |
|
{user, [" user=", user], ""}, |
45 |
|
{from_jid, [" from_jid=", from_jid], ""}, |
46 |
|
{to_jid, [" to_jid=", to_jid], ""}, |
47 |
|
{stacktrace, [" stacktrace=", stacktrace], ""}, |
48 |
|
{text, [" text=", text], ""}, " ", |
49 |
|
msg, "\n"]. |