Skip to content

Commit

Permalink
Log file and line numbers when available in the standard logger
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Dushin <fred@dushin.net>
  • Loading branch information
fadushin committed Nov 14, 2023
1 parent 11f0da3 commit 503a1bd
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion libs/estdlib/src/logger_std_h.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
log/2
]).

%% @private
format_msg({report, Report}) when is_map(Report) ->
io_lib:format("~p", [Report]);
format_msg({Format, Args}) when is_list(Format) andalso is_list(Args) ->
io_lib:format(Format, Args).

%% @hidden
log(LogEvent, _Config) ->
#{
level := Level,
Expand All @@ -38,14 +40,16 @@ log(LogEvent, _Config) ->
timestamp := Timestamp,
meta := MetaData
} = LogEvent,
io:format("~s [~p] ~p ~s~s~n", [
io:format("~s [~p] ~p ~s~s~s~n", [
make_timestamp(Timestamp),
Level,
Pid,
maybe_format_mfa(MetaData),
maybe_format_file_line(MetaData),
format_msg(Msg)
]).

%% @private
make_timestamp(Timestamp) ->
{{Year, Month, Day}, {Hour, Minute, Second}} =
calendar:system_time_to_universal_time(Timestamp, microsecond),
Expand All @@ -59,19 +63,28 @@ make_timestamp(Timestamp) ->
maybe_pad_100((Timestamp rem 1000000) div 1000)
]).

%% @private
maybe_pad(N) when N >= 0 andalso N < 10 ->
[$0 | integer_to_list(N)];
maybe_pad(N) ->
integer_to_list(N).

%% @private
maybe_pad_100(N) when N >= 0 andalso N < 10 ->
[$0, $0 | integer_to_list(N)];
maybe_pad_100(N) when N >= 0 andalso N < 100 ->
[$0 | integer_to_list(N)];
maybe_pad_100(N) ->
integer_to_list(N).

%% @private
maybe_format_mfa(#{location := #{mfa := {Module, FunctionName, FunctionArity}}} = _MetaData) ->
io_lib:format("~p:~p/~p ", [Module, FunctionName, FunctionArity]);
maybe_format_mfa(_MetaData) ->
"".

%% @private
maybe_format_file_line(#{location := #{file := File, line := Line}} = _MetaData) ->
io_lib:format("(~s:~p) ", [File, Line]);
maybe_format_file_line(_MetaData) ->
"".

0 comments on commit 503a1bd

Please sign in to comment.