Erlang -- logger

Kernel

Reference Manual

Version 6.0

- User's Guide
- Reference Manual
- Release Notes
- PDF
- Top

- Expand All
- Contract All

### Table of Contents

- kernel (App)
- app
- application
  - Top of manual page
  - ensure_all_started/1
  - ensure_all_started/2
  - ensure_started/1
  - ensure_started/2
  - get_all_env/0
  - get_all_env/1
  - get_all_key/0
  - get_all_key/1
  - get_application/0
  - get_application/1
  - get_env/1
  - get_env/2
  - get_env/3
  - get_key/1
  - get_key/2
  - load/1
  - load/2
  - loaded_applications/0
  - permit/2
  - set_env/3
  - set_env/4
  - start/1
  - start/2
  - start_type/0
  - stop/1
  - takeover/2
  - unload/1
  - unset_env/2
  - unset_env/3
  - which_applications/0
  - which_applications/1
  - Module:config_change/3
  - Module:prep_stop/1
  - Module:start/2
  - Module:start_phase/3
  - Module:stop/1
- auth
  - Top of manual page
  - cookie/0
  - cookie/1
  - is_auth/1
  - node_cookie/1
  - node_cookie/2
- code
  - Top of manual page
  - add_path/1
  - add_patha/1
  - add_paths/1
  - add_pathsa/1
  - add_pathsz/1
  - add_pathz/1
  - all_loaded/0
  - atomic_load/1
  - clash/0
  - compiler_dir/0
  - del_path/1
  - delete/1
  - ensure_loaded/1
  - ensure_modules_loaded/1
  - finish_loading/1
  - get_mode/0
  - get_object_code/1
  - get_path/0
  - is_loaded/1
  - is_module_native/1
  - is_sticky/1
  - lib_dir/0
  - lib_dir/1
  - lib_dir/2
  - load_abs/1
  - load_binary/3
  - load_file/1
  - modified_modules/0
  - module_status/1
  - objfile_extension/0
  - prepare_loading/1
  - priv_dir/1
  - purge/1
  - replace_path/2
  - root_dir/0
  - set_path/1
  - soft_purge/1
  - stick_dir/1
  - unstick_dir/1
  - where_is_file/1
  - which/1
- config
- disk_log
  - Top of manual page
  - accessible_logs/0
  - alog/2
  - alog_terms/2
  - balog/2
  - balog_terms/2
  - bchunk/2
  - bchunk/3
  - block/1
  - block/2
  - blog/2
  - blog_terms/2
  - breopen/3
  - btruncate/2
  - change_header/2
  - change_notify/3
  - change_size/2
  - chunk/2
  - chunk/3
  - chunk_info/1
  - chunk_step/3
  - close/1
  - format_error/1
  - inc_wrap_file/1
  - info/1
  - lclose/1
  - lclose/2
  - log/2
  - log_terms/2
  - open/1
  - pid2name/1
  - reopen/2
  - reopen/3
  - sync/1
  - truncate/1
  - truncate/2
  - unblock/1
- erl_boot_server
  - Top of manual page
  - add_slave/1
  - delete_slave/1
  - start/1
  - start_link/1
  - which_slaves/0
- erl_ddll
  - Top of manual page
  - demonitor/1
  - format_error/1
  - info/0
  - info/1
  - info/2
  - load/2
  - load_driver/2
  - loaded_drivers/0
  - monitor/2
  - reload/2
  - reload_driver/2
  - try_load/3
  - try_unload/2
  - unload/1
  - unload_driver/1
- erl_epmd
  - Top of manual page
  - address_please/3
  - names/1
  - port_please/2
  - port_please/3
  - register_node/2
  - register_node/3
  - start_link/0
- erl_prim_loader
- erlang
- error_handler
  - Top of manual page
  - raise_undef_exception/3
  - undefined_function/3
  - undefined_lambda/3
- error_logger
  - Top of manual page
  - add_report_handler/1
  - add_report_handler/2
  - delete_report_handler/1
  - error_msg/1
  - error_msg/2
  - error_report/1
  - error_report/2
  - format/2
  - get_format_depth/0
  - info_msg/1
  - info_msg/2
  - info_report/1
  - info_report/2
  - logfile/1
  - tty/1
  - warning_map/0
  - warning_msg/1
  - warning_msg/2
  - warning_report/1
  - warning_report/2
