From a9a6b803a60793d42a74e0f1693a7594dffb6bc3 Mon Sep 17 00:00:00 2001 From: Siri Hansen Date: Thu, 17 May 2018 18:19:57 +0200 Subject: Improve documentation of logger and error_logger --- lib/kernel/doc/src/logger_chapter.xml | 222 ++++++++++++++++++++-------------- 1 file changed, 130 insertions(+), 92 deletions(-) (limited to 'lib/kernel/doc/src/logger_chapter.xml') diff --git a/lib/kernel/doc/src/logger_chapter.xml b/lib/kernel/doc/src/logger_chapter.xml index 4232429589..21b460e72a 100644 --- a/lib/kernel/doc/src/logger_chapter.xml +++ b/lib/kernel/doc/src/logger_chapter.xml @@ -36,14 +36,14 @@ 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 will forward log events to one - or more handler(s).

+ handler part. The logger part forwards log events to + one or more handler(s).

Conceptual overview -

Filters can be added to the logger and to each +

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.

@@ -121,10 +121,10 @@ log(Log, Config) -> ok -

A handler is called by the logger backend after filtering on - logger level and on handler level for the handler which is - about to be called. The function call is done on the client - process, and it is up to the handler implementation if other +

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 @@ -134,7 +134,7 @@ Filter -

Filters can be set on the logger or on a handler. Logger +

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 @@ -145,7 +145,7 @@ {fun((Log,Extra) -> Log | stop | ignore), Extra}

The configuration parameter filter_default - specifies the behavior if all filters return ignore. + specifies the behaviour if all filters return ignore. filter_default is by default set to log.

The Extra parameter may contain any data that the @@ -250,52 +250,58 @@

Logger can be configured either when the system starts through configuration parameters, - or at run-time by using the logger + or at run-time by using the logger(3) API. The recommended approach is to do the initial configuration in the sys.config file and then use the API when some configuration - has to be changed at run-time, such as the logging level.

+ has to be changed at run-time, such as the log level.

- Application configuration parameters + Kernel Configuration Parameters

Logger is best configured by using the configuration parameters - of kernel. There are three possible configuration parameters: + of Kernel. There are four possible configuration parameters: logger, logger_level, logger_sasl_compatible and logger_log_progress. - logger_level, logger_sasl_compatible and logger_log_progress are described in the + logger_level, logger_sasl_compatible and logger_log_progress are described in the Kernel Configuration, while logger is described below.

-
+ - logger -

The logger application configuration parameter is used to configure - three different logger aspects; handlers, logger filters and module levels. +

logger

+

The application configuration parameter logger is used to configure + three different Logger aspects; handlers, logger filters and module levels. The configuration is a list containing tagged tuples that look like this:

DisableHandler = {handler,default,undefined} - Disable the default handler. This will allow another application + +

Disable the default handler. This allows another application to add its own default handler. See - logger:add_handlers/1 for more details. + logger:add_handlers/1 for more details.

+

Only one entry of this option is allowed.

AddHandler = {handler,HandlerId,Module,HandlerConfig} - Add a handler as if - logger:add_handler(HandlerId,Module,HandlerConfig) had been - called. - Filters = {filters, FilterDefault, [Filter]}
+ +

Add a handler as if + logger:add_handler(HandlerId,Module,HandlerConfig) is + called.

+

It is allowed to have multiple entries of this option.

+ Filters = {filters, default, [Filter]}
FilterDefault = log | stop
Filter = {FilterId, {FilterFun, FilterConfig}}
- Add the specified - logger filters. Only one entry is allowed of this option. - ModuleLevel - {module_level, Level, [Module]}, - this option configures the - module log level to be used. It is possible to have multiple - module_level entries. + +

Add the specified + logger filters.

+

Only one entry of this option is allowed.

+ ModuleLevel = {module_level, Level, [Module]} + +

This option configures + module log level.

+

It is allowed to have multiple entries of this option.

