NAME¶
Log::Log4perl::Catalyst - Log::Log4perl Catalyst Module
SYNOPSIS¶
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!");
DESCRIPTION¶
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.
METHODS¶
- new($config, [%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
- _flush()
- Flushes the cache.
- abort($abort)
- Clears the logging system's internal buffers without
logging anything.
Using :easy Macros with Catalyst¶
If you're tired of typing
$c->log->debug("...");
and would prefer to use Log4perl's convenient :easy mode macros like
DEBUG "...";
then just pull those macros in via Log::Log4perl's :easy mode and start
cranking:
use Log::Log4perl qw(:easy);
# ... use macros later on
sub base :Chained('/') :PathPart('apples') :CaptureArgs(0) {
my ( $self, $c ) = @_;
DEBUG "Handling apples";
}
Note the difference between Log4perl's initialization in Catalyst, which uses
the Catalyst-specific Log::Log4perl::Catalyst module (top of this page), and
making use of Log4perl's loggers with the standard Log::Log4perl loggers and
macros. While initialization requires Log4perl to perform dark magic to
conform to Catalyst's different logging strategy, obtaining Log4perl's logger
objects or calling its macros are unchanged.
Instead of using Catalyst's way of referencing the "context" object $c
to obtain logger references via its
log() method, you can just as well
use Log4perl's
get_logger() or macros to access Log4perl's logger
singletons. The result is the same.
COPYRIGHT AND LICENSE¶
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.