- file
  - Top of manual page
  - advise/4
  - allocate/3
  - change_group/2
  - change_mode/2
  - change_owner/2
  - change_owner/3
  - change_time/2
  - change_time/3
  - close/1
  - consult/1
  - copy/2
  - copy/3
  - datasync/1
  - del_dir/1
  - delete/1
  - eval/1
  - eval/2
  - format_error/1
  - get_cwd/0
  - get_cwd/1
  - list_dir/1
  - list_dir_all/1
  - make_dir/1
  - make_link/2
  - make_symlink/2
  - native_name_encoding/0
  - open/2
  - path_consult/2
  - path_eval/2
  - path_open/3
  - path_script/2
  - path_script/3
  - pid2name/1
  - position/2
  - pread/2
  - pread/3
  - pwrite/2
  - pwrite/3
  - read/2
  - read_file/1
  - read_file_info/1
  - read_file_info/2
  - read_line/1
  - read_link/1
  - read_link_all/1
  - read_link_info/1
  - read_link_info/2
  - rename/2
  - script/1
  - script/2
  - sendfile/2
  - sendfile/5
  - set_cwd/1
  - sync/1
  - truncate/1
  - write/2
  - write_file/2
  - write_file/3
  - write_file_info/2
  - write_file_info/3
- gen_sctp
  - Top of manual page
  - abort/2
  - close/1
  - connect/4
  - connect/5
  - connect_init/4
  - connect_init/5
  - controlling_process/2
  - eof/2
  - error_string/1
  - listen/2
  - open/0
  - open/1
  - open/2
  - peeloff/2
  - recv/1
  - recv/2
  - send/3
  - send/4
- gen_tcp
  - Top of manual page
  - accept/1
  - accept/2
  - close/1
  - connect/3
  - connect/4
  - controlling_process/2
  - listen/2
  - recv/2
  - recv/3
  - send/2
  - shutdown/2
- gen_udp
  - Top of manual page
  - close/1
  - controlling_process/2
  - open/1
  - open/2
  - recv/2
  - recv/3
  - send/4
- global
  - Top of manual page
  - del_lock/1
  - del_lock/2
  - notify_all_name/3
  - random_exit_name/3
  - random_notify_name/3
  - re_register_name/2
  - re_register_name/3
  - register_name/2
  - register_name/3
  - registered_names/0
  - send/2
  - set_lock/1
  - set_lock/2
  - set_lock/3
  - sync/0
  - trans/2
  - trans/3
  - trans/4
  - unregister_name/1
  - whereis_name/1
- global_group
  - Top of manual page
  - global_groups/0
  - info/0
  - monitor_nodes/1
  - own_nodes/0
  - registered_names/1
  - send/2
  - send/3
  - sync/0
  - whereis_name/1
  - whereis_name/2
- heart
  - Top of manual page
  - clear_callback/0
  - clear_cmd/0
  - get_callback/0
  - get_cmd/0
  - get_options/0
  - set_callback/2
  - set_cmd/1
  - set_options/1
- inet
  - Top of manual page
  - close/1
  - format_error/1
  - get_rc/0
  - getaddr/2
  - getaddrs/2
  - gethostbyaddr/1
  - gethostbyname/1
  - gethostbyname/2
  - gethostname/0
  - getifaddrs/0
  - getopts/2
  - getstat/1
  - getstat/2
  - i/0
  - i/1
  - i/2
  - ipv4_mapped_ipv6_address/1
  - ntoa/1
  - parse_address/1
  - parse_ipv4_address/1
  - parse_ipv4strict_address/1
  - parse_ipv6_address/1
  - parse_ipv6strict_address/1
  - parse_strict_address/1
  - peername/1
  - peernames/1
  - peernames/2
  - port/1
  - setopts/2
  - sockname/1
  - socknames/1
  - socknames/2
- inet_res
  - Top of manual page
  - getbyname/2
  - getbyname/3
  - gethostbyaddr/1
  - gethostbyaddr/2
  - gethostbyname/1
  - gethostbyname/2
  - gethostbyname/3
  - lookup/3
  - lookup/4
  - lookup/5
  - nnslookup/4
  - nnslookup/5
  - nslookup/3
  - nslookup/4
  - resolve/3
  - resolve/4
  - resolve/5
