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

NAME

Log::Contextual - Simple logging interface with a contextual log

SYNOPSIS

 use Log::Contextual qw( :log :dlog set_logger with_logger );
 use Log::Contextual::SimpleLogger;
 use Log::Log4perl ':easy';
 Log::Log4perl->easy_init($DEBUG);


 my $logger  = Log::Log4perl->get_logger;

 set_logger $logger;

 log_debug { 'program started' };

 sub foo {
   with_logger(Log::Contextual::SimpleLogger->new({
       levels => [qw( trace debug )]
     }) => sub {
     log_trace { 'foo entered' };
     my ($foo, $bar) = Dlog_trace { "params for foo: $_" } @_;
     # ...
     log_trace { 'foo left' };
   });
 }

 foo();

Beginning with version 1.008 Log::Dispatchouli also works out of the box with Log::Contextual:

 use Log::Contextual qw( :log :dlog set_logger );
 use Log::Dispatchouli;
 my $ld = Log::Dispatchouli->new({
    ident     => 'slrtbrfst',
    to_stderr => 1,
    debug     => 1,
 });

 set_logger $ld;

 log_debug { 'program started' };

DESCRIPTION

This module is a simple interface to extensible logging. It is bundled with a really basic logger, Log::Contextual::SimpleLogger, but in general you should use a real logger instead of that. For something more serious but not overly complicated, try Log::Dispatchouli (see "SYNOPSIS" for example.)

OPTIONS

-logger

When you import this module you may use -logger as a shortcut for set_logger, for example:

 use Log::Contextual::SimpleLogger;
 use Log::Contextual qw( :dlog ),
   -logger => Log::Contextual::SimpleLogger->new({ levels => [qw( debug )] });

sometimes you might want to have the logger handy for other stuff, in which case you might try something like the following:

 my $var_log;
 BEGIN { $var_log = VarLogger->new }
 use Log::Contextual qw( :dlog ), -logger => $var_log;

-package_logger

The -package_logger import option is similar to the -logger import option except -package_logger sets the the logger for the current package.

Unlike "-default_logger", -package_logger cannot be overridden with "set_logger".

 package My::Package;
 use Log::Contextual::SimpleLogger;
 use Log::Contextual qw( :log ),
   -package_logger => Log::Contextual::WarnLogger->new({
      env_prefix => 'MY_PACKAGE'
   });

If you are interested in using this package for a module you are putting on CPAN we recommend Log::Contextual::WarnLogger for your package logger.

-default_logger

The -default_logger import option is similar to the -logger import option except -default_logger sets the the default logger for the current package.

Basically it sets the logger to be used if set_logger is never called; so

 package My::Package;
 use Log::Contextual::SimpleLogger;
 use Log::Contextual qw( :log ),
   -default_logger => Log::Contextual::WarnLogger->new({
      env_prefix => 'MY_PACKAGE'
   });

A WORK IN PROGRESS

This module is certainly not complete, but we will not break the interface lightly, so I would say it's safe to use in production code. The main result from that at this point is that doing:

 use Log::Contextual;

will die as we do not yet know what the defaults should be. If it turns out that nearly everyone uses the :log tag and :dlog is really rare, we'll probably make :log the default. But only time and usage will tell.

FUNCTIONS

set_logger

 my $logger = WarnLogger->new;
 set_logger $logger;

Arguments: Ref|CodeRef $returning_logger

set_logger will just set the current logger to whatever you pass it. It expects a CodeRef, but if you pass it something else it will wrap it in a CodeRef for you. set_logger is really meant only to be called from a top-level script. To avoid foot-shooting the function will warn if you call it more than once.

with_logger

 my $logger = WarnLogger->new;
 with_logger $logger => sub {
    if (1 == 0) {
       log_fatal { 'Non Logical Universe Detected' };
    } else {
       log_info  { 'All is good' };
    }
 };

Arguments: Ref|CodeRef $returning_logger, CodeRef $to_execute

