File Coverage

blib/lib/MooseX/LogDispatch.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package MooseX::LogDispatch;
2              
3 1     1   25110 use 5.008001;
  1         4  
  1         67  
4              
5             our $VERSION = '1.2002';
6              
7 1     1   491 use Moose::Role;
  0            
  0            
8             use Log::Dispatch::Config;
9             use MooseX::LogDispatch::ConfigMaker;
10             use Moose::Exporter;
11             use MooseX::LogDispatch::Logger;
12              
13             Moose::Exporter->setup_import_methods(
14             as_is => [ \&MooseX::LogDispatch::Logger::Logger ]
15             );
16              
17             use Moose::Util::TypeConstraints;
18              
19             my $ldc_type = subtype 'LogDispatchConfigurator' => as 'Object' => where { $_->isa('Log::Dispatch::Configurator') };
20              
21             coerce 'LogDispatchConfigurator'
22             => from 'Str' => via {
23             require Log::Dispatch::Configurator::AppConfig;
24             Log::Dispatch::Configurator::AppConfig->new($_)
25             }
26             => from 'HashRef' => via { return MooseX::LogDispatch::ConfigMaker->new($_) };
27              
28              
29             has logger => (
30             isa => 'Log::Dispatch',
31             is => 'rw',
32             lazy_build => 1,
33             );
34              
35             has use_logger_singleton => (
36             isa => "Bool",
37             is => "rw",
38             default => 0,
39             );
40              
41             sub _build_logger {
42             my $self = shift;
43              
44             unless ( Log::Dispatch::Config->__instance and $self->use_logger_singleton ) {
45             Log::Dispatch::Config->configure( $self->_build_configurator );
46             }
47              
48             return Log::Dispatch::Config->instance;
49             }
50              
51             sub _build_configurator {
52             my $self = shift;
53             my $meta = $self->meta;
54              
55             my $conf_method =
56             $self->can('log_dispatch_conf') ||
57             $self->can('config_filename');
58              
59             return $ldc_type->coercion->coerce($self->$conf_method)
60             if $conf_method;
61              
62             return MooseX::LogDispatch::ConfigMaker->new({
63             class => 'Log::Dispatch::Screen',
64             min_level => 'debug',
65             stderr => 1,
66             format => '[%p] %m at %F line %L%n',
67             });
68             }
69              
70              
71             1;
72             __END__
73              
74             =head1 NAME
75              
76             MooseX::LogDispatch - A Logging Role for Moose
77              
78             =head1 VERSION
79              
80             This document describes MooseX::LogDispatch version 1.1000
81              
82             =head1 SYNOPSIS
83              
84             package MyApp;
85             use Moose;
86             with 'MooseX::LogDispatch';
87             # or
88             # with 'MooseX::LogDispatch::Levels'
89            
90             # This is optional. Will log to screen if not provided
91             has log_dispatch_conf => (
92             is => 'ro',
93             lazy => 1,
94             default => sub {
95             my $self = shift;
96             My::Configurator->new( # <- you write this class!
97             file => $self->log_file,
98             debug => $self->debug,
99             );
100            
101             }
102             );
103              
104             # This is the same as the old FileBased config parameter to the role. If you
105             # prefer you could name the attribute 'config_filename' instead.
106             has log_dispatch_conf => (
107             is => 'ro',
108             lazy => 1,
109             default => "/path/to/my/logger.conf"
110             );
111              
112             # Here's another variant, using a Log::Dispatch::Configurator-style
113             # hashref to configure things without an explicit subclass
114             has log_dispatch_conf => (
115             is => 'ro',
116             isa => 'HashRef',
117             lazy => 1,
118             required => 1,
119             default => sub {
120             my $self = shift;
121             return $self->debug ?
122             {
123             class => 'Log::Dispatch::Screen',
124             min_level => 'debug',
125             stderr => 1,
126             format => '[%p] %m at %F line %L%n',
127             }
128             : {
129             class => 'Log::Dispatch::Syslog',
130             min_level => 'info',
131             facility => 'daemon',
132             ident => $self->daemon_name,
133             format => '[%p] %m',
134             };
135             },
136             );
137              
138              
139             sub foo {
140             my ($self) = @_;
141             $self->logger->debug("started foo");
142             ....
143             $self->logger->debug('ending foo');
144             }
145            
146             =head1 DESCRIPTION
147              
148             L<Log::Dispatch> role for use with your L<Moose> classes.
149              
150             =head1 ACCESSORS
151              
152             =head2 logger
153              
154             This is the main L<Log::Dispatch::Config> object that does all the work. It
155             has methods for each of the log levels, such as C<debug> or C<error>.
156              
157             =head2 log_dispatch_conf
158              
159             This is an optional attribute you can give to your class. If you define it as
160             a hashref value, that will be interpreted in the style of the configuration
161             hashrefs documented in L<Log::Dispatch::Config> documents where they show
162             examples of using a
163             L<PLUGGABLE CONFIGURATOR|Log::Dispatch::Configurator/PLUGGABLE CONFIGURATOR>
164             for pluggable configuration.
165              
166             You can also gain greater flexibility by defining your own complete
167             L<Log::Dispatch::Configurator> subclass and having your C<log_dispatch_config>
168             attribute be an instance of this class.
169              
170             If this attribute has a value of a string, it will be taken to by the path to
171             a config file for L<Log::Dispatch::Configurator::AppConfig>.
172              
173             By lazy-loading this attribute (C<< lazy => 1 >>), you can have the
174             configuration determined at runtime. This is nice if you want to change your
175             log format and/or destination at runtime based on things like
176             L<MooseX::Getopt> / L<MooseX::Daemonize> parameters.
177              
178             If you don't provide this attribute, we'll default to sending everything to
179             the screen in a reasonable debugging format.
180              
181             =head2 use_logger_singleton
182              
183             If this attribute has a true value, and L<Log::Dispatch::Config> has a
184             configured log instance, this will be used in preference to anything set via
185             C<log_dispatch_config>.
186              
187             The main use for this attribute is when you want to use this module in another
188             library module - i.e. the consumer of this role is not the end user. Setting
189             this attribute to true makes it much easier for the end user to configure
190             logging.
191              
192             Note: If you are using a class consuming this one as a role, and plan on
193             reinstantiating that class, its probably a good idea to set this to 1 to avoid
194             errors.
195              
196             =head1 SEE ALSO
197              
198             L<MooseX::LogDispatch::Levels>, L<Log::Dispatch::Configurator>,
199             L<Log::Dispatch::Config>, L<Log::Dispatch>.
200              
201             =head1 DEPRECATION NOTICE
202              
203             The old C<with Logger(...)> style has been deprecated in favour of just
204             using one of two roles and making the config much more flexible. As of
205             version 1.2000 of this module, attempting to use it will make your code die.
206              
207             =head1 BUGS AND LIMITATIONS
208              
209             Please report any bugs or feature requests to
210             C<bug-moosex-logdispatch@rt.cpan.org>, or through the web interface at
211             L<http://rt.cpan.org>.
212              
213             Or come bother us in C<#moose> on C<irc.perl.org>.
214              
215             =head1 AUTHOR
216              
217             Ash Berlin C<< <ash@cpan.org> >>
218             v1.2000 fixes by Mike Whitaker C<< <penfold@cpan.org> >>
219              
220             Based on work by Chris Prather C<< <perigrin@cpan.org> >>
221              
222             Thanks to Brandon Black C<< <blblack@gmail.com> >> for showing me a much nicer
223             way to configure things.
224              
225             =head1 LICENCE AND COPYRIGHT
226              
227             Some development sponsored by Takkle Inc.
228              
229             Copyright (c) 2007, Ash Berlin C<< <ash@cpan.org> >>. Some rights reserved.
230              
231             Copyright (c) 2007, Chris Prather C<< <perigrin@cpan.org> >>. Some rights
232             reserved.
233              
234             This module is free software; you can redistribute it and/or
235             modify it under the same terms as Perl itself. See L<perlartistic>.
236              
237