- init
- logger
  - Top of manual page
  - add_handler/3
  - add_handler_filter/3
  - add_handlers/1
  - add_primary_filter/2
  - alert/1
  - alert/2
  - alert/2
  - compare_levels/2
  - critical/1
  - critical/2
  - critical/2
  - debug/1
  - debug/2
  - debug/2
  - emergency/1
  - emergency/2
  - emergency/2
  - error/1
  - error/2
  - error/2
  - format_report/1
  - get_config/0
  - get_handler_config/0
  - get_handler_config/1
  - get_handler_ids/0
  - get_module_level/0
  - get_module_level/1
  - get_primary_config/0
  - get_process_metadata/0
  - info/1
  - info/2
  - info/2
  - log/2
  - log/3
  - log/4
  - notice/1
  - notice/2
  - notice/2
  - remove_handler/1
  - remove_handler_filter/2
  - remove_primary_filter/1
  - set_handler_config/2
  - set_handler_config/3
  - set_module_level/2
  - set_primary_config/1
  - set_primary_config/2
  - set_process_metadata/1
  - unset_module_level/0
  - unset_module_level/1
  - unset_process_metadata/0
  - update_formatter_config/2
  - update_formatter_config/3
  - update_handler_config/2
  - update_primary_config/1
  - update_process_metadata/1
  - warning/1
  - warning/2
  - warning/2
  - FModule:check_config/1
  - FModule:format/2
  - HModule:adding_handler/1
  - HModule:changing_config/2
  - HModule:log/2
  - HModule:removing_handler/1
- logger_filters
  - Top of manual page
  - domain/2
  - level/2
  - progress/2
  - remote_gl/2
- logger_formatter
  - Top of manual page
  - check_config/1
  - format/2
- logger_std_h
  - Top of manual page
  - filesync/1
- logger_disk_log_h
  - Top of manual page
  - filesync/1
- net_adm
  - Top of manual page
  - dns_hostname/1
  - host_file/0
  - localhost/0
  - names/0
  - names/1
  - ping/1
  - world/0
  - world/1
  - world_list/1
  - world_list/2
- net_kernel
  - Top of manual page
  - allow/1
  - connect_node/1
  - get_net_ticktime/0
  - getopts/2
  - monitor_nodes/1
  - monitor_nodes/2
  - set_net_ticktime/1
  - set_net_ticktime/2
  - setopts/2
  - start/1
  - start/1
  - start/1
  - stop/0
- os
  - Top of manual page
  - cmd/1
  - cmd/2
  - find_executable/1
  - find_executable/2
  - getenv/0
  - getenv/1
  - getenv/2
  - getpid/0
  - perf_counter/0
  - perf_counter/1
  - putenv/2
  - set_signal/2
  - system_time/0
  - system_time/1
  - timestamp/0
  - type/0
  - unsetenv/1
  - version/0
- pg2
  - Top of manual page
  - create/1
  - delete/1
  - get_closest_pid/1
  - get_local_members/1
  - get_members/1
  - join/2
  - leave/2
  - start/0
  - start_link/0
  - which_groups/0
- rpc
  - Top of manual page
  - abcast/2
  - abcast/3
  - async_call/4
  - block_call/4
  - block_call/5
  - call/4
  - call/5
  - cast/4
  - eval_everywhere/3
  - eval_everywhere/4
  - multi_server_call/2
  - multi_server_call/3
  - multicall/3
  - multicall/4
  - multicall/5
  - nb_yield/1
  - nb_yield/2
  - parallel_eval/1
  - pinfo/1
  - pinfo/2
  - pmap/3
  - sbcast/2
  - sbcast/3
  - server_call/4
  - yield/1
- seq_trace
  - Top of manual page
  - get_system_tracer/0
  - get_token/0
  - get_token/1
  - print/1
  - print/2
  - reset_trace/0
  - set_system_tracer/1
  - set_token/1
  - set_token/2
- user
- wrap_log_reader
  - Top of manual page
  - chunk/1
  - chunk/2
  - close/1
  - open/1
  - open/2
- zlib

# logger

### Module

logger

### Module Summary

API module for Logger, the standard logging facility in Erlang/OTP.

### Description

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:

- the Logger facility in general, see the User's Guide.
- how to configure Logger, see the Configuration section in the User's Guide.
- the built-in handlers, see logger_std_h and logger_disk_log_h.
- the built-in formatter, see logger_formatter.
- built-in filters, see logger_filters.

Note

Since Logger is new in Erlang/OTP 21.0, we do reserve the right to introduce changes to the Logger API and functionality in patches following this release. These changes might or might not be backwards compatible with the initial version.

### Data Types

primary_config() =
#{level => level() | all | none,
filter_default => log | stop,
filters => [{filter_id(), filter()}]}

Primary configuration data for Logger. The following default values apply:

- level => info
- filter_default => log
- filters => []

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:

- level => all
- filter_default => log
- filters => []
- formatter => {logger_formatter, DefaultFormatterConfig}

