File Coverage

blib/lib/MooX/Role/Logger.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 21 21 100.0


line stmt bran cond sub pod time code
1 2     2   59673 use strict;
  2         4  
  2         67  
2 2     2   8 use warnings;
  2         3  
  2         88  
3              
4             package MooX::Role::Logger;
5             # ABSTRACT: Provide logging via Log::Any
6             our $VERSION = '0.005'; # VERSION
7              
8 2     2   7 use Moo::Role;
  2         2  
  2         11  
9              
10 2     2   524 use Log::Any ();
  2         2  
  2         307  
11              
12             #pod =method _logger
13             #pod
14             #pod Returns a logging object. See L<Log::Any> for a list of logging methods it accepts.
15             #pod
16             #pod =cut
17              
18             has _logger => (
19             is => 'lazy',
20             isa => sub { ref( $_[0] ) =~ /^Log::Any/ }, # XXX too many options
21             init_arg => undef,
22             );
23              
24             sub _build__logger {
25 2     2   5079 my ($self) = @_;
26 2         10 return Log::Any->get_logger( category => "" . $self->_logger_category );
27             }
28              
29             has _logger_category => ( is => 'lazy', );
30              
31             #pod =method _build__logger_category
32             #pod
33             #pod Override to set the category used for logging. Defaults to the class name of
34             #pod the object (which could be a subclass). You can override to lock it to a
35             #pod particular name:
36             #pod
37             #pod sub _build__logger_category { __PACKAGE__ }
38             #pod
39             #pod =cut
40              
41 2     2   844 sub _build__logger_category { return ref $_[0] }
42              
43             1;
44              
45              
46             # vim: ts=4 sts=4 sw=4 et:
47              
48             __END__
49              
50             =pod
51              
52             =encoding UTF-8
53              
54             =head1 NAME
55              
56             MooX::Role::Logger - Provide logging via Log::Any
57              
58             =head1 VERSION
59              
60             version 0.005
61              
62             =head1 SYNOPSIS
63              
64             In your modules:
65              
66             package MyModule;
67             use Moose;
68             with 'MooX::Role::Logger';
69              
70             sub run {
71             my ($self) = @_;
72             $self->cry;
73             }
74              
75             sub cry {
76             my ($self) = @_;
77             $self->_logger->info("I'm sad");
78             }
79              
80             In your application:
81              
82             use MyModule;
83             use Log::Any::Adapter ('File', '/path/to/file.log');
84              
85             MyModule->run;
86              
87             =head1 DESCRIPTION
88              
89             This role provides universal logging via L<Log::Any>. The class using this
90             role doesn't need to know or care about the details of log configuration,
91             implementation or destination.
92              
93             Use it when you want your module to offer logging capabilities, but don't know
94             who is going to use your module or what kind of logging they will implement.
95             This role lets you do your part and leaves actual log setup and routing to
96             someone else.
97              
98             The application that ultimately uses your module can then choose to direct log
99             messages somewhere based on its own needs and configuration with
100             L<Log::Any::Adapter>.
101              
102             This role is based on L<Moo> so it should work with either L<Moo> or L<Moose>
103             based classes.
104              
105             =head1 USAGE
106              
107             =head2 Testing
108              
109             Testing with L<Log::Any> is pretty easy, thanks to L<Log::Any::Test>.
110             Just load that before L<Log::Any> loads and your log messages get
111             sent to a test adapter that includes testing methods:
112              
113             use Test::More 0.96;
114             use Log::Any::Test;
115             use Log::Any qw/$log/;
116              
117             use lib 't/lib';
118             use MyModule;
119              
120             MyModule->new->cry;
121             $log->contains_ok( qr/I'm sad/, "got log message" );
122              
123             done_testing;
124              
125             =head2 Customizing
126              
127             If you have a whole set of classes that should log with a single category,
128             create your own role and set the C<_build__logger_category> there:
129              
130             package MyLibrary::Role::Logger;
131             use Moo::Role;
132             with 'MooX::Role::Logger';
133              
134             sub _build__logger_category { "MyLibrary" }
135              
136             Then in your other classes, use your custom role:
137              
138             package MyLibrary::Foo;
139             use Moo;
140             with 'MyLibrary::Role::Logger'
141              
142             =head1 METHODS
143              
144             =head2 _logger
145              
146             Returns a logging object. See L<Log::Any> for a list of logging methods it accepts.
147              
148             =head2 _build__logger_category
149              
150             Override to set the category used for logging. Defaults to the class name of
151             the object (which could be a subclass). You can override to lock it to a
152             particular name:
153              
154             sub _build__logger_category { __PACKAGE__ }
155              
156             =head1 SEE ALSO
157              
158             Since MooX::Role::Logger is universal, you have to use it with one of
159             several L<Log::Any::Adapter> classes:
160              
161             =over 4
162              
163             =item *
164              
165             L<Log::Any::Adapter::File>
166              
167             =item *
168              
169             L<Log::Any::Adapter::Stderr>
170              
171             =item *
172              
173             L<Log::Any::Adapter::Stdout>
174              
175             =item *
176              
177             L<Log::Any::Adapter::ScreenColoredLevel>
178              
179             =item *
180              
181             L<Log::Any::Adapter::Dispatch>
182              
183             =item *
184              
185             L<Log::Any::Adapter::Syslog>
186              
187             =item *
188              
189             L<Log::Any::Adapter::Log4perl>
190              
191             =back
192              
193             These other logging roles are specific to particular logging packages, rather
194             than being universal:
195              
196             =over 4
197              
198             =item *
199              
200             L<MooseX::LazyLogDispatch>
201              
202             =item *
203              
204             L<MooseX::Log::Log4perl>
205              
206             =item *
207              
208             L<MooseX::LogDispatch>
209              
210             =item *
211              
212             L<MooseX::Role::LogHandler>
213              
214             =item *
215              
216             L<MooseX::Role::Loggable> (uses L<Log::Dispatchouli>)
217              
218             =item *
219              
220             L<Role::Log::Syslog::Fast>
221              
222             =back
223              
224             =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
225              
226             =head1 SUPPORT
227              
228             =head2 Bugs / Feature Requests
229              
230             Please report any bugs or feature requests through the issue tracker
231             at L<https://github.com/dagolden/MooX-Role-Logger/issues>.
232             You will be notified automatically of any progress on your issue.
233              
234             =head2 Source Code
235              
236             This is open source software. The code repository is available for
237             public review and contribution under the terms of the license.
238              
239             L<https://github.com/dagolden/MooX-Role-Logger>
240              
241             git clone https://github.com/dagolden/MooX-Role-Logger.git
242              
243             =head1 AUTHOR
244              
245             David Golden <dagolden@cpan.org>
246              
247             =head1 COPYRIGHT AND LICENSE
248              
249             This software is Copyright (c) 2013 by David Golden.
250              
251             This is free software, licensed under:
252              
253             The Apache License, Version 2.0, January 2004
254              
255             =cut