with_logger sets the logger for the scope of the CodeRef $to_execute. As with "set_logger", with_logger will wrap $returning_logger with a CodeRef if needed.

log_$level

Import Tag: :log

Arguments: CodeRef $returning_message, @args

All of the following six functions work the same except that a different method is called on the underlying $logger object. The basic pattern is:

 sub log_$level (&@) {
   if ($logger->is_$level) {
     $logger->$level(shift->(@_));
   }
   @_
 }

Note that the function returns it's arguments. This can be used in a number of ways, but often it's convenient just for partial inspection of passthrough data

 my @friends = log_trace {
   'friends list being generated, data from first friend: ' .
     Dumper($_[0]->TO_JSON)
 } generate_friend_list();

If you want complete inspection of passthrough data, take a look at the "Dlog_$level" functions.

log_trace

 log_trace { 'entered method foo with args ' join q{,}, @args };

log_debug

 log_debug { 'entered method foo' };

log_info

 log_info { 'started process foo' };

log_warn

 log_warn { 'possible misconfiguration at line 10' };

log_error

 log_error { 'non-numeric user input!' };

log_fatal

 log_fatal { '1 is never equal to 0!' };

logS_$level

Import Tag: :log

Arguments: CodeRef $returning_message, Item $arg

This is really just a special case of the "log_$level" functions. It forces scalar context when that is what you need. Other than that it works exactly same:

 my $friend = logS_trace {
   'I only have one friend: ' .  Dumper($_[0]->TO_JSON)
 } friend();

See also: "DlogS_$level".

Dlog_$level

Import Tag: :dlog

Arguments: CodeRef $returning_message, @args

All of the following six functions work the same as their "log_$level" brethren, except they return what is passed into them and put the stringified (with Data::Dumper::Concise) version of their args into $_. This means you can do cool things like the following:

 my @nicks = Dlog_debug { "names: $_" } map $_->value, $frew->names->all;

and the output might look something like:

 names: "fREW"
 "fRIOUX"
 "fROOH"
 "fRUE"
 "fiSMBoC"

Dlog_trace

 my ($foo, $bar) = Dlog_trace { "entered method foo with args: $_" } @_;

Dlog_debug

 Dlog_debug { "random data structure: $_" } { foo => $bar };

Dlog_info

 return Dlog_info { "html from method returned: $_" } "<html>...</html>";

Dlog_warn

 Dlog_warn { "probably invalid value: $_" } $foo;

Dlog_error

 Dlog_error { "non-numeric user input! ($_)" } $port;

Dlog_fatal

 Dlog_fatal { '1 is never equal to 0!' } 'ZOMG ZOMG' if 1 == 0;

DlogS_$level

Import Tag: :dlog

Arguments: CodeRef $returning_message, Item $arg

Like "logS_$level", these functions are a special case of "Dlog_$level". They only take a single scalar after the $returning_message instead of slurping up (and also setting wantarray) all the @args

 my $pals_rs = DlogS_debug { "pals resultset: $_" }
   $schema->resultset('Pals')->search({ perlers => 1 });

LOGGER INTERFACE

Because this module is ultimately pretty looking glue (glittery?) with the awesome benefit of the Contextual part, users will often want to make their favorite logger work with it. The following are the methods that should be implemented in the logger:

 is_trace
 is_debug
 is_info
 is_warn
 is_error
 is_fatal
 trace
 debug
 info
 warn
 error
 fatal

The first six merely need to return true if that level is enabled. The latter six take the results of whatever the user returned from their coderef and log them. For a basic example see Log::Contextual::SimpleLogger.

AUTHOR

frew - Arthur Axel "fREW" Schmidt <frioux@gmail.com>

DESIGNER

mst - Matt S. Trout <mst@shadowcat.co.uk>

COPYRIGHT

Copyright (c) 2010 the Log::Contextual "AUTHOR" and "DESIGNER" as listed above.

LICENSE

This library is free software and may be distributed under the same terms as Perl 5 itself.