In addition to these, the following fields are automatically inserted by Logger, values taken from the two first parameters to add_handler/3:

- id => HandlerId
- module => Module

Handler specific configuration data is inserted by the handler callback itself, in a sub structure associated with the field named config.

See the logger_formatter(3) manual page for information about the default configuration for this formatter.

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(3) for an example of a formatter implementation.

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 => fun((report()) -> {io:format(), [term()]}),
atom() => term()}

Metadata for the log event.

Logger adds the following metadata to each log event:

- pid => self()
- gl => group_leader()
- time => erlang:system_time(microsecond)

When a log macro is used, Logger also inserts location information:

- mfa => {?MODULE, ?FUNCTION_NAME, ?FUNCTION_ARITY}
- file => ?FILE
- line => ?LINE

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:

domain

  The value associated with this key is used by filters for grouping log events originating from, for example, specific functional areas. See logger_filters:domain/2 for a description of how this field can be used.

report_cb

  If the log message is specified as a report(), the report_cb key can be associated with a fun (report callback) that converts the report to a format string and arguments. See section Log Message in the User's Guide for more information about report callbacks.

msg_fun() =
fun((term()) ->
{io:format(), [term()]} |
report() |
unicode:chardata())

report() = map() | [{atom(), term()}]

timestamp() = integer()

A timestamp produced with erlang:system_time(microsecond).

### Macros

The following macros are defined:

- ?LOG_EMERGENCY(StringOrReport[,Metadata])
- ?LOG_EMERGENCY(FunOrFormat,Args[,Metadata])
- ?LOG_ALERT(StringOrReport[,Metadata])
- ?LOG_ALERT(FunOrFormat,Args[,Metadata])
- ?LOG_CRITICAL(StringOrReport[,Metadata])
- ?LOG_CRITICAL(FunOrFormat,Args[,Metadata])
- ?LOG_ERROR(StringOrReport[,Metadata])
- ?LOG_ERROR(FunOrFormat,Args[,Metadata])
- ?LOG_WARNING(StringOrReport[,Metadata])
- ?LOG_WARNING(FunOrFormat,Args[,Metadata])
- ?LOG_NOTICE(StringOrReport[,Metadata])
- ?LOG_NOTICE(FunOrFormat,Args[,Metadata])
- ?LOG_INFO(StringOrReport[,Metadata])
- ?LOG_INFO(FunOrFormat,Args[,Metadata])
- ?LOG_DEBUG(StringOrReport[,Metadata])
- ?LOG_DEBUG(FunOrFormat,Args[,Metadata])

All macros expand to a call to Logger, where Level is taken from the macro name, and location data is added to the metadata. See the description of the metadata() type for more information about the location data.

The call is wrapped in a case statement and will be evaluated only if Level is equal to or below the configured log level.

### Logging API functions

### Exports

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

Level = level()
StringOrReport = unicode:chardata() | report()
Format = io:format()
Args = [term()]
Fun = msg_fun()
FunArgs = term()
Metadata = metadata()

Log the given message.

### Configuration API functions

### Exports

add_handler(HandlerId, Module, Config) -> ok | {error, term()}

### Types

HandlerId = handler_id()
Module = module()
Config = handler_config()

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

HandlerId = handler_id()
FilterId = filter_id()
Filter = filter()

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:

log_event()

  The filter passed. The next handler filter, if any, is applied. If no more filters exist for this handler, the log event is forwarded to the handler callback.

stop

  The filter did not pass, and the log event is immediately discarded.

ignore

  The filter has no knowledge of the log event. The next handler filter, if any, is applied. If no more filters exist for this handler, the value of the filter_default configuration parameter for the handler specifies if the log event shall 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

Application = atom()

Reads the application configuration parameter logger and calls add_handlers/1 with its contents.

add_handlers(HandlerConfig) -> ok | {error, term()}

### Types

HandlerConfig = [config_handler()]
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_all 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

FilterId = filter_id()
Filter = filter()

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:

log_event()

  The filter passed. The next primary filter, if any, is applied. If no more primary filters exist, the log event is forwarded to the handler part of Logger, where handler filters are applied.

stop

  The filter did not pass, and the log event is immediately discarded.

ignore

  The filter has no knowledge of the log event. The next primary filter, if any, is applied. If no more primary filters exist, the value of the primary filter_default configuration parameter specifies if the log event shall be discarded or forwarded to the handler part.

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

Config = handler_config()

Look up the current configuration for all handlers.

get_handler_config(HandlerId) -> {ok, Config} | {error, term()}

