The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Leyland::Manual::Logging - How to use a log in Leyland applications

VERSION

version 0.001007

LOGGING

Logging is an important aspect of any web application, at least in my opinion. There are quite a lot of options on CPAN for logging, such as Log::Dispatch, Log::Handler (my favorite), Log::Log4perl and many others (not to mention plain old print STDERR $log_msg).

Leyland gives you the ability to choose which logging platform to use. Currently, Log::Dispatch and Log::Handler are supported, plus a default platform that simply logs to the standard error output.

SELECTING A LOG CLASS

You tell Leyland which platform to use with the "logger" option in your application's $config variable (see "CONFIGURING LEYLAND APPLICATIONS" in Leyland::Manual::Applications for more info). The "logger" option is expected to be a hash-ref with two keys: "class", which defines the log class to use, and "opts", which is a hash-ref of options that are specific to the log class chosen.

The three current log classes are LogDispatch, LogHandler and STDERR (the latter takes no arguments and is the default, so there's no need to define it if you want to use it). Refer to the documentation of the log class of your choice to learn about how to configure it. Here's a Log::Dispatch example:

        # in app.psgi
        my $config = {
                ...
                logger => {
                        class => 'LogDispatch',
                        opts => {
                                outputs => [
                                        [ 'File',   min_level => 'debug', filename => "myapp.$ENV{PLACK_ENV}.log", newline => 1 ],
                                        [ 'Screen', min_level => 'warning', newline => 1 ],
                                ],
                        }
                },
                ...
        };

As you can see, we are telling leyland that our log class is going to be Leyland::Logger::LogDispatch, and that we want our log messages to be printed to the screen, and also to a file.

PRINTING LOG MESSAGES

As you may already know, when using logging platforms, every log message you print is assigned a log level, which denotes the severity of the message. Common levels are "info", "warn" and "error". Unfortunately, different log classes have different log levels, so Leyland only supports levels which are common to all classes. These are "info", "warn", "error" and "debug".

Once initialized, a log object will be available both to your application object and also to every context object:

        get '^/$' {
                $c->log->info("Got a request to display the index page");
                $c->template('index.html');
        }

And that's pretty much it.

BUT WAIT, WHY NOT USE PLACK'S LOG MIDDLEWARES?

See "WHY AREN'T YOU USING PLACK'S LOGGING MIDDLEWARES?" in Leyland::Manual::FAQ for the answer.

WHAT'S NEXT?

Read Leyland::Manual::Exceptions to learn how to return proper HTTP exceptions or return to the table of contents.

AUTHOR

Ido Perlmuter, <ido at ido50.net>

BUGS

Please report any bugs or feature requests to bug-Leyland at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Leyland. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

        perldoc Leyland::Manual::Views

You can also look for information at:

LICENSE AND COPYRIGHT

Copyright 2010-2011 Ido Perlmuter.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.