logger(3erl) | Erlang Module Definition | logger(3erl) |
logger - API module for Logger, the standard logging facility
in Erlang/OTP.
This module implements the main API for logging in Erlang/OTP. To create a log event, use the API functions or the log macros, for example:
?LOG_ERROR("error happened because: ~p", [Reason]). % With macro logger:error("error happened because: ~p", [Reason]). % Without macro
To configure the Logger backend, use Kernel configuration parameters or configuration functions in the Logger API.
By default, the Kernel application installs one log handler at system start. This handler is named default. It receives and processes standard log events produced by the Erlang runtime system, standard behaviours and different Erlang/OTP applications. The log events are by default printed to the terminal.
If you want your systems logs to be printed to a file instead, you must configure the default handler to do so. The simplest way is to include the following in your sys.config:
[{kernel,
[{logger,
[{handler, default, logger_std_h,
#{config => #{type => {file, "path/to/file.log"}}}}]}]}].
For more information about:
filter() =
{fun((log_event(), filter_arg()) -> filter_return()),
filter_arg()}
A filter which can be installed as a handler filter, or as a primary filter in Logger.
filter_arg() = term()
The second argument to the filter fun.
filter_id() = atom()
A unique identifier for a filter.
filter_return() = stop | ignore | log_event()
The return value from the filter fun.
formatter_config() = #{atom() => term()}
Configuration data for the formatter. See logger_formatter(3erl) for an example of a formatter implementation.
handler_config() =
#{id => handler_id(),
config => term(),
level => level() | all | none,
module => module(),
filter_default => log | stop,
filters => [{filter_id(), filter()}],
formatter => {module(), formatter_config()}}
Handler configuration data for Logger. The following default values apply:
In addition to these, the following fields are automatically inserted by Logger, values taken from the two first parameters to add_handler/3:
These are read-only and cannot be changed in runtime.
Handler specific configuration data is inserted by the handler callback itself, in a sub structure associated with the field named config. See the logger_std_h(3erl) and logger_disk_log_h manual pages for information about the specifc configuration for these handlers.
See the logger_formatter(3erl) manual page for information about the default configuration for this formatter.
handler_id() = atom()
A unique identifier for a handler instance.
level() =
emergency |
alert |
critical |
error |
warning |
notice |
info |
debug
The severity level for the message to be logged.
log_event() =
#{level := level(),
msg :=
{io:format(), [term()]} |
{report, report()} |
{string, unicode:chardata()},
meta := metadata()}
metadata() =
#{pid => pid(),
gl => pid(),
time => timestamp(),
mfa => {module(), atom(), integer() >= 0},
file => file:filename(),
line => integer() >= 0,
domain => [atom()],
report_cb => report_cb(),
atom() => term()}
Metadata for the log event.
Logger adds the following metadata to each log event:
When a log macro is used, Logger also inserts location information:
You can add custom metadata, either by specifying a map as the last parameter to any of the log macros or the API functions, or by setting process metadata with set_process_metadata/1 or update_process_metadata/1.
Logger merges all the metadata maps before forwarding the log event to the handlers. If the same keys occur, values from the log call overwrite process metadata, which in turn overwrite values set by Logger.
The following custom metadata keys have special meaning:
msg_fun() =
fun((term()) ->
{io:format(), [term()]} |
report() |
unicode:chardata())
primary_config() =
#{level => level() | all | none,
filter_default => log | stop,
filters => [{filter_id(), filter()}]}
Primary configuration data for Logger. The following default values apply:
report() = map() | [{atom(), term()}]
report_cb() =
fun((report()) -> {io:format(), [term()]}) |
fun((report(), report_cb_config()) -> unicode:chardata())
A fun which converts a report() to a format string and arguments, or directly to a string. See section Log Message in the User's Guide for more information.
report_cb_config() =
#{depth := integer() >= 1 | unlimited,
chars_limit := integer() >= 1 | unlimited,
single_line := boolean()}
timestamp() = integer()
A timestamp produced with erlang:system_time(microsecond).
The following macros are defined in logger.hrl, which is included in a module with the directive
-include_lib("kernel/include/logger.hrl").
All macros expand to a call to Logger, where Level is taken from the macro name, or from the first argument in the case of the ?LOG macro. Location data is added to the metadata as described under the metadata() type definition.
The call is wrapped in a case statement and will be evaluated only if Level is equal to or below the configured log level.
emergency(StringOrReport[,Metadata])
emergency(Format,Args[,Metadata])
emergency(Fun,FunArgs[,Metadata])
Equivalent to log(emergency,...).
alert(StringOrReport[,Metadata])
alert(Format,Args[,Metadata])
alert(Fun,FunArgs[,Metadata])
Equivalent to log(alert,...).
critical(StringOrReport[,Metadata])
critical(Format,Args[,Metadata])
critical(Fun,FunArgs[,Metadata])
Equivalent to log(critical,...).
error(StringOrReport[,Metadata])
error(Format,Args[,Metadata])
error(Fun,FunArgs[,Metadata])
Equivalent to log(error,...).
warning(StringOrReport[,Metadata])
warning(Format,Args[,Metadata])
warning(Fun,FunArgs[,Metadata])
Equivalent to log(warning,...).
notice(StringOrReport[,Metadata])
notice(Format,Args[,Metadata])
notice(Fun,FunArgs[,Metadata])
Equivalent to log(notice,...).
info(StringOrReport[,Metadata])
info(Format,Args[,Metadata])
info(Fun,FunArgs[,Metadata])
Equivalent to log(info,...).
debug(StringOrReport[,Metadata])
debug(Format,Args[,Metadata])
debug(Fun,FunArgs[,Metadata])
Equivalent to log(debug,...).
log(Level, StringOrReport) -> ok
log(Level, StringOrReport, Metadata) -> ok
log(Level, Format, Args) -> ok
log(Level, Fun, FunArgs) -> ok
log(Level, Format, Args, Metadata) -> ok
log(Level, Fun, FunArgs, Metadata) -> ok
Types:
Log the given message.
add_handler(HandlerId, Module, Config) -> ok | {error, term()}
Types:
Add a handler with the given configuration.
HandlerId is a unique identifier which must be used in all subsequent calls referring to this handler.
add_handler_filter(HandlerId, FilterId, Filter) ->
ok | {error, term()}
Types:
Add a filter to the specified handler.
The filter fun is called with the log event as the first parameter, and the specified filter_args() as the second parameter.
The return value of the fun specifies if a log event is to be discarded or forwarded to the handler callback:
See section Filters in the User's Guide for more information about filters.
Some built-in filters exist. These are defined in logger_filters.
add_handlers(Application) -> ok | {error, term()}
Types:
Reads the application configuration parameter logger and calls add_handlers/1 with its contents.
add_handlers(HandlerConfig) -> ok | {error, term()}
Types:
config_handler() =
{handler, handler_id(), module(), handler_config()}
This function should be used by custom Logger handlers to make configuration consistent no matter which handler the system uses. Normal usage is to add a call to logger:add_handlers/1 just after the processes that the handler needs are started, and pass the application's logger configuration as the argument. For example:
-behaviour(application). start(_, []) ->
case supervisor:start_link({local, my_sup}, my_sup, []) of
{ok, Pid} ->
ok = logger:add_handlers(my_app),
{ok, Pid, []};
Error -> Error
end.
This reads the logger configuration parameter from the my_app application and starts the configured handlers. The contents of the configuration use the same rules as the logger handler configuration.
If the handler is meant to replace the default handler, the Kernel's default handler have to be disabled before the new handler is added. A sys.config file that disables the Kernel handler and adds a custom handler could look like this:
[{kernel,
[{logger,
%% Disable the default Kernel handler
[{handler, default, undefined}]}]},
{my_app,
[{logger,
%% Enable this handler as the default
[{handler, default, my_handler, #{}}]}]}].
add_primary_filter(FilterId, Filter) -> ok | {error, term()}
Types:
Add a primary filter to Logger.
The filter fun is called with the log event as the first parameter, and the specified filter_args() as the second parameter.
The return value of the fun specifies if a log event is to be discarded or forwarded to the handlers:
See section Filters in the User's Guide for more information about filters.
Some built-in filters exist. These are defined in logger_filters.
get_config() ->
#{primary => primary_config(),
handlers => [handler_config()],
module_levels =>
[{module(), level() | all | none}]}
Look up all current Logger configuration, including primary and handler configuration, and module level settings.
get_handler_config() -> [Config]
Types:
Look up the current configuration for all handlers.
get_handler_config(HandlerId) -> {ok, Config} | {error, term()}
Types:
Look up the current configuration for the given handler.
get_handler_ids() -> [HandlerId]
Types:
Look up the identities for all installed handlers.
get_primary_config() -> Config
Types:
Look up the current primary configuration for Logger.
get_module_level() -> [{Module, Level}]
Types:
Look up all current module levels. Returns a list containing one {Module,Level} element for each module for which the module level was previously set with set_module_level/2.
get_module_level(Modules) -> [{Module, Level}]
Types:
Look up the current level for the given modules. Returns a list containing one {Module,Level} element for each of the given modules for which the module level was previously set with set_module_level/2.
get_process_metadata() -> Meta | undefined
Types:
Retrieve data set with set_process_metadata/1 or update_process_metadata/1.
remove_handler(HandlerId) -> ok | {error, term()}
Types:
Remove the handler identified by HandlerId.
remove_handler_filter(HandlerId, FilterId) -> ok | {error, term()}
Types:
Remove the filter identified by FilterId from the handler identified by HandlerId.
remove_primary_filter(FilterId) -> ok | {error, term()}
Types:
Remove the primary filter identified by FilterId from Logger.
set_application_level(Application, Level) ->
ok | {error, not_loaded}
Types:
Set the log level for all the modules of the specified application.
This function is a convenience function that calls logger:set_module_level/2 for each module associated with an application.
set_handler_config(HandlerId, Config) -> ok | {error, term()}
Types:
Set configuration data for the specified handler. This overwrites the current handler configuration.
To modify the existing configuration, use update_handler_config/2, or, if a more complex merge is needed, read the current configuration with get_handler_config/1, then do the merge before writing the new configuration back with this function.
If a key is removed compared to the current configuration, and the key is known by Logger, the default value is used. If it is a custom key, then it is up to the handler implementation if the value is removed or a default value is inserted.
set_handler_config(HandlerId, Key :: level, Level) -> Return
set_handler_config(HandlerId,
Key :: filter_default,
FilterDefault) ->
Return
set_handler_config(HandlerId, Key :: filters, Filters) -> Return
set_handler_config(HandlerId, Key :: formatter, Formatter) ->
Return
set_handler_config(HandlerId, Key :: config, Config) -> Return
Types:
Add or update configuration data for the specified handler. If the given Key already exists, its associated value will be changed to the given value. If it does not exist, it will be added.
If the value is incomplete, which for example can be the case for the config key, it is up to the handler implementation how the unspecified parts are set. For all handlers in the Kernel application, unspecified data for the config key is set to default values. To update only specified data, and keep the existing configuration for the rest, use update_handler_config/3.
See the definition of the handler_config() type for more information about the different parameters.
set_primary_config(Config) -> ok | {error, term()}
Types:
Set primary configuration data for Logger. This overwrites the current configuration.
To modify the existing configuration, use update_primary_config/1, or, if a more complex merge is needed, read the current configuration with get_primary_config/0, then do the merge before writing the new configuration back with this function.
If a key is removed compared to the current configuration, the default value is used.
set_primary_config(Key :: level, Level) -> ok | {error, term()}
set_primary_config(Key :: filter_default, FilterDefault) ->
ok | {error, term()}
set_primary_config(Key :: filters, Filters) ->
ok | {error, term()}
Types:
Add or update primary configuration data for Logger. If the given Key already exists, its associated value will be changed to the given value. If it does not exist, it will be added.
set_module_level(Modules, Level) -> ok | {error, term()}
Types:
Set the log level for the specified modules.
The log level for a module overrides the primary log level of Logger for log events originating from the module in question. Notice, however, that it does not override the level configuration for any handler.
For example: Assume that the primary log level for Logger is info, and there is one handler, h1, with level info and one handler, h2, with level debug.
With this configuration, no debug messages will be logged, since they are all stopped by the primary log level.
If the level for mymodule is now set to debug, then debug events from this module will be logged by the handler h2, but not by handler h1.
Debug events from other modules are still not logged.
To change the primary log level for Logger, use set_primary_config(level, Level).
To change the log level for a handler, use set_handler_config(HandlerId, level, Level).
set_process_metadata(Meta) -> ok
Types:
Set metadata which Logger shall automatically insert in all log events produced on the current process.
Location data produced by the log macros, and/or metadata given as argument to the log call (API function or macro), are merged with the process metadata. If the same keys occur, values from the metadata argument to the log call overwrite values from the process metadata, which in turn overwrite values from the location data.
Subsequent calls to this function overwrites previous data set. To update existing data instead of overwriting it, see update_process_metadata/1.
unset_application_level(Application) -> ok | {error, not_loaded}
Types:
Unset the log level for all the modules of the specified application.
This function is a convinience function that calls logger:unset_module_level/2 for each module associated with an application.
unset_module_level() -> ok
Remove module specific log settings. After this, the primary log level is used for all modules.
unset_module_level(Modules) -> ok
Types:
Remove module specific log settings. After this, the primary log level is used for the specified modules.
unset_process_metadata() -> ok
Delete data set with set_process_metadata/1 or update_process_metadata/1.
update_formatter_config(HandlerId, FormatterConfig) ->
ok | {error, term()}
Types:
Update the formatter configuration for the specified handler.
The new configuration is merged with the existing formatter configuration.
To overwrite the existing configuration without any merge, use
set_handler_config(HandlerId, formatter, {FormatterModule, FormatterConfig}).
update_formatter_config(HandlerId, Key, Value) ->
ok | {error, term()}
Types:
Update the formatter configuration for the specified handler.
This is equivalent to
update_formatter_config(HandlerId, #{Key => Value})
update_handler_config(HandlerId, Config) -> ok | {error, term()}
Types:
Update configuration data for the specified handler. This function behaves as if it was implemented as follows:
{ok, {_, Old}} = logger:get_handler_config(HandlerId), logger:set_handler_config(HandlerId, maps:merge(Old, Config)).
To overwrite the existing configuration without any merge, use set_handler_config/2.
update_handler_config(HandlerId, Key :: level, Level) -> Return
update_handler_config(HandlerId,
Key :: filter_default,
FilterDefault) ->
Return
update_handler_config(HandlerId, Key :: filters, Filters) ->
Return
update_handler_config(HandlerId, Key :: formatter, Formatter) ->
Return
update_handler_config(HandlerId, Key :: config, Config) -> Return
Types:
Add or update configuration data for the specified handler. If the given Key already exists, its associated value will be changed to the given value. If it does not exist, it will be added.
If the value is incomplete, which for example can be the case for the config key, it is up to the handler implementation how the unspecified parts are set. For all handlers in the Kernel application, unspecified data for the config key is not changed. To reset unspecified data to default values, use set_handler_config/3.
See the definition of the handler_config() type for more information about the different parameters.
update_primary_config(Config) -> ok | {error, term()}
Types:
Update primary configuration data for Logger. This function behaves as if it was implemented as follows:
Old = logger:get_primary_config(), logger:set_primary_config(maps:merge(Old, Config)).
To overwrite the existing configuration without any merge, use set_primary_config/1.
update_process_metadata(Meta) -> ok
Types:
Set or update metadata to use when logging from current process
If process metadata exists for the current process, this function behaves as if it was implemented as follows:
logger:set_process_metadata(maps:merge(logger:get_process_metadata(), Meta)).
If no process metadata exists, the function behaves as set_process_metadata/1.
compare_levels(Level1, Level2) -> eq | gt | lt
Types:
Compare the severity of two log levels. Returns gt if Level1 is more severe than Level2, lt if Level1 is less severe, and eq if the levels are equal.
format_report(Report) -> FormatArgs
Types:
Convert a log message on report form to {Format, Args}. This is the default report callback used by logger_formatter when no custom report callback is found. See section Log Message in the Kernel User's Guide for information about report callbacks and valid forms of log messages.
The function produces lines of Key: Value from key-value lists. Strings are printed with ~ts and other terms with ~tp.
If Report is a map, it is converted to a key-value list before formatting as such.
The following functions are to be exported from a handler callback module.
HModule:adding_handler(Config1) -> {ok, Config2} | {error,
Reason}
Types:
This callback function is optional.
The function is called on a temporary process when an new handler is about to be added. The purpose is to verify the configuration and initiate all resources needed by the handler.
The handler identity is associated with the id key in Config1.
If everything succeeds, the callback function can add possible default values or internal state values to the configuration, and return the adjusted map in {ok,Config2}.
If the configuration is faulty, or if the initiation fails, the callback function must return {error,Reason}.
HModule:changing_config(SetOrUpdate, OldConfig, NewConfig)
-> {ok, Config} | {error, Reason}
Types:
This callback function is optional.
The function is called on a temporary process when the configuration for a handler is about to change. The purpose is to verify and act on the new configuration.
OldConfig is the existing configuration and NewConfig is the new configuration.
The handler identity is associated with the id key in OldConfig.
SetOrUpdate has the value set if the configuration change originates from a call to set_handler_config/2,3, and update if it originates from update_handler_config/2,3. The handler can use this parameteter to decide how to update the value of the config field, that is, the handler specific configuration data. Typically, if SetOrUpdate equals set, values that are not specified must be given their default values. If SetOrUpdate equals update, the values found in OldConfig must be used instead.
If everything succeeds, the callback function must return a possibly adjusted configuration in {ok,Config}.
If the configuration is faulty, the callback function must return {error,Reason}.
HModule:filter_config(Config) -> FilteredConfig
Types:
This callback function is optional.
The function is called when one of the Logger API functions for fetching the handler configuration is called, for example logger:get_handler_config/1.
It allows the handler to remove internal data fields from its configuration data before it is returned to the caller.
HModule:log(LogEvent, Config) -> void()
Types:
This callback function is mandatory.
The function is called when all primary filters and all handler filters for the handler in question have passed for the given log event. It is called on the client process, that is, the process that issued the log event.
The handler identity is associated with the id key in Config.
The handler must log the event.
The return value from this function is ignored by Logger.
HModule:removing_handler(Config) -> ok
Types:
This callback function is optional.
The function is called on a temporary process when a handler is about to be removed. The purpose is to release all resources used by the handler.
The handler identity is associated with the id key in Config.
The return value is ignored by Logger.
The following functions are to be exported from a formatter callback module.
FModule:check_config(FConfig) -> ok | {error, Reason}
Types:
This callback function is optional.
The function is called by a Logger when formatter configuration is set or modified. The formatter must validate the given configuration and return ok if it is correct, and {error,Reason} if it is faulty.
The following Logger API functions can trigger this callback:
See logger_formatter(3erl) for an example implementation. logger_formatter is the default formatter used by Logger.
FModule:format(LogEvent, FConfig) -> FormattedLogEntry
Types:
This callback function is mandatory.
The function can be called by a log handler to convert a log event term to a printable string. The returned value can, for example, be printed as a log entry to the console or a file using io:put_chars/1,2.
See logger_formatter(3erl) for an example implementation. logger_formatter is the default formatter used by Logger.
config(5), erlang(3erl), io(3erl), logger_disk_log_h(3erl), logger_filters(3erl), logger_formatter(3erl), logger_std_h(3erl), unicode(3erl)
kernel 6.2 | Ericsson AB |