From e484181ecc5c3e2928d10632138837eba3c3229e Mon Sep 17 00:00:00 2001
From: Siri Hansen
This module is the main API for logging in Erlang/OTP. It
contains functions that allow applications to use a single log
@@ -217,6 +190,15 @@ logger:error("error happened because: ~p",[Reason]). %% Without macro
Configuration data for the
+ formatter. See
See section
See section
Some built-in filters exist. These are defined
in
See
- section
Some built-in filters exist. These are defined in
The following functions are to be exported from a handler callback module.
This callback function is optional.
The function is called when an new handler is about to be added, and the purpose is to verify the configuration and - initiate all resourced needed by the handler.
+ initiate all resources needed by the handler.If everything succeeds, the callback function can add possible default values or internal state values to the configuration, and return the adjusted map @@ -922,24 +905,7 @@ logger:set_process_metadata(maps:merge(logger:get_process_metadata(),Meta)).
This callback function is optional.
-The function is called when a handler is about to be - removed, and the purpose is to release all resources used by - the handler. The return value is ignored by Logger.
-This callback function is mandatory.
+The function is called when all global filters and all + handler filters for the handler in question have passed for + the given log event.
+The handler must log the event.
+The return value from this function is ignored by + Logger.
+This callback function is optional.
+The function is called when a handler is about to be + removed, and the purpose is to release all resources used by + the handler. The return value is ignored by Logger.
+The following functions are to be exported from a formatter + callback module.
+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
See
As of OTP-21, Erlang/OTP provides a standard API for logging
+ through
By default, the Kernel application installs one log handler at
+ system start. This handler is named
You can also configure the system so that the default handler
+ prints log events to a single file, or to a set of wrap logs
+ via
By confiugration, you can aslo modify or disable the default + handler, replace it by a custom handler, and install additional + handlers.
+Erlang/OTP provides a standard API for logging. The backend of - this API can be used as is, or it can be customized to suite - specific needs.
-It consists of two parts - the logger part and the - handler part. The logger part forwards log events to - one or more handler(s).
+A log event consists of a log level, the + message to be logged, and metadata.
+The Logger backend forwards log events from the API, first + through a set of global filters, then through a set + of handler filters for each log handler.
+Each filter set consists of a log level check, + followed by zero or more filter functions.
+The following figure show a conseptual overview of Logger. The + figure shows two log handlers, but any number of handlers can be + installed.
Filters can be added to the logger part and to each - handler. The filters decide if an event is to be forwarded or - not, and they can also modify all parts of the log event.
- -A formatter can be set for each handler. The formatter - does the final formatting of the log event, including the log - message itself, and possibly a timestamp, header and other - metadata.
- -In accordance with the Syslog protocol, RFC-5424, eight - severity levels can be specified:
+In accordance with the Syslog protocol, RFC-5424, eight log + levels can be specified:
A log event is allowed by Logger if the integer value of
- its
A handler is defined as a module exporting the following - function:
- -log(Log, Config) -> ok
-
- The handler callback is called after filtering on logger - level and on handler level for the handler in - question. The function call is done on the client process, - and it is up to the handler implementation if other - processes are to be involved or not.
- -Multiple instances of the same handler can be - added. Configuration is per instance.
- -Filters can be set on the logger part, or on a handler. Logger - filters are applied first, and if passed, the handler filters - for each handler are applied. The handler callback is only - called if all handler filters for the handler in question also - pass.
- -A filter is specified as:
- -{fun((Log,Extra) -> Log | stop | ignore), Extra}
-
- The configuration parameter
The
A formatter is defined as a module exporting the following - function:
- -format(Log,FConfig) -> unicode:chardata()
-
- The formatter callback is called by each handler, and the - returned string can be printed to the handler's destination - (stdout, file, ...).
-This is the default handler used by OTP. Multiple instances - can be started, and each instance will write log events to a - given destination, console or file. Filters can be used for - selecting which event to send to which handler instance.
-This handler behaves much like logger_std_h, except it uses
-
This handler is to be used for backwards compatibility
- only. It is not started by default, but will be automatically
- started the first time an event handler is added
- with
No built-in event handlers exist.
-This filter provides a way of filtering log events based on a
-
A log event passes the level check if the integer value of its + log level is less than or equal to the currently configured log + level, that is, if the event is equally or more severe than the + configured level.
+The global log level can be overridden by a log level + configured per module. This is to, for instance, allow more + verbose logging from a specific part of the system.
+Filter functions can be used for more sophisticated filtering
+ than the log level check provides. A filter function can stop or
+ pass a log event, based on any of the event's contents. It can
+ also modify all parts of the log event. See see
+ section
If a log event passes through all global filters and all
+ handler filters for a specific handler, Logger forwards the event
+ to the handler callback. The handler formats and prints the
+ event to its destination. See
+ section
Everything upto and including the call to the handler callbacks + is executed on the client process, that is, the process where + the log event was issued. It is up to the handler implementation + if other processes are to be involved or not.
+The handlers are called in sequence, and the order is not + defined.
+Filters can be global, or attached to a specific + handler. Logger calls the global filters first, and if they all + pass, it calls the handler filters for each handler. Logger + calls the handler callback only if all filters attached to the + handler in question also pass.
+A filter is defined as:
+{FilterFun, Extra}+
where
The filter function can return
If
If the log event is returned, the next filter function is + called with the returned value as the first argument. That is, + if a filter function modifies the log event, the next filter + function receivea the modified event. The value returned from + the last filter funcion is the value that the handler callback + receives.
+If the filter function returns
The configuration
+ option
Filters are applied in the reverse order of installation, + meaning that the last added filter is applied first.
+ +Global filters are added
+ with
Handler filters are added with
+ with
To see which filters are currently installed in the system,
+ use
For convenience, the following built-in filters exist:
+ +This filter provides a way of filtering log events based
- on the log level. See
A handler is defined as a module exporting at least the + following function:
+ ++ +log(Log, Config)
This function is called when a log event has passed through all + global filters, and all handler filters attached to the handler + in question. The function call is executed on the client + process, and it is up to the handler implementation if other + processes are to be involved or not.
+ +Logger allows adding multiple instances of a handler + callback. That is, the callback module might be implemented in + such a way that, by using different handler identities, the same + callback module can be used for multiple handler + instances. Handler configuration is per instance.
+ +In addition to the mandatory callback function
The following built-in handlers exist:
-This filter matches all progress reports
- from
This is the default handler used by OTP. Multiple instances + can be started, and each instance will write log events to a + given destination, console or file. Filters can be used for + selecting which event to send to which handler instance.
+This filter matches all events originating from a process
- that has its group leader on a remote node.
- See
This handler behaves much like logger_std_h, except it uses
+
This handler is to be used for backwards compatibility
+ only. It is not started by default, but will be
+ automatically started the first time an
The old
The default formatter is
A formatter can be used by the handler implementation to do the
+ final formatting of a log event, before printing to the
+ handler's destination. The handler callback gets the formatter
+ information in the handler configuration, which is passed as the
+ second argument to
The formatter information consits of a formatter
+ module,
+format(Log,FConfig) + -> FormattedLogEntry
See the
Specifies the severity level to log.
+Specifies the global log level to log.
+See table
The initial value of this option is set by the Kernel
+ configuration
+ parameter
Logger filters are added or removed with +
Global filters are added and removed with
See
By default, no filters exist.
+See section
Default is
Specifies what to do with an event if all filters
return
See section
Default is
Handlers are added or removed with
-
See
Specifies the severity level to log.
+Specifies the log level which the handler logs.
+See table
The log level can be specified when adding the handler,
+ or changed during runtime with, for
+ instance,
Default is
Handler filters can be specified when adding the handler,
- or added or removed later with
+ or added or removed during runtime with
See
See
By default, no filters exist.
+Default is
Specifies what to do with an event if all filters
return
See section
Default is
See
The formatter which the handler can use for converting + the log event term to a printable string.
+See
The default module is
Default
+ is
Any keys not listed above are considered to be handler
+ specific configuration. The configuration of the Kernel
+ handlers can be found in
+ the
Note that
Notice that
All Logger's built-in handlers will call the given formatter + before printing.
Logger provides backwards compatibility with the old
Log data is expected to be either a format string and
- arguments, a string (unicode:chardata), or a report (map or
+ arguments, a string
+ (
Logger does, to a certain extent, check its input data before forwarding a log event to the handlers, but it does not evaluate conversion funs or check the validity of format strings @@ -630,17 +743,17 @@ removing_handler(logger:handler_id(),logger:config()) -> ok changing_config(logger:handler_id(),logger:config(),logger:config()) -> {ok,logger:config()} | {error,term()}
When
A handler can be removed by calling
When
In order for the built-in handlers to survive, and stay responsive, during periods of high load (i.e. when huge numbers of incoming log requests must be handled), a mechanism for overload protection @@ -734,7 +847,7 @@ do_log(Fd,Log,#{formatter:={FModule,FConfig}}) -> as follows:
The handler process keeps track of the length of its message queue and reacts in different ways depending on the current status. The purpose is to keep the handler in, or (as quickly as possible), @@ -829,7 +942,7 @@ logger:add_handler(my_standard_h, logger_std_h,
A potential problem with large bursts of log requests, is that log files may get full or wrapped too quickly (in the latter case overwriting previously logged data that could be of great importance). For this reason, @@ -870,7 +983,7 @@ logger:add_handler(my_disk_log_h, logger_disk_log_h,
A handler process may grow large even if it can manage peaks of high load without crashing. The overload protection mechanism includes user configurable levels for a maximum allowed message queue length and maximum allowed memory diff --git a/lib/kernel/src/logger.erl b/lib/kernel/src/logger.erl index 2a0e2e5f50..9c721d7fee 100644 --- a/lib/kernel/src/logger.erl +++ b/lib/kernel/src/logger.erl @@ -93,14 +93,16 @@ -type config() :: #{level => level(), filter_default => log | stop, filters => [{filter_id(),filter()}], - formatter => {module(),map()}, - term() => term()}. + formatter => {module(),formatter_config()}, + atom() => term()}. -type timestamp() :: integer(). +-type formatter_config() :: #{atom() => term()}. -type config_handler() :: {handler, handler_id(), module(), config()}. -export_type([log/0,level/0,report/0,msg_fun/0,metadata/0,config/0,handler_id/0, - filter_id/0,filter/0,filter_arg/0,filter_return/0, config_handler/0]). + filter_id/0,filter/0,filter_arg/0,filter_return/0,config_handler/0, + formatter_config/0]). %%%----------------------------------------------------------------- %%% API @@ -390,7 +392,7 @@ get_handler_config(HandlerId) -> -spec update_formatter_config(HandlerId,FormatterConfig) -> ok | {error,term()} when HandlerId :: config(), - FormatterConfig :: map(). + FormatterConfig :: formatter_config(). update_formatter_config(HandlerId,FormatterConfig) -> logger_server:update_formatter_config(HandlerId,FormatterConfig). -- cgit v1.2.3