Examples:

-

Output logs into a the file "logs/erlang.log"

+

Output logs into the file "logs/erlang.log"

[{kernel, [{logger, @@ -338,7 +344,6 @@
-
@@ -441,36 +446,30 @@ error_logger in the following ways:

- Legacy event handlers - -

To use event handlers written for error_logger, just - add your event handler with

- -error_logger:add_report_handler/1,2. - -

This will automatically start the error_logger - event manager, and add error_logger as a - handler to logger, with configuration

- -#{level=>info, - filter_default=>log, - filters=>[]}. - -

Note that this handler will ignore events that do not - originate from the old error_logger API, or from - within OTP. This means that if your code uses the logger API - for logging, then your log events will be discarded by this - handler.

-

Also note that error_logger is not overload - protected.

-
- Logger API + API for Logging

The old error_logger API still exists, but should only be used by legacy code. It will be removed in a later release.

+

Calls + to + error_logger:error_report/1,2, + + error_logger:error_msg/1,2, and + corresponding functions for warning and info messages, are + all forwarded to Logger as calls + to + logger:log(Level,Report,Metadata).

+

Level = error | warning | info and is taken + from the function name. Report contains the actual + log message, and Metadata contains additional + information which can be used for creating backwards + compatible events for legacy error_logger event + handlers, see + section Legacy + Event Handlers.

- Output format + Output Format

To get log events on the same format as produced by error_logger_tty_h and error_logger_file_h, @@ -478,13 +477,13 @@ error_logger:add_report_handler/1,2. configuration parameter legacy_header=>true. This is also the default.

- Default format of log events from OTP + Default Format of Log Events from OTP

By default, all log events originating from within OTP, except the former so called "SASL reports", look the same as before.

- SASL reports + SASL Reports

By SASL reports we mean supervisor reports, crash reports and progress reports.

@@ -494,32 +493,63 @@ error_logger:add_report_handler/1,2. named sasl_report_tty_h and sasl_report_file_h.

The destination of these log events were configured by - environment variables for the SASL application.

+ SASL + configuration parameters.

Due to the specific event handlers, the output format slightly differed from other log events.

As of OTP-21, the concept of SASL reports is removed, - meaning that the default behavior is as follows:

+ meaning that the default behaviour is as follows:

Supervisor reports, crash reports and progress reports are no longer connected to the SASL application. Supervisor reports and crash reports are logged by default. Progress reports are not logged by default, but can be - enabled with the kernel environment - variable logger_log_progress. + enabled with the Kernel configuration + parameter + logger_log_progress.
The output format is the same for all log events. -

If the old behavior is preferred, the kernel environment - variable logger_sasl_compatible can be set - to true. The old SASL environment variables can then - be used as before, and the SASL reports will only be printed - if the SASL application is running - through a second log - handler named sasl_h.

+

If the old behaviour is preferred, the Kernel configuation + parameter + logger_sasl_compatible can be set + to true. The + old SASL + configuration parameters can then be used as + before, and the SASL reports will only be printed if the + SASL application is running, through a second log handler + named sasl_h.

All SASL reports have a metadata field domain=>[beam,erlang,otp,sasl], which can be used, for example, by filters to stop or allow the events.

+

See the SASL User's + Guide for more information about the old SASL + error logging functionality.

+ + Legacy Event Handlers + +

To use event handlers written for error_logger, just + add your event handler with

+ +error_logger:add_report_handler/1,2. + +

This will automatically start the error_logger + event manager, and add error_logger as a + handler to logger, with configuration

+ +#{level=>info, + filter_default=>log, + filters=>[]}. + +

Notice that this handler will ignore events that do not + originate from the old error_logger API, or from + within OTP. This means that if your code uses the Logger API + for logging, then your log events will be discarded by this + handler.

+

Also notice that error_logger is not overload + protected.

@@ -535,14 +565,14 @@ error_logger:add_report_handler/1,2. converting the report to a format string and arguments. The handler might also do a custom conversion if the default format is not desired.

-

logger does, to a certain extent, check its input data +

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 and arguments. This means that any filter or handler must be careful when formatting the data of a log event, making sure that it does not crash due to bad input data or faulty callbacks.

-

If a filter or handler still crashes, logger will remove the +

If a filter or handler still crashes, Logger will remove the filter or handler in question from the configuration, and then print a short error message on the console. A debug event containing the crash reason and other details is also issued, @@ -552,13 +582,13 @@ error_logger:add_report_handler/1,2.

Example: add a handler to log debug events to file -

When starting an erlang node, the default behavior is that all +

When starting an erlang node, the default behaviour is that all log events with level info and above are logged to the console. In order to also log debug events, you can either change the global log level to debug or add a separate handler to take care of this. In this example we will add a new handler which prints the debug events to a separate file.

-

First, we add an instance of logger_std_h with +

First, we add an instance of logger_std_h with type {file,File}, and we set the handler's level to debug:

@@ -575,9 +605,9 @@ ok
#Fun<erl_eval.12.98642416> 4> logger:add_handler_filter(debug_handler,allow_debug,{Fun,[]}). ok -

And finally, we need to make sure that the logger itself allows +

And finally, we need to make sure that Logger itself allows debug events. This can either be done by setting the global - logger level:

+ log level:

 5> logger:set_logger_config(level,debug).
 ok
@@ -599,21 +629,22 @@ adding_handler(logger:handler_id(),logger:config()) -> {ok,logger:config()} | {e removing_handler(logger:handler_id(),logger:config()) -> ok changing_config(logger:handler_id(),logger:config(),logger:config()) -> {ok,logger:config()} | {error,term()} -

When logger:add_handler(Id,Module,Config) is called, logger - will first call Module:adding_handler(Id,Config), and if it - returns {ok,NewConfig} the NewConfig is written to the +

When logger:add_handler(Id,Module,Config) is called, Logger + will first call Module:adding_handler(Id,Config), and if it + returns {ok,NewConfig}, NewConfig is written to the configuration database. After this, the handler may receive log - events as calls to Module:log/2.

+ events as calls to Module:log/2.

A handler can be removed by calling - logger:remove_handler(Id). logger will call - Module:removing_handler(Id,Config), and then remove the handler's - configuration from the configuration database.

-

When logger:set_handler_config is called, logger calls - Module:changing_config(Id,OldConfig,NewConfig). If this function - returns ok, the NewConfig is written to the configuration - database.

- -

A simple handler which prints to the console could be + logger:remove_handler(Id). Logger will call + Module:removing_handler(Id,Config), and then remove the + handler's configuration from the configuration database.

+

When logger:set_handler_config/2,3 + or logger:update_handler_config/2 are called, Logger + calls Module:changing_config(Id,OldConfig,NewConfig). If + this function returns {ok,NewConfig}, NewConfig is + written to the configuration database.

+ +

A simple handler that prints to the console could be implemented as follows:

-module(myhandler). @@ -720,7 +751,7 @@ do_log(Fd,Log,#{formatter:={FModule,FConfig}}) -> and as long as the length of the message queue is lower, all log requests are handled asynchronously. This simply means that the process sending the log request (by calling a log function in the - logger API) does not wait for a response from the handler but + Logger API) does not wait for a response from the handler but continues executing immediately after the request (i.e. it will not be affected by the time it takes the handler to print to the log device). If the message queue grows larger than this value, however, @@ -876,7 +907,14 @@ logger:add_handler(my_disk_log_h, logger_disk_log_h,
See Also -

error_logger(3), - SASL(6)

+

+ disk_log(3), + error_logger(3), + logger(3), + logger_disk_log_h(3), + logger_filters(3), + logger_formatter(3), + logger_std_h(3), + sasl(6)

-- cgit v1.2.3