Logging

Zotonic uses Logger for logging. Logger metadata is automatically set by Zotonic in the controller functions.

To log a message from your code, simply call Logger directly:

-include_lib("kernel/include/logger.hrl").

?LOG_ERROR("Something went very wrong with ~p", [SomeParam]).

which will write the following in the error.log file:

2022-01-28 17:17:45 ERROR <0.9.0> [some_module:some_function/0:42] ▸ text="Something went very wrong with whatever"

Configuration

In The erlang.config file file, change the kernel section to configure log handlers and formatters. See the Logger documentation for more information.

For instance, to configure Logger for logging to Logstash, uncomment the logstash handler in the logger configuration of your erlang.config file and check the logstasher configuration:

erlang.config
{kernel, [
    % Minimum log level for all loggers below.
    {logger_level, info},

    {logger, [

        %% To use logstash:
        %% - Enable the logstasher_h handler
        %% - Configure logstasher (see below the kernel config)
        %%
        % {handler, logstash, logstasher_h,
        %     #{
        %         level => info
        %     }
        % },

        % Log of all information to the terminal/console.
        % The 'logger_level' above defines what is shown on the console.
        {handler, default, z_logger_h,
            #{
                level => debug,
                formatter => {z_logger_formatter,
                    #{
                        prettify => true,
                        colored => true,
                        time_designator => $\s,
                        time_offset => "",
                        time_unit => second,
                        strip_tz => true,
                        level_capitalize => true
                    }
                }
            }
        },

        % Error log on disk.
        % LOG_DIR is replaced with the default Zotonic ZOTONIC_LOG_DIR directory.
        {handler, error_log, z_logger_h,
            #{
                level => error,
                config => #{
                    type => file,
                    file => "LOG_DIR/error.log",
                    max_no_files => 10,
                    max_no_bytes => 52428800 % 10 x 5mb
                },
                formatter => {z_logger_formatter,
                    #{
                        prettify => true,
                        colored => false,
                        time_designator => $\s,
                        time_offset => "",
                        time_unit => second,
                        strip_tz => true,
                        level_capitalize => true
                    }
                }
            }
        },

        % Console log on disk.
        % LOG_DIR is replaced with the default Zotonic ZOTONIC_LOG_DIR directory.
        {handler, console_log, z_logger_h,
            #{
                level => debug,
                config => #{
                    type => file,
                    file => "LOG_DIR/console.log",
                    max_no_files => 10,
                    max_no_bytes => 52428800 % 10 x 5mb
                },
                formatter => {z_logger_formatter,
                    #{
                        prettify => true,
                        colored => false,
                        time_designator => $\s,
                        time_offset => "",
                        time_unit => second,
                        strip_tz => true,
                        level_capitalize => true
                    }
                }
            }
        }
    ]}
]},

%% Logstash configuration.
%% If a logger handler with 'logstasher_h' is defined then zotonic_core will start the
%% logstasher application.
{logstasher, [
    {transport, console}, % tcp | udp | console
    {host, "localhost"},  % inet:hostname()
    {port, 5000}          % inet:port_number()
]},

Command-line shell Developer Guide Testing sites

Referred by

Logging to Logstash

Logstash is often used for log centralization and analysis. This cookbook describes how to set up Zotonic for logging…

Upgrade notes

These notes list the most important changes between Zotonic versions. Please read these notes carefully when upgrading…