blib/lib/XAS/Lib/Modules/Log.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 79 CC 7 SCAR 29.2
CC
7
Cov
0%
init
42.0
level
6.0
build
2.0
CRAP 18.5
1 package XAS::Lib::Modules::Log;
2  
3 our $VERSION = '0.01';
4  
5 use DateTime;
6 use Config::IniFiles;
7 use XAS::Constants ':logging';
8 use Params::Validate 'HASHREF';
9  
10 use XAS::Class
11 version => $VERSION,
12 base => 'XAS::Singleton',
13 filesystem => 'File',
14 vars => {
15 LEVELS => {
16 trace => 0,
17 debug => 0,
18 info => 1,
19 warn => 1,
20 error => 1,
21 fatal => 1,
22 },
23 PARAMS => {
24 -filename => { optional => 1 },
25 -process => { optional => 1, default => 'XAS' },
26 -levels => { optional => 1, default => {}, type => HASHREF },
27 -type => { optional => 1, default => 'console', regex => LOG_TYPES },
28 -facility => { optional => 1, default => 'local7', regex => LOG_FACILITY },
29 }
30 },
31 ;
32  
33 #use Data::Dumper;
34  
35 my $mixins = {
36 console => 'XAS::Lib::Modules::Log::Console',
37 file => 'XAS::Lib::Modules::Log::File',
38 logstash => 'XAS::Lib::Modules::Log::Logstash',
39 syslog => 'XAS::Lib::Modules::Log::Syslog',
40 };
41  
42 # ------------------------------------------------------------------------
43 # Public Methods
44 # ------------------------------------------------------------------------
45  
46 sub level {
47 my $self = shift;
48  
49 my ($level, $action) = $self->validate_params(\@_, [
50 { regex => LOG_LEVELS },
51 { optional => 1, default => undef , regex => qr/0|1/ },
52 ]);
53  
54 $self->{$level} = $action if (defined($action));
55  
56 return $self->{$level};
57  
58 }
59  
60 sub build {
61 my $self = shift;
62  
63 my ($level, $message) = $self->validate_params(\@_, [
64 { regex => LOG_LEVELS },
65 1
66 ]);
67  
68 return {
69 hostname => $self->env->host,
70 datetime => DateTime->now(time_zone => 'local'),
71 process => $self->process,
72 pid => $$,
73 msgid => 0,
74 facility => $self->facility,
75 priority => $level,
76 message => $message,
77 };
78  
79 }
80  
81 # ------------------------------------------------------------------------
82 # Private Methods
83 # ------------------------------------------------------------------------
84  
85 sub init {
86 my $class = shift;
87  
88 my $self = $class->SUPER::init(@_);
89 my $type = $self->type();
90  
91 if (defined($self->{filename})) {
92  
93 $self->{filename} = File($self->{filename});
94  
95 }
96  
97 # populate $self for each level using the
98 # value in $self->levels, or the default in $LEVELS
99  
100 my $l = $self->class->hash_vars( LEVELS => $self->levels );
101  
102 while (my ($level, $default) = each %$l) {
103  
104 $self->level($level, $default);
105  
106 }
107  
108 # autogenerate some methods, saves typing
109  
110 foreach my $level (keys %$LEVELS) {
111  
112 $self->class->methods($level => sub {
113 my $self = shift;
114  
115 return $self->{$level} unless @_;
116  
117 if ($self->{$level}) {
118  
119 my $args = $self->build("$level", join(" ", @_));
120 $self->output($args);
121  
122 }
123  
124 });
125  
126 $self->class->methods($level . '_msg' => sub {
127 my $self = shift;
128  
129 return $self->{$level} unless @_;
130  
131 if ($self->{$level}) {
132  
133 my $args = $self->build("$level", $self->message(@_));
134 $self->output($args);
135  
136 }
137  
138 });
139  
140 }
141  
142 # load and initialize our output mixin
143  
144 $self->class->mixin($mixins->{$type});
145 $self->init_log();
146  
147 return $self;
148  
149 }
150  
151 sub DESTROY {
152 my $self = shift;
153  
154 $self->destroy();
155  
156 }
157  
158 1;
159  
160 __END__
161  
162 =head1 NAME
163  
164 XAS::Lib::Modules::Log - A class for logging in the XAS Environment
165  
166 =head1 SYNOPSIS
167  
168 use XAS::Lib::Modules::Log;
169  
170 my $log = XAS::Lib::Modules::Log->new();
171  
172 $log->debug('a debug message');
173 $log->info('an info message');
174 $log->warn('a warning message');
175 $log->error('an error message');
176 $log->fatal('a fatal error message');
177 $log->trace('a tracing message');
178  
179 =head1 DESCRIPTION
180  
181 This module defines a simple logger for messages
182 generated by an application. It is intentionally very simple in design,
183 providing the bare minimum in functionality with the possibility for
184 extension by sub-classing.
185  
186 =head1 METHODS
187  
188 =head2 new
189  
190 This will initialize the base object. It takes the following parameters:
191  
192 =over 4
193  
194 =item B<-type>
195  
196 The type of the log. This can be "console", "file", "logstash" or "syslog".
197 Defaults to "console". Which means all logging goes to the current terminal.
198  
199 =item B<-filename>
200  
201 The name of the log file. This only is relevant if the log type is "file".
202  
203 =item B<-facility>
204  
205 The facility of the log message. Primarily has meaning when using a log type of
206 "logstash" or "syslog". The following have been defined and follows the syslog
207 standard.
208
209 auth, authpriv, cron, daemon, ftp,
210 local[0-7], lpr, mail, news, user, uucp
211  
212 Defaults to "local7".
213  
214 =item B<-process>
215  
216 The name of the process. Defaults to "XAS", which is not to useful.
217  
218 =item B<-levels>
219  
220 A hashref of values to set the internal logging level with.
221  
222 Example:
223  
224 my $log = XAS::Lib::Modules::Log->new(
225 -levels => {
226 debug => $self->debugging ? 1 : 0,
227 }
228 );
229  
230 This would set the debug level of logging, depending on the value of
231 $self->debugging.
232  
233 =back
234  
235 =head2 level($level, $boolean)
236  
237 This will query or toggle the log level. When toggled that particular level is set.
238 There is no hierarchy of log levels.
239  
240 =over 4
241  
242 =item B<$level>
243  
244 The log level to toggle. This can be one of the following:
245  
246 info, warn, error, fatal, debug, trace
247  
248 =item B<$boolean>
249  
250 An optional valve. It needs to be 0 or 1 to set the level.
251  
252 =back
253  
254 =head2 info($line)
255  
256 This method will log an entry with an level of "info".
257  
258 =over 4
259  
260 =item B<$line>
261  
262 The message to write out. This can be an array which will be joined with a
263 "space" separator.
264  
265 =back
266  
267 =head2 warn($line)
268  
269 This method will log an entry with an level of "warn".
270  
271 =over 4
272  
273 =item B<$line>
274  
275 The message to write out. This can be an array which will be joined with a
276 "space" separator.
277  
278 =back
279  
280 =head2 error($line)
281  
282 This method will log an entry with an level of "error".
283  
284 =over 4
285  
286 =item B<$line>
287  
288 The message to write out. This can be an array which will be joined with a
289 "space" separator.
290  
291 =back
292  
293 =head2 fatal($line)
294  
295 This method will log an entry with an level of "fatal".
296  
297 =over 4
298  
299 =item B<$line>
300  
301 The message to write out. This can be an array which will be joined with a
302 "space" separator.
303  
304 =back
305  
306 =head2 debug($line)
307  
308 This method will log an entry with an level of "debug". By default this level
309 is turned off.
310  
311 =over 4
312  
313 =item B<$line>
314  
315 The message to write out. This can be an array which will be joined with a
316 "space" separator.
317  
318 =back
319  
320 =head2 trace($line)
321  
322 This method will log an entry with an level of "trace".
323  
324 =over 4
325  
326 =item B<$line>
327  
328 The line to write out. This can be an array which will be joined with a
329 "space" separator.
330  
331 =back
332  
333 =head2 info_msg($message, $line)
334  
335 This method will log an entry with an level of "info".
336  
337 =over 4
338  
339 =item B<$message>
340  
341 The message to apply line against. This should be defined in the package
342 variable $MESSAGE or in the message stanza in XAS::Class.
343  
344 =item B<$line>
345  
346 The line to write out. This can be an array which will be joined with a
347 "space" separator.
348  
349 =back
350  
351 =head2 warn_msg($message, $line)
352  
353 This method will log an entry with an level of "warn".
354  
355 =over 4
356  
357 =item B<$message>
358  
359 The message to apply line against. This should be defined in the package
360 variable $MESSAGE or in the message stanza in XAS::Class.
361  
362 =item B<$line>
363  
364 The line to write out. This can be an array which will be joined with a
365 "space" separator.
366  
367 =back
368  
369 =head2 error_msg($message, $line)
370  
371 This method will log an entry with an level of "error".
372  
373 =over 4
374  
375 =item B<$message>
376  
377 The message to apply line against. This should be defined in the package
378 variable $MESSAGE or in the message stanza in XAS::Class.
379  
380 =item B<$line>
381  
382 The line to write out. This can be an array which will be joined with a
383 "space" separator.
384  
385 =back
386  
387 =head2 fatal_msg($message, $line)
388  
389 This method will log an entry with an level of "fatal".
390  
391 =over 4
392  
393 =item B<$message>
394  
395 The message to apply line against. This should be defined in the package
396 variable $MESSAGE or in the message stanza in XAS::Class.
397  
398 =item B<$line>
399  
400 The line to write out. This can be an array which will be joined with a
401 "space" separator.
402  
403 =back
404  
405 =head2 debug_msg($message, $line)
406  
407 This method will log an entry with an level of "debug".
408  
409 =over 4
410  
411 =item B<$message>
412  
413 The message to apply line against. This should be defined in the package
414 variable $MESSAGE or in the message stanza in XAS::Class.
415  
416 =item B<$line>
417  
418 The line to write out. This can be an array which will be joined with a
419 "space" separator.
420  
421 =back
422  
423 =head2 trace_msg($message, $line)
424  
425 This method will log an entry with an level of "trace".
426  
427 =over 4
428  
429 =item B<$message>
430  
431 The message to apply line against. This should be defined in the package
432 variable $MESSAGE or in the message stanza in XAS::Class.
433  
434 =item B<$line>
435  
436 The line to write out. This can be an array which will be joined with a
437 "space" separator.
438  
439 =back
440  
441 =head1 SEE ALSO
442  
443 =over 4
444  
445 =item L<XAS|XAS>
446  
447 =back
448  
449 =head1 AUTHOR
450  
451 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
452  
453 =head1 COPYRIGHT AND LICENSE
454  
455 Copyright (C) 2014 Kevin L. Esteb
456  
457 This library is free software; you can redistribute it and/or modify
458 it under the same terms as Perl itself, either Perl version 5.8.8 or,
459 at your option, any later version of Perl 5 you may have available.
460  
461 See L<http://dev.perl.org/licenses/> for more information.
462  
463 =cut