++ed by:

1 PAUSE user

Michael Jemmeson
and 1 contributors


Mojo::Log::JSON - Simple JSON logger


    use Mojo::Log::JSON;

    # Log to STDERR
    my $logger = Mojo::Log::JSON->new;

    # Customize log file location, minimum log level and default fields
    my $logger = Mojo::Log::JSON->new(    #
        path           => '/var/log/mojo.log',
        level          => 'warn',
        default_fields => {                      # default fields added to log
            app_name => "MyApp",                 # either scalar
            random_thing => sub { rand() },      # or codref,
        include_level => 1,    # default - include 'level' key in output

    # To add an extra default field
    $logger->default_fields->{foo} = "bar";

    # Log messages - debug, info, warn, error, fatal (same as Mojo::Log)
    $logger->debug( "A simple string" );
    $logger->debug( "A", "message", "over", "multiple", "lines" );
    $logger->debug( { message => "A data structure", abc => "123" } );

    # The above examples would generate something like the following:
    {"datetime":"2014-03-13 13:15:44","foo":"bar","level":"debug","message":"A simple string"}
    {"datetime":"2014-03-13 13:15:45","foo":"bar","level":"debug","message":"A\nmessage\nover\nmultiple\nlines"}
    {"datetime":"2014-03-13 13:15:46","abc":"123","foo":"bar","level":"debug","message":"A data structure"}

    # To not include 'level' set an undefined default_fields key either in the
    # constructure or as follows:
    $logger->default_fields->{level} = undef;


Mojo::Log::JSON is a simple JSON logger for Mojo projects. It logs a JSON object (hashref) per log message. Each object occupies a single line to allow easy parsing of the log output.

The key level is always added to the data structure, with the value set to the level of the log message being emitted. To omit this, set include_level to a false value.

By default the key datetime is also added to the data structure with a value of the current time in ISO 8601 format. This can be removed or other fields can be added via the default_fields attribute.


All of the attributes of Mojo::Log are present, plus the following additional attributes.


Pass in a JSON object to override the default JSON formatting settings.


Hashref of key/value pairs to add to the data structure. Can be overwritten by values in the log message


Mojo::Log::JSON inherits all methods from Mojo::Log.




Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker at https://github.com/mjemmeson/mojo-log-json/issues. You will be notified automatically of any progress on your issue.

Source Code

This is open source software. The code repository is available for public review and contribution under the terms of the license.


    git clone git://github.com/mjemmeson/mojo-log-json.git


Michael Jemmeson <mjemmeson@cpan.org>


  • Tom Hukins (CPAN: TOMHUKINS)


Copyright 2014- Michael Jemmeson


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