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

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