NAME

Plack::Middleware::AccessLog::Structured - Access log middleware which creates structured log messages

VERSION

version 0.001002

SYNOPSIS

        use Plack::Middleware::AccessLog::Structured;

        Plack::Middleware::AccessLog::Structured->wrap($app,
                callback    => sub {
                        my ($env, $message) = @_;
                        $message->{foo} = 'bar';
                        return $message;
                },
                extra_field => {
                        'some.application.field' => 'log_field',
                        'some.psgi-env.field'    => 'another_log_field',
                },
        );

DESCRIPTION

Plack::Middleware::AccessLog::Structured is a Plack::Middleware which creates and logs structured messages.

If the above example is used with a basic PSGI application that simply returns ok, the following, JSON-encoded message would be logged (on one log line):

        {
                "remote_addr": "127.0.0.1",
                "request_method": "GET",
                "request_uri": "/",
                "server_protocol": "HTTP/1.1",
                "remote_user": null,
                "http_referer": null,
                "http_user_agent": "Mozilla/5.0 [...]",
                "request_duration": 0.0679492950439453,
                "epochtime": 1348687439.49608,
                "date": "2012-09-26T19:23:59.496Z",
                "hostfqdn": "some.hostname.tld",
                "hostname": "some",
                "http_host": "localhost:5000",
                "pid": 4777,
                "log_field": null,
                "another_log_field": null,
                "foo": "bar",
                "response_status": 200,
                "content_length": 2,
                "content_type": "text/plain",
                "class": "Plack::Middleware::AccessLog::Structured"
        }

METHODS

new

Constructor, creates new instance.

Parameters

This method expects its parameters as a hash reference.

callback

Optional callback that can be used to modify the log message before it is encoded and logged. Gets the PSGI environment and the message as parameters and must return the possibly modified message.

extra_field

Optional hash reference with a mapping from PSGI environment keys to keys in the log message. If passed, values from the PSGI environment will be copied to the corresponding fields in the log message, using this mapping.

logger

A callback to pass the JSON-encoded log messages to. By default, log messages are printed to the psgi.errors output stream.

Result

A fresh instance of the middleware.

call

Specialized call method.

SEE ALSO

AUTHOR

Manfred Stock <mstock@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2018 by Manfred Stock.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.