File Coverage

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


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