Logging to Logstash

Logstash is often used for log centralization and analysis. This cookbook describes how to set up Zotonic for logging to Logstash over UDP. As mentioned in the Logging chapter, Zotonic uses Logger.

So we will change Zotonic’s Logstash configuration in order to send messages to Logstash.

Step 1: add a Logstash handler

Zotonic comes with the logstash handler logstasher_h for logger. The handler will be started automatically if it is configured as a logger handler in The erlang.config file:

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
            }
        },

        %%% Other logger configs here
        ...
    }
]},

Step 2: configure the Logstash handler

The next step is to tell the Logstash handler where it should send its messages to. The configuration of logstasher in the The erlang.config file can be found below the kernel section or else added there:

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

Replace logger with the hostname or IP address of your logger. IP addresses can be configured as an Erlang tuple, for example: {127,0,0,1}

If the console output is shipped to Logstash, then use console as the transport. The transport console ignores the configurations host and port.

After you changed the erlang.config file you will need to restart Zotonic.

You should now find all Zotonic log messages in Logstash. To test this, just call:

logger:error_msg("Just testing the Logstash setup here!").