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

NAME

MojoX::Log::Log4perl - Log::Log4perl logging for Mojo/Mojolicious

VERSION

Version 0.01

SYNOPSIS

In lib/MyApp.pm:

  use MojoX::Log::Log4perl;

  # just create a custom logger object for Mojo/Mojolicious to use
  # (this is usually done inside the "startup" sub on Mojolicious).
  # If we dont supply any arguments to new, it will work almost
  # like the default Mojo logger.
  
  $self->log( MojoX::Log::Log4perl->new() );

  # But the real power of Log4perl lies in the configuration, so
  # lets try that. example.conf is included in the distribution.
  
  $self->log( MojoX::Log::Log4perl->new('example.conf') );

And later...

  $c->app->log->debug("This is using log4perl!");

DESCRIPTION:

This module provides a Mojo::Log implementation that uses Log::Log4perl as the underlying log mechanism. It provides all the methods listed in Mojo::Log (and many more from Log4perl - see below), so, if you already use Mojo::Log in your application, there is no need to change a single line of code!

These methods will all instantiate a logger with the component set to the package that called it. For example, if you were in the MyApp::Main package, the following:

  package MyApp::Main;
  use base 'Mojolicious::Controller';
        
  sub default {
      my ( $self, $c ) = @_;
      my $logger = $c->app->log;
      
      $logger->debug("Woot!");
  }

Would send a message to the Myapp.Main Log::Log4perl component.

See Log::Log4perl for more information on how to configure different logging mechanisms based on the component.

INSTANTIATION

new

new($config)

    This builds a new MojoX::Log::Log4perl object. If you provide an argument to new(), it will be passed directly to Log::Log4perl::init.

LOG LEVELS

Below are all log levels from MojoX::Log::Log4perl, in descending priority:

fatal

error

warn

info

debug

trace

Just like Log::Log4perl: "If your configured logging level is WARN, then messages logged with info(), debug(), and trace() will be suppressed. fatal(), error() and warn() will make their way through, because their priority is higher or equal than the configured setting."

log

You can also use the log() method just like in Mojo::Log:

  $logger->log( info => 'I can haz cheezburger');

But nobody does that, really.

CHECKING LOG LEVELS

As usual, you can (and should) avoid doing expensive log calls by checking the current log level:

is_fatal

is_error

is_warn

is_info

is_debug

is_trace

is_level

You can also use the is_level() method just like in Mojo::Log:

  $logger->is_level( 'warn' );

But nobody does that, really.

ADDITIONAL LOGGING METHODS

The following log4perl methods are also available for direct usage:

logwarn

   $logger->logwarn($message);
   

This will behave just like:

   $logger->warn($message)
       && warn $message;

logdie

   $logger->logdie($message);
   

This will behave just like:

   $logger->fatal($message)
       && die $message;

If you also wish to use the ERROR log level with warn() and die(), you can:

error_warn

   $logger->error_warn($message);
   

This will behave just like:

   $logger->error($message)
       && warn $message;

error_die

   $logger->error_die($message);
   

This will behave just like:

   $logger->error($message)
       && die $message;

Finally, there's the Carp functions that do just what the Carp functions do, but with logging:

logcarp

    $logger->logcarp();        # warn w/ 1-level stack trace

logcluck

    $logger->logcluck();       # warn w/ full stack trace

logcroak

    $logger->logcroak();       # die w/ 1-level stack trace

logconfess

    $logger->logconfess();     # die w/ full stack trace

ATTRIBUTES

The original handle and path attributes from Mojo::Log are not implemented as they make little sense in a Log4perl environment. The only attribute available, therefore, is level.

level

  my $level = $logger->level();
  

This will return the current log level (debug, info, ...). You can also use this to force a level:

  $logger->level('warn');  # forces 'debug' level

But you really shouldn't do that at all, as it breaks log4perl's configuration structure. The whole point of Log4perl is letting you setup your logging from outside your code. So, once again: don't do this.

AUTHOR

Breno G. de Oliveira, <garu at cpan.org>

BUGS

Please report any bugs or feature requests to bug-mojo-log-log4perl at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MojoX-Log-Log4perl. 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 MojoX::Log::Log4perl

You can also look for information at:

ACKNOWLEDGEMENTS

This module was heavilly inspired by Catalyst::Log::Log4perl. A lot of the documentation and specifications were taken almost verbatim from it.

Also, this is just a minor work. Credit is really due to Michael Schilli and Sebastian Riedel, creators and maintainers of Log::Log4perl and Mojo, respectively.

SEE ALSO

Log::Log4perl, Mojo::Log, Mojo, Mojolicious

COPYRIGHT & LICENSE

Copyright 2009 Breno G. de Oliveira, all rights reserved.

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