blib/lib/HPCI/Logger.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 142 CC 6 SCAR 17.3
CC
6
Cov
0%
_default_logger
20.0
_do_log
6.0
_log_level_change
6.0
CRAP 5.7
1 package HPCI::Logger;
2  
3 use namespace::autoclean;
4  
5 use Moose::Role;
6 use Moose::Util::TypeConstraints;
7  
8 use autodie;
9 use Carp;
10  
11 use Log::Log4perl qw(:easy get_logger);
12 use Log::Log4perl::Appender::Screen;
13 use Log::Log4perl::Level;
14 use MooseX::Types::Path::Class qw(Dir File);
15  
16 =head1 NAME
17  
18 HPCI::Logger - This role gives the consumer a simple logger attribute
19  
20 =head1 DESCRIPTION
21  
22 Defines a log attribute which handles the logging methods
23 (debug, info, warn, error, fatal). If a log attribute (of type
24 Log::Log4perl::Logger) is not provided, one will be created
25 automatically.
26  
27 Also allows specifying the directory/file that will be used for
28 the automatically-created log (if not log was provided), and the
29 level of message that will be logged.
30  
31 =head1 Attributes
32  
33 =over 4
34  
35 =item * log - A Log::Log4perl object
36  
37 Used to log all cluster-control activities of HPCI.
38  
39 If log is not provided, one will be created, using the log_dir and
40 log_file attributes, or the log_path attribute to specify the file
41 location where the log will be written.
42  
43 If a log B<is> provided, then HPCI log messages can be put into
44 the same log as other logged aspects of the calling program.
45  
46 =item * log_path - path to file where the log will be written
47  
48 Optional, only used if no log was explicitly provided. If this
49 attribute is not provided, the following two attributes are used.
50  
51 =item * log_dir - directory where the log will be written
52  
53 Optional, only used if no log was explicitly provided. Default value
54 is B<group_dir>
55  
56 =item * log_file - filename where the log will be written
57  
58 Optional, only used if no log was explicitly provided. Default value
59 is: "group.log"
60  
61 =item * log_level - set the log level
62  
63 Accepts a Log4perl log level value, or a string which can be
64 converted to such a value. the default is "info"
65  
66 =item * log_no_stderr, log_no_file - suppress default logging to stderr or log file
67  
68 Normally, the default log is written to both stderr and to the log file.
69 Either of those can be suppressed by setting the corresponding attribute to a true value.
70 These attributes have no effect if the user provides their own logger instead of using the default one.
71  
72 =back
73  
74 =cut
75  
76 requires qw(group_dir name);
77  
78 has 'log_dir' => (
79 is => 'ro',
80 isa => Dir,
81 coerce => 1,
82 lazy => 1,
83 default => sub { my $self = shift; $self->group_dir },
84 );
85  
86 has 'log_file' => (
87 is => 'ro',
88 isa => 'Str',
89 lazy => 1,
90 default => "group.log",
91 );
92  
93 has 'log_path' => (
94 is => 'ro',
95 isa => File,
96 coerce => 1,
97 lazy => 1,
98 default => sub {
99 my $self = shift;
100 $self->log_dir->file( $self->log_file )
101 },
102 );
103  
104 has 'log_no_stdout' => (
105 is => 'ro',
106 isa => 'Bool',
107 default => $ENV{HPCI_LOG_NO_STDOUT} // '0',
108 );
109  
110 has 'log_no_file' => (
111 is => 'ro',
112 isa => 'Bool',
113 default => $ENV{HPCI_LOG_NO_FILE} // '0',
114 );
115  
116 subtype 'LogLevel',
117 as 'Num';
118  
119 coerce 'LogLevel', from 'Str', via { Log::Log4perl::Level::to_priority( uc $_ ) };
120  
121 has '_log' => (
122 is => 'rw',
123 isa => 'Log::Log4perl::Logger',
124 init_arg => undef,
125 );
126  
127 has 'log' => (
128 is => 'ro',
129 isa => 'Log::Log4perl::Logger',
130 lazy => 1,
131 trigger => \&_log_provided,
132 builder => '_default_logger',
133 );
134  
135 before 'BUILD' => sub {
136 my $self = shift;
137 $self->log;
138 $self->info( "Log fully initialized." );
139 };
140  
141 sub _log_provided {
142 my $self = shift;
143 $self->_init_stage(2);
144 }
145  
146 has '_init_stage' => (
147 is => 'rw',
148 isa => 'Num',
149 init_arg => undef,
150 default => 0,
151 );
152  
153 has '_log_stack' => (
154 is => 'ro',
155 isa => 'ArrayRef',
156 init_arg => undef,
157 default => sub { [] },
158 );
159  
160 sub _do_log {
161 my $self = shift;
162 my $stack = $self->_log_stack;
163 if ($self->_init_stage < 2) {
164 push @$stack, [ @_ ];
165 return;
166 }
167 $self->_do_a_log( @{ pop @$stack } ) while @$stack;
168 $self->_do_a_log( @_ );
169 }
170  
171 sub _do_a_log {
172 my $self = shift;
173 my $level = shift;
174 $self->log->$level( @_ );
175 }
176  
177 sub debug {
178 my $self = shift;
179 $self->_do_log( 'debug', @_ );
180 }
181  
182 sub info {
183 my $self = shift;
184 $self->_do_log( 'info', @_ );
185 }
186  
187 sub warn {
188 my $self = shift;
189 $self->_do_log( 'warn', @_ );
190 }
191  
192 sub error {
193 my $self = shift;
194 $self->_do_log( 'error', @_ );
195 }
196  
197 sub fatal {
198 my $self = shift;
199 $self->_do_log( 'fatal', @_ );
200 }
201  
202 has 'log_level' => (
203 is => 'rw',
204 isa => 'LogLevel',
205 lazy => 1,
206 coerce => 1,
207 trigger => \&_log_level_change,
208 default => 'info',
209 );
210  
211 # update log when log_level changes (but not during initialization)
212 sub _log_level_change {
213 my $self = shift;
214 my $new_level = shift;
215 $self->log->level($new_level) if @_;
216 }
217  
218 sub _default_logger {
219 my $self = shift;
220  
221 return $self->_log if $self->_init_stage;
222  
223 my $log = get_logger(__PACKAGE__.$self->_unique_name);
224 $self->_init_stage(1);
225 $self->_log($log);
226  
227 $log->level( $self->log_level );
228  
229 # Layouts - output formats for appenders. See here for specification:
230 # (http://search.cpan.org/~mschilli/Log-Log4perl-1.40/lib/Log/Log4perl/Layout/PatternLayout.pm)
231 my %layout = (
232 simple => "%d %6p> %m%n",
233 verbose => "%d (%10r) %6p> %m%n",
234 );
235 $_ = Log::Log4perl::Layout::PatternLayout->new($_) for values %layout;
236  
237 if ($self->log_no_file) {
238 $log->debug("No file logger initialized");
239 }
240 else {
241 my $file_appender = Log::Log4perl::Appender->new(
242 "Log::Log4perl::Appender::File",
243 filename => $self->log_path,
244 mode => 'append',
245 );
246 $file_appender->layout($layout{verbose});
247 $log->add_appender($file_appender);
248 $log->debug("File logger initialized");
249 }
250  
251 if ($self->log_no_stdout) {
252 $log->debug("No stdout logger initialized");
253 }
254 else {
255 my $stdout_appender = Log::Log4perl::Appender->new(
256 'Log::Log4perl::Appender::Screen',
257 stderr => 0,
258 utf8 => 1,
259 );
260 $stdout_appender->layout($layout{simple});
261 $log->add_appender($stdout_appender);
262 $log->debug("Stdout logger initialized");
263 }
264  
265 $self->_init_stage(2);
266 return $log;
267 }
268  
269 1;