如何从Erlang shell登录到文件?

hs1ihplo  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(103)

一直忘记Logging chapter of the the Kernel User's Guide已经有了答案。

voase2hg

voase2hg1#

Paraphrasing the 2.9 Example: Add a handler to log info events to file section in the Logging chapter of the Kernel User's Guide:

1. Set log level (default: notice )

  • Globally: logger:set_primary_config/2
  • For certain modules only: logger:set_module_level/2

Accepted log levels (from least severe to most):
debug , info , notice , warning , error , critical , alert , emergency

Note:

The default log level in the Erlang shell is notice , so if you leave it as is, but set a lower level (such as debug or info ) when adding a log handler in the next step, those level of logs will never get through.

Example:

logger:set_primary_config(level, debug).

2. Configure and add log handler

Specify the handler configuration map, for example:

Config = #{config => #{file => "./sample.log"}, level => debug}.

And add the handler:

logger:add_handler(to_file_handler, logger_std_h, Config).

logger_std_h is the standard handler for Logger.

3. Filter out logs below a certain level on Erlang shell

Following the examples above, all levels of logs will be printed. To restore the notice default, but still save every level of logs in the file), use logger:set_handler_config/3.

logger:set_handler_config(default, level, notice).

Work in progress: Log each process' events into their own logfile

This module documents my (partially successful) attempts; will revisit and expand on this section when time permits. My use case was that the FreeSWITCH phone server would spawn an Erlang process to handle the call, and so logging each of them into their own files made sense at the time.

相关问题