logger_formatter(3erl)
π¦ Welcome to logger_formatter(3erl) β Erlang Module Definition π¦
πΈ NAME πΈ
logger_formatter β the default formatter for Logger. π
π DESCRIPTION π
Okay so bestie, every Logger handler has a configured formatter, and that formatter is specified as a module plus a configuration term. The whole point of the formatter is to translate log events into one final printable string (unicode:chardata()) that can be written to the handlerβs output device. For the full lore, see sections Handlers and Formatters in the Kernel Userβs Guide.
logger_formatter is the default formatter used by Logger. Periodt. π
β¨ DATA TYPES β¨
config() =
#{chars_limit => integer() >= 1 | unlimited,
depth => integer() >= 1 | unlimited,
legacy_header => boolean(),
max_size => integer() >= 1 | unlimited,
report_cb => logger:report_cb(),
single_line => boolean(),
template => template(),
time_designator => byte(),
time_offset => integer() | [byte()]}
The configuration term for logger_formatter is a map, and these keys can be set as configuration parameters:
chars_limit = integer() > 0 | unlimitedβ a positive integer that becomes the value of the option with the same name when callingio_lib:format/3. It limits the total number of characters printed for each log event. Heads up: this is a soft limit. For a hard truncation limit, see optionmax_size. Defaults tounlimited.depth = integer() > 0 | unlimitedβ a positive integer for the maximum depth to which terms shall be printed by this formatter. Format strings passed to this formatter are rewritten: the format controls~pand~ware replaced with~Pand~W, respectively, and the value is used as the depth parameter. For details, seeio:format/2,3in STDLIB. Defaults tounlimited.legacy_header = boolean()β if set totrue, a header field is added tologger_formatterβs part of Metadata. The value of this field is a string similar to the header created by the olderror_loggerevent handlers. You can include it in the log event by adding the list[logger_formatter,header]to the template. See the description of thetemplate()type for more info. Defaults tofalse.max_size = integer() > 0 | unlimitedβ a positive integer for the absolute maximum size a string returned from this formatter can have. If the formatted string is longer β after possibly being limited bychars_limitordepthβ it is truncated. Defaults tounlimited.report_cb = logger:report_cb()β a report callback used by the formatter to transform log messages on report form into a format string and arguments. The report callback can be specified in the metadata for the log event. If no report callback exists in metadata,logger_formatterwill uselogger:format_report/1as default callback. If this configuration parameter is set, it replaces both the default report callback and any report callback found in metadata. That is, all reports are converted by this configured function. πsingle_line = boolean()β if set totrue, each log event is printed as a single line. To achieve this,logger_formattersets the field width to0for all~pand~Pcontrol sequences in the format string (seeio:format/2), and replaces all newlines in the message with", ". White spaces following directly after newlines are removed. Notice that newlines added by the template parameter are not replaced. Defaults totrue.template = template()β the template describes how the formatted string is composed by combining different data values from the log event. See the description of thetemplate()type for more information about this.time_designator = byte()β timestamps are formatted according to RFC3339, and the time designator is the character used as date and time separator. Defaults to$T. The value of this parameter is used as thetime_designatoroption tocalendar:system_time_to_rfc3339/2.time_offset = integer() | [byte()]β the time offset, either a string or an integer, to be used when formatting the timestamp. An empty string is interpreted as local time. The values"Z","z"or0are interpreted as Universal Coordinated Time (UTC). Strings, other than"Z","z", or"", must be on the formΒ±[hh]:[mm], for example"-02:00"or"+00:00". Integers must be in microseconds, meaning that the offset7200000000is equivalent to"+02:00". Defaults to an empty string, meaning that timestamps are displayed in local time. However, for backwards compatibility, if the SASL configuration parameterutc_log=true, the default is changed to"Z", meaning that timestamps are displayed in UTC. The value of this parameter is used as theoffsetoption tocalendar:system_time_to_rfc3339/2.
metakey() = atom() | [atom()]
template() =
[metakey() |
{metakey(), template(), template()} |
unicode:chardata()]
The template is a list of atoms, atom lists, tuples and strings. The atoms level or msg are treated as placeholders for the severity level and the log message, respectively. Other atoms or atom lists are interpreted as placeholders for metadata, where atoms are expected to match top level keys, and atom lists represent paths to sub keys when the metadata is a nested map.
For example, the list [key1,key2] is replaced by the value of the key2 field in the nested map below. The atom key1 on its own is replaced by the complete value of the key1 field. The values are converted to strings.
#{key1 => #{key2 => my_value,
...}
...}
Tuples in the template express if-exist tests for metadata keys. For example, the following tuple says that if key1 exists in the metadata map, print "key1=Value", where Value is the value that key1 is associated with in the metadata map. If key1 does not exist, print nothing.
{key1, ["key1=",key1], []}
Strings in the template are printed literally.
The default value for the template configuration parameter depends on the value of the single_line and legacy_header configuration parameters as follows.
The log event used in the examples is:
?LOG_ERROR("name: ~p~nexit_reason: ~p", [my_name, "It crashed"])
legacy_header = true, single_line = false:
Default template: [[logger_formatter,header],"\n",msg,"\n"]
Example log entry:
=ERROR REPORT==== 17-May-2018::18:30:19.453447 ===
name: my_name
exit_reason: "It crashed"
Notice that all eight levels can occur in the heading, not only ERROR, WARNING or INFO as error_logger produces. And microseconds are added at the end of the timestamp.
legacy_header = true, single_line = true:
Default template: [[logger_formatter,header],"\n",msg,"\n"]
Notice that the template is here the same as for single_line=false, but the resulting log entry differs in that there is only one line after the heading:
=ERROR REPORT==== 17-May-2018::18:31:06.952665 ===
name: my_name, exit_reason: "It crashed"
legacy_header = false, single_line = true:
Default template: [time," ",level,": ",msg,"\n"]
Example log entry:
2018-05-17T18:31:31.152864+02:00 error: name: my_name, exit_reason: "It crashed"
legacy_header = false, single_line = false:
Default template: [time," ",level,":\n",msg,"\n"]
Example log entry:
2018-05-17T18:32:20.105422+02:00 error:
name: my_name
exit_reason: "It crashed"
π EXPORTS π
check_config(Config) -> ok | {error, term()}
Types:
Config = config()
The function is called by Logger when the formatter configuration for a handler is set or modified. It returns ok if the configuration is valid, and {error,term()} if it is faulty.
The following Logger API functions can trigger this callback:
logger:add_handler/3logger:set_handler_config/2,3logger:update_handler_config/2logger:update_formatter_config/2
format(LogEvent, Config) -> unicode:chardata()
Types:
LogEvent = logger:log_event()
Config = config()
This is the formatter callback function to be called from handlers. The log event is processed as follows:
- If the message is on report form, it is converted to
{Format,Args}by calling the report callback. See section Log Message in the Kernel Userβs Guide for more information about report callbacks and valid forms of log messages. - The message size is limited according to the values of configuration parameters
chars_limitanddepth. - The full log entry is composed according to the template.
- If the final string is too long, it is truncated according to the value of configuration parameter
max_size.
π SEE ALSO π
calendar(3erl)error_logger(3erl)io(3erl)io_lib(3erl)logger(3erl)maps(3erl)sasl(7)unicode(3erl)
π Thatβs the whole page, bestie! Footer tea: Ericsson AB, kernel 8.5.4.2, logger_formatter(3erl) β and thereβs no date on this footer, so donβt come for me. π«Ά
π¦ where this page comes from
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4 Β· Apache-2.0
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4.8 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms