Open6
logger モジュール
モチベーション
そろそろ lager から卒業して、公式の logger に移行したので、その記録。
ドキュメント
ユーザズガイド
リファレンスモジュール
動かす
erl -kernel logger_level info
Erlang/OTP 25 [RELEASE CANDIDATE 1] [erts-13.0] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit]
=PROGRESS REPORT==== 3-Mar-2022::19:22:21.731352 ===
application: kernel
started_at: nonode@nohost
=PROGRESS REPORT==== 3-Mar-2022::19:22:21.737612 ===
application: stdlib
started_at: nonode@nohost
=PROGRESS REPORT==== 3-Mar-2022::19:22:21.743016 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.78.0>},
{id,disk_log_sup},
{mfargs,{disk_log_sup,start_link,[]}},
{restart_type,permanent},
{significant,false},
{shutdown,1000},
{child_type,supervisor}]
=PROGRESS REPORT==== 3-Mar-2022::19:22:21.743096 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.79.0>},
{id,disk_log_server},
{mfargs,{disk_log_server,start_link,[]}},
{restart_type,permanent},
{significant,false},
{shutdown,2000},
{child_type,worker}]
Eshell V13.0 (abort with ^G)
1>
ログの時間を UTC にする
2> logger:set_handler_config(default, formatter, {logger_formatter, #{}}).
ok
3> logger:error("Oh noes, another error").
2022-03-03T19:27:53.213144+09:00 error: Oh noes, another error
ok
4> logger:set_handler_config(default, formatter, {logger_formatter, #{time_offset => "Z"}}).
ok
5> logger:error("Oh noes, another error").
2022-03-03T10:27:57.570802Z error: Oh noes, another error
logger_formatter で time_offset を "Z" に指定する。
logger の handlers を見る
Eshell V13.0 (abort with ^G)
1> logger:i(handlers).
Handler configuration:
Id: default
Module: logger_std_h
Level: all
Formatter:
Module: logger_formatter
Config:
legacy_header: true
single_line: false
Filter Default: stop
Filters:
Id: remote_gl
Fun: fun logger_filters:remote_gl/2
Arg: stop
Id: domain
Fun: fun logger_filters:domain/2
Arg: {log,super,[otp,sasl]}
Id: no_domain
Fun: fun logger_filters:domain/2
Arg: {log,undefined,[]}
Handler Config:
burst_limit_enable: true
burst_limit_max_count: 500
burst_limit_window_time: 1000
drop_mode_qlen: 200
filesync_repeat_interval: no_repeat
flush_qlen: 1000
overload_kill_enable: false
overload_kill_mem_size: 3000000
overload_kill_qlen: 20000
overload_kill_restart_after: 5000
sync_mode_qlen: 10
type: standard_io
ok
独自フォーマッターを作る
OTP でいくつか独自フォーマッターを定義しているので、参考にする。
-
https://github.com/erlang/otp/blob/master/lib/kernel/src/logger_formatter.erl
- デフォルト
- https://github.com/erlang/otp/blob/master/lib/ssl/src/ssl_logger.erl
- https://github.com/erlang/otp/blob/master/lib/inets/src/http_server/httpd_logger.erl
- ferd/flatlog: A custom formatter for the Erlang logger application that turns maps into single line text logs
metadata を設定
template で指定できるフォーマット。
template() =
[metakey() |
{metakey(), template(), template()} |
unicode:chardata()]
1> logger:set_handler_config(default, formatter, {logger_formatter, #{template => [{pid, ["pid=", pid], []}, "\n"], single_line => true, time_offset => "Z"}}).
ok
2> logger:error("Oh noes, another error", [], #{pid => pid_to_list(self())}).
pid=<0.84.0>
ok
11> logger:set_process_metadata(#{a => 10}).
ok
12> logger:error("Oh noes, another error", [], #{}).
pid=<0.84.0>10
ok