Log::Log4perl::Catalyst - Log::Log4perl Catalyst Module
In your main Catalyst application module:
use Log::Log4perl::Catalyst; # Either make Log4perl act like the Catalyst default logger: __PACKAGE__->log(Log::Log4perl::Catalyst->new()); # or use a Log4perl configuration file, utilizing the full # functionality of Log4perl __PACKAGE__->log(Log::Log4perl::Catalyst->new('l4p.conf'));
... and then sprinkly logging statements all over any code executed by Catalyst:
$c->log->debug("This is using log4perl!");
This module provides Log4perl functions to Catalyst applications. It was inspired by Catalyst::Log::Log4perl on CPAN, but has been completely rewritten and uses a different approach to unite Catalyst and Log4perl.
Log4perl provides loggers, usually associated with the current package, which can then be remote-controlled by a central configuration. This means that if you have a controller function like
package MyApp::Controller::User; sub add : Chained('base'): PathPart('add'): Args(0) { my ( $self, $c ) = @_; $c->log->info("Adding a user"); # ... }
Level-based control is available via the following methods:
$c->log->debug("Reading configuration"); $c->log->info("Adding a user"); $c->log->warn("Can't read configuration ($!)"); $c->log->error("Can't add user ", $user); $c->log->fatal("Database down, aborting request");
But that's no all, Log4perl is much more powerful.
The logging statement can be suppressed or activated based on a Log4perl file that looks like
# All MyApp loggers opened up for DEBUG and above log4perl.logger.MyApp = DEBUG, Screen # ...
or
# All loggers block messages below INFO log4perl.logger=INFO, Screen # ...
respectively. See the Log4perl manpage on how to perform fine-grained log-level and location filtering, and how to forward messages not only to the screen or to log files, but also to databases, email appenders, and much more.
Also, you can vary the layout of each message. For example if you want to know where a particular statement was logged, turn on file names and line numbers:
# Log4perl configuration file # ... log4perl.appender.Screen.layout.ConversionPattern = \ %F{1}-%L: %p %m%n
Messages will then look like
MyApp.pm-1869: INFO Saving user profile for user "wonko"
Or want to log a request's IP address with every log statement? No problem with Log4perl, just call
Log::Log4perl::MDC->put( "ip", $c->req->address() );
at the beginning of the request cycle and use
# Log4perl configuration file # ... log4perl.appender.Screen.layout.ConversionPattern = \ [%d]-%X{ip} %F{1}-%L: %p %m%n
as a Log4perl layout. Messages will look like
[2010/02/22 23:25:55]-123.122.108.10 MyApp.pm-1953: INFO Reading profile for user "wonko"
Again, check the Log4perl manual page, there's a plethora of configuration options.
If called without parameters, new() initializes Log4perl in a way so that messages are logged similiarly to Catalyst's default logging mechanism. If you provide configuration, either the name of a configuration file or a reference to scalar string containing the configuration, it will call Log4perl with these parameters.
The second (optional) parameter is a list of key/value pairs:
'autoflush' => 1 # Log without buffering ('abort' not supported) 'watch_delay' => 30 # If set, use L<Log::Log4perl>'s init_and_watch
Flushes the cache.
Clears the logging system's internal buffers without logging anything.
Copyright 2002-2010 by Mike Schilli <m@perlmeister.com>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Log::Log4perl, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Log::Log4perl
CPAN shell
perl -MCPAN -e shell install Log::Log4perl
For more information on module installation, please visit the detailed CPAN module installation guide.