line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package XAS::Lib::Log; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
801
|
use DateTime; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
6
|
1
|
|
|
1
|
|
3
|
use XAS::Constants ':logging HASHREF'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use XAS::Class |
9
|
1
|
|
|
|
|
7
|
version => $VERSION, |
10
|
|
|
|
|
|
|
base => 'XAS::Singleton', |
11
|
|
|
|
|
|
|
accessors => 'logger', |
12
|
|
|
|
|
|
|
filesystem => 'File', |
13
|
|
|
|
|
|
|
utils => ':boolean :validation load_module', |
14
|
|
|
|
|
|
|
vars => { |
15
|
|
|
|
|
|
|
LEVELS => { |
16
|
|
|
|
|
|
|
trace => 0, |
17
|
|
|
|
|
|
|
debug => 0, |
18
|
|
|
|
|
|
|
info => 1, |
19
|
|
|
|
|
|
|
warn => 1, |
20
|
|
|
|
|
|
|
error => 1, |
21
|
|
|
|
|
|
|
fatal => 1, |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
} |
24
|
1
|
|
|
1
|
|
162
|
; |
|
1
|
|
|
|
|
2
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
#use Data::Dumper; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
29
|
|
|
|
|
|
|
# Public Methods |
30
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub level { |
33
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
34
|
|
|
|
|
|
|
my ($level, $action) = validate_params(\@_, [ |
35
|
|
|
|
|
|
|
{ regex => LOG_LEVELS }, |
36
|
|
|
|
|
|
|
{ optional => 1, default => undef , |
37
|
|
|
|
|
|
|
callbacks => { |
38
|
|
|
|
|
|
|
'must be a boolean value or undef' => sub { |
39
|
0
|
|
|
0
|
|
|
my $param = shift; |
40
|
0
|
0
|
|
|
|
|
return 1 unless (defined($param)); |
41
|
0
|
0
|
|
|
|
|
return 1 if (is_truthy($param)); |
42
|
0
|
0
|
|
|
|
|
return 1 if (is_falsey($param)); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
}, |
46
|
0
|
|
|
|
|
|
]); |
47
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
$self->{$level} = $action if (defined($action)); |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
return $self->{$level}; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub build { |
55
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
56
|
0
|
|
|
|
|
|
my ($level, $message) = validate_params(\@_, [ |
57
|
|
|
|
|
|
|
{ regex => LOG_LEVELS }, |
58
|
|
|
|
|
|
|
1 |
59
|
|
|
|
|
|
|
]); |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
return { |
62
|
0
|
|
|
|
|
|
hostname => $self->env->host, |
63
|
|
|
|
|
|
|
datetime => DateTime->now(time_zone => 'local'), |
64
|
|
|
|
|
|
|
process => $self->env->script, |
65
|
|
|
|
|
|
|
pid => $$, |
66
|
|
|
|
|
|
|
msgid => 0, |
67
|
|
|
|
|
|
|
facility => $self->env->log_facility, |
68
|
|
|
|
|
|
|
priority => $level, |
69
|
|
|
|
|
|
|
message => $message, |
70
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub activate { |
75
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
my $type = $self->env->log_type; |
78
|
0
|
|
|
|
|
|
my $logger = 'XAS::Lib::Log::' . ucfirst($type); |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
load_module($logger); |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
|
$self->{'logger'} = $logger->new(); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
87
|
|
|
|
|
|
|
# Private Methods |
88
|
|
|
|
|
|
|
# ------------------------------------------------------------------------ |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub init { |
91
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
my $self = $class->SUPER::init(@_); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# populate $self for each level using the |
96
|
|
|
|
|
|
|
# value in $LEVELS |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
my $levels = $self->class->hash_vars('LEVELS'); |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
while (my ($level, $default) = each %$levels) { |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# set defined levels |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
$self->level($level, $default); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# autogenerate some methods, saves typing |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
$self->class->methods($level => sub { |
109
|
0
|
|
|
0
|
|
|
my $self = shift; |
110
|
|
|
|
|
|
|
|
111
|
0
|
0
|
|
|
|
|
if ($self->{$level}) { |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my $args = $self->build($level, join(" ", @_)); |
114
|
0
|
|
|
|
|
|
$self->logger->output($args); |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
}); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
$self->class->methods($level . '_msg' => sub { |
121
|
0
|
|
|
0
|
|
|
my $self = shift; |
122
|
|
|
|
|
|
|
|
123
|
0
|
0
|
|
|
|
|
if ($self->{$level}) { |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
my $args = $self->build($level, $self->message(@_)); |
126
|
0
|
|
|
|
|
|
$self->logger->output($args); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
}); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# load and initialize our output mixin |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
$self->activate(); |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
return $self; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
1; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
__END__ |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 NAME |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
XAS::Lib::Log - A class for logging in the XAS Environment |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 SYNOPSIS |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
use XAS::Lib::Log; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
my $log = XAS::Lib::Log->new(); |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
$log->debug('a debug message'); |
157
|
|
|
|
|
|
|
$log->info('an info message'); |
158
|
|
|
|
|
|
|
$log->warn('a warning message'); |
159
|
|
|
|
|
|
|
$log->error('an error message'); |
160
|
|
|
|
|
|
|
$log->fatal('a fatal error message'); |
161
|
|
|
|
|
|
|
$log->trace('a tracing message'); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 DESCRIPTION |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
This module defines a simple logger for messages |
166
|
|
|
|
|
|
|
generated by an application. It is intentionally very simple in design, |
167
|
|
|
|
|
|
|
providing the bare minimum in functionality with the possibility for |
168
|
|
|
|
|
|
|
extension by sub-classing. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 METHODS |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head2 new |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
This will initialize the base object. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head2 level($level, $boolean) |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
This will query or toggle the log level. When toggled that particular level is set. |
179
|
|
|
|
|
|
|
There is no hierarchy of log levels. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=over 4 |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=item B<$level> |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
The log level to toggle. This can be one of the following: |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
info, warn, error, fatal, debug, trace |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=item B<$boolean> |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
An optional valve. It needs to be 0 or 1 to set the level. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=back |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head2 info($line) |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
This method will log an entry with an level of "info". |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=over 4 |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=item B<$line> |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
The message to write out. This can be an array which will be joined with a |
204
|
|
|
|
|
|
|
"space" separator. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=back |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
=head2 warn($line) |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
This method will log an entry with an level of "warn". |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=over 4 |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=item B<$line> |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
The message to write out. This can be an array which will be joined with a |
217
|
|
|
|
|
|
|
"space" separator. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=back |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head2 error($line) |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
This method will log an entry with an level of "error". |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=over 4 |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=item B<$line> |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
The message to write out. This can be an array which will be joined with a |
230
|
|
|
|
|
|
|
"space" separator. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=back |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 fatal($line) |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
This method will log an entry with an level of "fatal". |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=over 4 |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=item B<$line> |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
The message to write out. This can be an array which will be joined with a |
243
|
|
|
|
|
|
|
"space" separator. |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=back |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head2 debug($line) |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
This method will log an entry with an level of "debug". By default this level |
250
|
|
|
|
|
|
|
is turned off. |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=over 4 |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
=item B<$line> |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
The message to write out. This can be an array which will be joined with a |
257
|
|
|
|
|
|
|
"space" separator. |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=back |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head2 trace($line) |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
This method will log an entry with an level of "trace". |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=over 4 |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=item B<$line> |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
The line to write out. This can be an array which will be joined with a |
270
|
|
|
|
|
|
|
"space" separator. |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=back |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=head2 info_msg($message, ...) |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
This method will log an entry with an level of "info". |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=over 4 |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=item B<$message> |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
The pre-defined message string. Usually in a message file. |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=item B<...> |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
The items to be used in the pre-defined message. |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=back |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
=head2 warn_msg($message, ...) |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
This method will log an entry with an level of "warn". |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
=over 4 |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=item B<$message> |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
The pre-defined message string. Usually in a message file. |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=item B<...> |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
The items to be used in the pre-defined message. |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=back |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=head2 error_msg($message, ...) |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
This method will log an entry with an level of "error". |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=over 4 |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
=item B<$message> |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
The pre-defined message string. Usually in a message file. |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=item B<...> |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
The items to be used in the pre-defined message. |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
=back |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
=head2 fatal_msg($message, ...) |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
This method will log an entry with an level of "fatal". |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=over 4 |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
=item B<$message> |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
The pre-defined message string. Usually in a message file. |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
=item B<...> |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
The items to be used in the pre-defined message. |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
=back |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
=head2 debug_msg($message, ...) |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
This method will log an entry with an level of "debug". |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
=over 4 |
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
=item B<$message> |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
The pre-defined message string. Usually in a message file. |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
=item B<...> |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
The items to be used in the pre-defined message. |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
=back |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
=head2 trace_msg($message, ...) |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
This method will log an entry with an level of "trace". |
357
|
|
|
|
|
|
|
|
358
|
|
|
|
|
|
|
=over 4 |
359
|
|
|
|
|
|
|
|
360
|
|
|
|
|
|
|
=item B<$message> |
361
|
|
|
|
|
|
|
|
362
|
|
|
|
|
|
|
The pre-defined message string. Usually in a message file. |
363
|
|
|
|
|
|
|
|
364
|
|
|
|
|
|
|
=item B<...> |
365
|
|
|
|
|
|
|
|
366
|
|
|
|
|
|
|
The items to be used in the pre-defined message. |
367
|
|
|
|
|
|
|
|
368
|
|
|
|
|
|
|
=back |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
=head1 SEE ALSO |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
=over 4 |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
=item L<XAS::Lib::Log::Console|XAS::Lib::Log::Console> |
375
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
=item L<XAS::Lib::Log::File|XAS::Lib::Log::File> |
377
|
|
|
|
|
|
|
|
378
|
|
|
|
|
|
|
=item L<XAS::Lib::Log::Json|XAS::Lib::Log::Json> |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
=item L<XAS::Lib::Log::Syslog|XAS::Lib::Log::Syslog> |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
=item L<XAS|XAS> |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
=back |
385
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
=head1 AUTHOR |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> |
389
|
|
|
|
|
|
|
|
390
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
391
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
Copyright (c) 2012-2015 Kevin L. Esteb |
393
|
|
|
|
|
|
|
|
394
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
395
|
|
|
|
|
|
|
the terms of the Artistic License 2.0. For details, see the full text |
396
|
|
|
|
|
|
|
of the license at http://www.perlfoundation.org/artistic_license_2_0. |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
=cut |