A logging role for moose based on log::log4perl
package MyApp; use Moose; with 'MooseX::Log::Log4perl'; sub something { my ($self) = @_; $self->log->debug("started bar"); ### logs with default class catergory "MyApp" ... $self->log('special')->info('bar'); ### logs with category "special" ... $self->log('.special')->info('bar'); ### logs with category "MyApp.special" $self->log('::special')->info('bar');### logs with category "MyApp.special" }
A logging role building a very lightweight wrapper to Log::Log4perl for use with your Moose classes. The initialization of the Log4perl instance must be performed prior to logging the first log message. Otherwise the default initialization will happen, probably not doing the things you expect.
For compatibility the \*(C`logger\*(C' attribute can be accessed to use a common interface for application logging.
Using the logger within a class is as simple as consuming a role:
package MyClass; use Moose; with 'MooseX::Log::Log4perl';
sub dummy { my $self = shift; $self->log->info("Dummy log entry"); }
The logger needs to be setup before using the logger, which could happen in the main application:
package main; use Log::Log4perl qw(:easy); use MyClass;
BEGIN { Log::Log4perl->easy_init() };
my $myclass = MyClass->new(); $myclass->log->info("In my class"); # Access the log of the object $myclass->dummy; # Will log "Dummy log entry"
For simple logging needs use MooseX::Log::Log4perl::Easy to directly add log_<level> methods to your class instance.
$self->log_info("Dummy");
As this module is using Any::Moose, you can use it with Mouse instead of Moose too.
This will allow to simple use it as documented above in a Mouse based application, like shown in the example below:
This is your class consuming the MooseX::Log::Log4perl role.
package MyCat; use Mouse;
with 'MooseX::Log::Log4perl';
sub catch_it { my $self = shift; $self->log->debug("Say Miau"); }
Which can be simply used in your main application then.
package main; use MyCat; use Log::Log4perl qw(:easy); BEGIN { Log::Log4perl->easy_init() };
my $log = Log::Log4perl->get_logger(); $log->info("Application startup..."); MyCat->new()->catch_it(); ### Will log "Dummy dodo"
The \*(C`logger\*(C' attribute holds the Log::Log4perl object that implements all logging methods for the defined log levels, such as \*(C`debug\*(C' or \*(C`error\*(C'. As this method is defined also in other logging roles/systems like MooseX::Log::LogDispatch this can be thought of as a common logging interface.
package MyApp::View::JSON;
extends 'MyApp::View'; with 'MooseX:Log::Log4perl';
sub bar { $self->logger->info("Everything fine so far"); # logs a info message $self->logger->debug("Something is fishy here"); # logs a debug message }
Basically the same as logger, but also allowing to change the log category for this log message. If the category starts with a \*(C`+\*(C', we pre-pend the current class (what would have been the category if you didn't specify one).
if ($myapp->log->is_debug()) { $myapp->log->debug("Woot"); # category is class myapp } $myapp->log("TempCat")->info("Foobar"); # category TempCat $myapp->log->info("Grumble"); # category class again myapp $myapp->log(".TempCat")->info("Foobar"); # category myapp.TempCat $myapp->log("::TempCat")->info("Foobar"); # category myapp.TempCat
Log::Log4perl, Moose, MooseX::LogDispatch
Please report any bugs or feature requests to \*(C`[email protected]\*(C', or through the web interface at <http://rt.cpan.org>.
Or come bother us in \*(C`#moose\*(C' on \*(C`irc.perl.org\*(C'.
Roland Lammel \*(C`<[email protected]>\*(C'
Inspired by the work by Chris Prather \*(C`<[email protected]>\*(C' and Ash Berlin \*(C`<[email protected]>\*(C' on MooseX::LogDispatch
In alphabetical order:
Copyright (c) 2008-2012, Roland Lammel \*(C`<[email protected]>\*(C', http://www.quikit.at
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.