### Types

HandlerId = handler_id()
Config = handler_config()

Look up the current configuration for the given handler.

get_handler_ids() -> [HandlerId]

### Types

HandlerId = handler_id()

Look up the identities for all installed handlers.

get_primary_config() -> Config

### Types

Config = primary_config()

Look up the current primary configuration for Logger.

get_module_level() -> [{Module, Level}]

### Types

Module = module()
Level = level() | all | none

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

Modules = [Module] | Module
Module = module()
Level = level() | all | none

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

Meta = metadata()

Retrieve data set with set_process_metadata/1 or update_process_metadata/1.

remove_handler(HandlerId) -> ok | {error, term()}

### Types

HandlerId = handler_id()

Remove the handler identified by HandlerId.

remove_handler_filter(HandlerId, FilterId) -> ok | {error, term()}

### Types

HandlerId = handler_id()
FilterId = filter_id()

Remove the filter identified by FilterId from the handler identified by HandlerId.

remove_primary_filter(FilterId) -> ok | {error, term()}

### Types

FilterId = filter_id()

Remove the primary filter identified by FilterId from Logger.

set_handler_config(HandlerId, Config) -> ok | {error, term()}

### Types

HandlerId = handler_id()
Config = handler_config()

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, Value) -> ok | {error, term()}

### Types

HandlerId = handler_id()
Key = atom()
Value = term()

Add or update configuration data for the specified handler. If the given Key already exists, its associated value will be changed to Value. If it does not exist, it will be added.

set_primary_config(Config) -> ok | {error, term()}

### Types

Config = primary_config()

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, Value) -> ok | {error, term()}

### Types

Key = atom()
Value = term()

Add or update primary configuration data for Logger. If the given Key already exists, its associated value will be changed to Value. If it does not exist, it will be added.

set_module_level(Modules, Level) -> ok | {error, term()}

### Types

Modules = [module()] | module()
Level = level() | all | none

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).

Note

The originating module for a log event is only detected if the key mfa exists in the metadata, and is associated with {Module, Function, Arity}. When log macros are used, this association is automatically added to all log events. If an API function is called directly, without using a macro, the logging client must explicitly add this information if module levels shall have any effect.

set_process_metadata(Meta) -> ok

### Types

Meta = metadata()

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_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

Modules = [module()] | module()

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

HandlerId = handler_id()
FormatterConfig = formatter_config()

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

HandlerId = handler_id()
Key = atom()
Value = term()

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

HandlerId = handler_id()
Config = handler_config()

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_primary_config(Config) -> ok | {error, term()}

### Types

Config = primary_config()

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

Meta = metadata()

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.

### Miscellaneous API functions

### Exports

compare_levels(Level1, Level2) -> eq | gt | lt

### Types

Level1 = Level2 = level()

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

Report = report()
FormatArgs = {io:format(), [term()]}

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.

### Handler Callback Functions

The following functions are to be exported from a handler callback module.

### Exports

HModule:adding_handler(Config1) -> {ok, Config2} | {error, Reason}

### Types

Config1 = Config2 = handler_config()
Reason = term()

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(Config1, Config2) -> {ok, Config3} | {error, Reason}

### Types

Config1 = Config2 = Config3 = handler_config()
Reason = term()

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.

Config1 is the existing configuration and Config2 is the new configuration.

The handler identity is associated with the id key in Config1.

If everything succeeds, the callback function must return a possibly adjusted configuration in {ok,Config3}.

If the configuration is faulty, the callback function must return {error,Reason}.

HModule:log(LogEvent, Config) -> void()

### Types

LogEvent = log_event()
Config = handler_config()

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

Config = handler_config()

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.

### Formatter Callback Functions

The following functions are to be exported from a formatter callback module.

### Exports

FModule:check_config(FConfig) -> ok | {error, Reason}

### Types

FConfig = formatter_config()
Reason = term()

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:

- logger:add_handler/3
- logger:set_handler_config/2,3
- logger:updata_handler_config/2
- logger:update_formatter_config/2

See logger_formatter(3) for an example implementation. logger_formatter is the default formatter used by Logger.

FModule:format(LogEvent, FConfig) -> FormattedLogEntry

### Types

LogEvent = log_event()
FConfig = formatter_config()
FormattedLogEntry = unicode:chardata()

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(3) for an example implementation. logger_formatter is the default formatter used by Logger.

### See Also

config(4), erlang(3), io(3), logger_disk_log_h(3), logger_filters(3), logger_formatter(3), logger_std_h(3), unicode(3)