blib/lib/HPCI/Logger.pm untested

filter: Statementstmt 0.0% 0 / 98 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 / 141 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 has 'log_dir' => (
77 is => 'ro',
78 isa => Dir,
79 coerce => 1,
80 lazy => 1,
81 default => sub { my $self = shift; $self->group_dir },
82 );
83  
84 has 'log_file' => (
85 is => 'ro',
86 isa => 'Str',
87 lazy => 1,
88 default => "group.log",
89 );
90  
91 has 'log_path' => (
92 is => 'ro',
93 isa => File,
94 coerce => 1,
95 lazy => 1,
96 default => sub {
97 my $self = shift;
98 $self->log_dir->file( $self->log_file )
99 },
100 );
101  
102 has 'log_no_stdout' => (
103 is => 'ro',
104 isa => 'Bool',
105 default => $ENV{HPCI_LOG_NO_STDOUT} // '0',
106 );
107  
108 has 'log_no_file' => (
109 is => 'ro',
110 isa => 'Bool',
111 default => $ENV{HPCI_LOG_NO_FILE} // '0',
112 );
113  
114 subtype 'LogLevel',
115 as 'Num';
116  
117 coerce 'LogLevel', from 'Str', via { Log::Log4perl::Level::to_priority( uc $_ ) };
118  
119 has '_log' => (
120 is => 'rw',
121 isa => 'Log::Log4perl::Logger',
122 init_arg => undef,
123 );
124  
125 has 'log' => (
126 is => 'ro',
127 isa => 'Log::Log4perl::Logger',
128 lazy => 1,
129 trigger => \&_log_provided,
130 builder => '_default_logger',
131 );
132  
133 before 'BUILD' => sub {
134 my $self = shift;
135 $self->log;
136 $self->info( "Log fully initialized." );
137 };
138  
139 sub _log_provided {
140 my $self = shift;
141 $self->_init_stage(2);
142 }
143  
144 has '_init_stage' => (
145 is => 'rw',
146 isa => 'Num',
147 init_arg => undef,
148 default => 0,
149 );
150  
151 has '_log_stack' => (
152 is => 'ro',
153 isa => 'ArrayRef',
154 init_arg => undef,
155 default => sub { [] },
156 );
157  
158 sub _do_log {
159 my $self = shift;
160 my $stack = $self->_log_stack;
161 if ($self->_init_stage < 2) {
162 push @$stack, [ @_ ];
163 return;
164 }
165 $self->_do_a_log( @{ pop @$stack } ) while @$stack;
166 $self->_do_a_log( @_ );
167 }
168  
169 sub _do_a_log {
170 my $self = shift;
171 my $level = shift;
172 $self->log->$level( @_ );
173 }
174  
175 sub debug {
176 my $self = shift;
177 $self->_do_log( 'debug', @_ );
178 }
179  
180 sub info {
181 my $self = shift;
182 $self->_do_log( 'info', @_ );
183 }
184  
185 sub warn {
186 my $self = shift;
187 $self->_do_log( 'warn', @_ );
188 }
189  
190 sub error {
191 my $self = shift;
192 $self->_do_log( 'error', @_ );
193 }
194  
195 sub fatal {
196 my $self = shift;
197 $self->_do_log( 'fatal', @_ );
198 }
199  
200 has 'log_level' => (
201 is => 'rw',
202 isa => 'LogLevel',
203 lazy => 1,
204 coerce => 1,
205 trigger => \&_log_level_change,
206 default => 'info',
207 );
208  
209 # update log when log_level changes (but not during initialization)
210 sub _log_level_change {
211 my $self = shift;
212 my $new_level = shift;
213 $self->log->level($new_level) if @_;
214 }
215  
216 sub _default_logger {
217 my $self = shift;
218  
219 return $self->_log if $self->_init_stage;
220  
221 my $log = get_logger(__PACKAGE__.$self->_unique_name);
222 $self->_init_stage(1);
223 $self->_log($log);
224  
225 $log->level( $self->log_level );
226  
227 # Layouts - output formats for appenders. See here for specification:
228 # (http://search.cpan.org/~mschilli/Log-Log4perl-1.40/lib/Log/Log4perl/Layout/PatternLayout.pm)
229 my %layout = (
230 simple => "%d %6p> %m%n",
231 verbose => "%d (%10r) %6p> %m%n",
232 );
233 $_ = Log::Log4perl::Layout::PatternLayout->new($_) for values %layout;
234  
235 if ($self->log_no_file) {
236 $log->debug("No file logger initialized");
237 }
238 else {
239 my $file_appender = Log::Log4perl::Appender->new(
240 "Log::Log4perl::Appender::File",
241 filename => $self->log_path,
242 mode => 'append',
243 );
244 $file_appender->layout($layout{verbose});
245 $log->add_appender($file_appender);
246 $log->debug("File logger initialized");
247 }
248  
249 if ($self->log_no_stdout) {
250 $log->debug("No stdout logger initialized");
251 }
252 else {
253 my $stdout_appender = Log::Log4perl::Appender->new(
254 'Log::Log4perl::Appender::Screen',
255 stderr => 0,
256 utf8 => 1,
257 );
258 $stdout_appender->layout($layout{simple});
259 $log->add_appender($stdout_appender);
260 $log->debug("Stdout logger initialized");
261 }
262  
263 $self->_init_stage(2);
264 return $log;
265 }
266  
267 1;