File Coverage

blib/lib/MooseX/Log/Log4perl.pm
Criterion Covered Total %
statement 15 15 100.0
branch 4 4 100.0
condition 3 3 100.0
subroutine 4 4 100.0
pod 1 1 100.0
total 27 27 100.0


line stmt bran cond sub pod time code
1             package MooseX::Log::Log4perl;
2              
3 4     4   27291 use 5.008;
  4         15  
  4         164  
4 4     4   2651 use Any::Moose 'Role';
  4         49964  
  4         27  
5 4     4   34389 use Log::Log4perl;
  4         65525  
  4         36  
6              
7             our $VERSION = '0.46';
8              
9             has 'logger' => (
10             is => 'rw',
11             isa => 'Log::Log4perl::Logger',
12             lazy => 1,
13             default => sub { return Log::Log4perl->get_logger(ref($_[0])) }
14             );
15              
16             sub log {
17 15     15 1 5809 my $self = shift;
18 15         24 my $cat = shift;
19 15 100 100     79 if ($cat && $cat =~ m/^(\.|::)/) {
    100          
20 2         12 return Log::Log4perl->get_logger(ref($self) . $cat);
21             } elsif($cat) {
22 2         13 return Log::Log4perl->get_logger($cat);
23             } else {
24 11         410 return $self->logger;
25             }
26             }
27              
28             1;
29              
30             __END__
31              
32             =head1 NAME
33              
34             MooseX::Log::Log4perl - A Logging Role for Moose based on Log::Log4perl
35              
36             =head1 SYNOPSIS
37              
38             package MyApp;
39             use Moose;
40            
41             with 'MooseX::Log::Log4perl';
42            
43             sub something {
44             my ($self) = @_;
45             $self->log->debug("started bar"); ### logs with default class catergory "MyApp"
46             ...
47             $self->log('special')->info('bar'); ### logs with category "special"
48             ...
49             $self->log('.special')->info('bar'); ### logs with category "MyApp.special"
50             $self->log('::special')->info('bar');### logs with category "MyApp.special"
51             }
52              
53             =head1 DESCRIPTION
54              
55             A logging role building a very lightweight wrapper to L<Log::Log4perl> for use with your L<Moose> classes.
56             The initialization of the Log4perl instance must be performed prior to logging the first log message.
57             Otherwise the default initialization will happen, probably not doing the things you expect.
58              
59             For compatibility the C<logger> attribute can be accessed to use a common interface for application logging.
60              
61             Using the logger within a class is as simple as consuming a role:
62              
63             package MyClass;
64             use Moose;
65             with 'MooseX::Log::Log4perl';
66            
67             sub dummy {
68             my $self = shift;
69             $self->log->info("Dummy log entry");
70             }
71              
72             The logger needs to be setup before using the logger, which could happen in the main application:
73              
74             package main;
75             use Log::Log4perl qw(:easy);
76             use MyClass;
77            
78             BEGIN { Log::Log4perl->easy_init() };
79            
80             my $myclass = MyClass->new();
81             $myclass->log->info("In my class"); # Access the log of the object
82             $myclass->dummy; # Will log "Dummy log entry"
83              
84             =head1 EVEN SIMPLER USE
85              
86             For simple logging needs use L<MooseX::Log::Log4perl::Easy> to directly add log_<level> methods to your class
87             instance.
88              
89             $self->log_info("Dummy");
90              
91              
92             =head1 USING WITH MOUSE INSTEAD OF MOOSE
93              
94             As this module is using L<Any::Moose>, you can use it with Mouse instead of Moose too.
95              
96             This will allow to simple use it as documented above in a Mouse based application, like shown in the example below:
97              
98             This is your class consuming the MooseX::Log::Log4perl role.
99              
100             package MyCat;
101             use Mouse;
102            
103             with 'MooseX::Log::Log4perl';
104            
105             sub catch_it {
106             my $self = shift;
107             $self->log->debug("Say Miau");
108             }
109              
110             Which can be simply used in your main application then.
111              
112             package main;
113             use MyCat;
114             use Log::Log4perl qw(:easy);
115             BEGIN { Log::Log4perl->easy_init() };
116            
117             my $log = Log::Log4perl->get_logger();
118             $log->info("Application startup...");
119             MyCat->new()->catch_it(); ### Will log "Dummy dodo"
120              
121              
122             =head1 ACCESSORS
123              
124             =head2 logger
125              
126             The C<logger> attribute holds the L<Log::Log4perl> object that implements all logging methods for the
127             defined log levels, such as C<debug> or C<error>. As this method is defined also in other logging
128             roles/systems like L<MooseX::Log::LogDispatch> this can be thought of as a common logging interface.
129              
130             package MyApp::View::JSON;
131              
132             extends 'MyApp::View';
133             with 'MooseX:Log::Log4perl';
134              
135             sub bar {
136             $self->logger->info("Everything fine so far"); # logs a info message
137             $self->logger->debug("Something is fishy here"); # logs a debug message
138             }
139              
140              
141             =head2 log([$category])
142              
143             Basically the same as logger, but also allowing to change the log category
144             for this log message. If the category starts with a C<+>, we pre-pend the current
145             class (what would have been the category if you didn't specify one).
146              
147             if ($myapp->log->is_debug()) {
148             $myapp->log->debug("Woot"); # category is class myapp
149             }
150             $myapp->log("TempCat")->info("Foobar"); # category TempCat
151             $myapp->log->info("Grumble"); # category class again myapp
152             $myapp->log(".TempCat")->info("Foobar"); # category myapp.TempCat
153             $myapp->log("::TempCat")->info("Foobar"); # category myapp.TempCat
154              
155             =head1 SEE ALSO
156              
157             L<Log::Log4perl>, L<Moose>, L<MooseX::LogDispatch>
158              
159             =head1 BUGS AND LIMITATIONS
160              
161             Please report any bugs or feature requests to
162             C<bug-moosex-log4perl@rt.cpan.org>, or through the web interface at
163             L<http://rt.cpan.org>.
164              
165             Or come bother us in C<#moose> on C<irc.perl.org>.
166              
167             =head1 AUTHOR
168              
169             Roland Lammel C<< <lammel@cpan.org> >>
170              
171             Inspired by the work by Chris Prather C<< <perigrin@cpan.org> >> and Ash
172             Berlin C<< <ash@cpan.org> >> on L<MooseX::LogDispatch>
173              
174             =head1 CONTRIBUTORS
175              
176             In alphabetical order:
177              
178             =over 2
179              
180             =item Michael Schilli C<< <m@perlmeister.com> >> for L<Log::Log4perl> and suggestions in the interface.
181              
182             =item Tim Bunce C<< <TIMB@cpan.org> >> for corrections in the L<MooseX::Log::Log4perl::Easy> module.
183              
184             =back
185              
186             =head1 LICENSE AND COPYRIGHT
187              
188             Copyright (c) 2008-2012, Roland Lammel C<< <lammel@cpan.org> >>, http://www.quikit.at
189              
190             This module is free software; you can redistribute it and/or
191             modify it under the same terms as Perl itself. See L<perlartistic>.
192