line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
=head1 NAME |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
Log::Fine::Formatter - Log message formatting and sanitization |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 SYNOPSIS |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Provides a formatting facility for log messages |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Log::Fine::Handle; |
11
|
|
|
|
|
|
|
use Log::Fine::Formatter; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $handle = Log::Fine::Handle::Console->new(); |
14
|
|
|
|
|
|
|
my $formatter = Log::Fine::Formatter::Detailed->new( |
15
|
|
|
|
|
|
|
timestamp_format => "%Y-%m-%d %H:%M:%S" |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# By default, the handle will set its formatter to |
19
|
|
|
|
|
|
|
# Log::Fine::Formatter::Basic. If that's not what you want, set |
20
|
|
|
|
|
|
|
# it to preference. |
21
|
|
|
|
|
|
|
$handle->formatter($formatter); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Set the time-stamp to "YYYY-MM-DD HH:MM:SS" |
24
|
|
|
|
|
|
|
$formatter->timeStamp("%Y-%m-%d %H:%M:%S"); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# High resolution timestamps with milliseconds are |
27
|
|
|
|
|
|
|
# supported thus: |
28
|
|
|
|
|
|
|
my $hires_formatter = |
29
|
|
|
|
|
|
|
Log::Fine::Formatter::Basic->new( |
30
|
|
|
|
|
|
|
hires => 1, |
31
|
|
|
|
|
|
|
timestamp_format => "%H:%M:%S.%%millis%%", |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# Set the precision of the high resolution formatter |
35
|
|
|
|
|
|
|
my $fmtr = Log::Fine::Formatter::Basic->new( |
36
|
|
|
|
|
|
|
hires => 1, |
37
|
|
|
|
|
|
|
timestamp_format => "%H:%M:%S.%%millis%%", |
38
|
|
|
|
|
|
|
precision => 6 |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 DESCRIPTION |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
Base ancestral class for all formatters. All customized formatters |
44
|
|
|
|
|
|
|
must inherit from this class. The formatter class allows developers |
45
|
|
|
|
|
|
|
to adjust the time-stamp in a log message to a customizable |
46
|
|
|
|
|
|
|
strftime-compatible string without the tedious mucking about writing a |
47
|
|
|
|
|
|
|
formatter sub-class. By default, the time-stamp format is "%c". See |
48
|
|
|
|
|
|
|
L and the L man page on your system for |
49
|
|
|
|
|
|
|
further details. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head2 High Resolution Timestamps |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
High Resolution time stamps are generated using the L |
54
|
|
|
|
|
|
|
module. Depending on your distribution of perl, this may or may not |
55
|
|
|
|
|
|
|
be installed. Add the string "%%millis%%" (without the quotes) where |
56
|
|
|
|
|
|
|
you would like milliseconds displayed within your format. For example: |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$formatter->timeStamp("%H:%M:%S.%%millis%%"); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Note you I enable high resolution mode during Formatter |
61
|
|
|
|
|
|
|
construction as so: |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $formatter = Log::Fine::Formatter::Basic->new( hires => 1 ); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
By default, the time-stamp format for high resolution mode is |
66
|
|
|
|
|
|
|
"%H:%M:%S.%%millis%%". This can be changed via the L |
67
|
|
|
|
|
|
|
method or set during formatter construction. "%%millis%%" is a case |
68
|
|
|
|
|
|
|
insensitive value, thus "%%MILLIS%%" will work as well as |
69
|
|
|
|
|
|
|
"%%Millis%%". |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head3 Millisecond Precision |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Millisecond precision can be set on construction as so: |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $formatter = |
76
|
|
|
|
|
|
|
Log::Fine::Formatter::Basic->new( hires => 1, |
77
|
|
|
|
|
|
|
precision => 6 ); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
If not set, the default value of 5 will be used. Note that the |
80
|
|
|
|
|
|
|
precision hash element will be ignored unless hires is set. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 Using Log format templates |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
As of version 0.37, Log::Fine now supports log format templates. See |
85
|
|
|
|
|
|
|
L for details. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
18
|
|
|
18
|
|
3119
|
use strict; |
|
18
|
|
|
|
|
84
|
|
|
18
|
|
|
|
|
396
|
|
90
|
18
|
|
|
18
|
|
48
|
use warnings; |
|
18
|
|
|
|
|
20
|
|
|
18
|
|
|
|
|
459
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
package Log::Fine::Formatter; |
93
|
|
|
|
|
|
|
|
94
|
18
|
|
|
18
|
|
54
|
use base qw( Log::Fine ); |
|
18
|
|
|
|
|
14
|
|
|
18
|
|
|
|
|
1039
|
|
95
|
|
|
|
|
|
|
|
96
|
18
|
|
|
18
|
|
68
|
use Log::Fine::Logger; |
|
18
|
|
|
|
|
58
|
|
|
18
|
|
|
|
|
350
|
|
97
|
18
|
|
|
18
|
|
46
|
use POSIX qw( strftime ); |
|
18
|
|
|
|
|
18
|
|
|
18
|
|
|
|
|
83
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
our $VERSION = $Log::Fine::VERSION; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# Constant: LOG_TIMESTAMP_FORMAT, LOG_TIMESTAMP_FORMAT_PRECISE |
102
|
|
|
|
|
|
|
# LOG_TIMESTAMP_DEFAULT_PRECISION |
103
|
|
|
|
|
|
|
# |
104
|
|
|
|
|
|
|
# strftime(3)-compatible format string |
105
|
18
|
|
|
18
|
|
1126
|
use constant LOG_TIMESTAMP_FORMAT => "%c"; |
|
18
|
|
|
|
|
21
|
|
|
18
|
|
|
|
|
958
|
|
106
|
18
|
|
|
18
|
|
61
|
use constant LOG_TIMESTAMP_FORMAT_PRECISE => "%H:%M:%S.%%millis%%"; |
|
18
|
|
|
|
|
18
|
|
|
18
|
|
|
|
|
654
|
|
107
|
18
|
|
|
18
|
|
56
|
use constant LOG_TIMESTAMP_DEFAULT_PRECISION => 5; |
|
18
|
|
|
|
|
17
|
|
|
18
|
|
|
|
|
7366
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 METHODS |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 format |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Returns the formatted message. B be sub-classed! |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head3 Returns |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
The formatted string |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub format |
122
|
|
|
|
|
|
|
{ |
123
|
|
|
|
|
|
|
|
124
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
125
|
1
|
|
|
|
|
1
|
my $class = ref $self; |
126
|
|
|
|
|
|
|
|
127
|
1
|
50
|
|
|
|
4
|
if ($class eq 'Log::Fine::Formatter') { |
128
|
1
|
|
|
|
|
6
|
$self->_fatal("direct call to abstract method format()!"); |
129
|
|
|
|
|
|
|
} else { |
130
|
0
|
|
|
|
|
0
|
$self->_fatal("call to abstract method ${class}::format()"); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
} # format() |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head2 testFormat |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Special method used for unit tests only. |
138
|
|
|
|
|
|
|
I |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head3 Parameters |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=over |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item * level |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Level at which to log |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item * message |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Message to log |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=back |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head3 Returns |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
The formatted string |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub testFormat |
161
|
|
|
|
|
|
|
{ |
162
|
|
|
|
|
|
|
|
163
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
164
|
1
|
|
|
|
|
1
|
my $lvl = shift; |
165
|
1
|
|
|
|
|
1
|
my $msg = shift; |
166
|
|
|
|
|
|
|
|
167
|
1
|
|
|
|
|
3
|
return $self->format($lvl, $msg, 0); |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
} # testFormat() |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 timeStamp |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Getter/Setter for a L format string. |
174
|
|
|
|
|
|
|
If passed with an argument, sets the objects strftime compatible |
175
|
|
|
|
|
|
|
string. Otherwise, returns the objects format string. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head3 Parameters |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=over |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=item * string |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
B<[optional]> L compatible string to set |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=back |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head3 Returns |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
L compatible string |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=cut |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub timeStamp |
194
|
|
|
|
|
|
|
{ |
195
|
|
|
|
|
|
|
|
196
|
7
|
|
|
7
|
1
|
469
|
my $self = shift; |
197
|
7
|
|
|
|
|
6
|
my $str = shift; |
198
|
|
|
|
|
|
|
|
199
|
7
|
100
|
|
|
|
17
|
$self->{timestamp_format} = $str |
200
|
|
|
|
|
|
|
if (defined $str); |
201
|
|
|
|
|
|
|
|
202
|
7
|
|
|
|
|
25
|
return $self->{timestamp_format}; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
} # timeStamp() |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
# -------------------------------------------------------------------- |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
## |
209
|
|
|
|
|
|
|
# Initializer for this object |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
sub _init |
212
|
|
|
|
|
|
|
{ |
213
|
|
|
|
|
|
|
|
214
|
424
|
|
|
424
|
|
411
|
my $self = shift; |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
# Perform any necessary upper class initializations |
217
|
424
|
|
|
|
|
810
|
$self->SUPER::_init(); |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
# Verify that we can load the Time::HiRes module |
220
|
424
|
100
|
|
|
|
707
|
if ($self->{hires}) { |
221
|
|
|
|
|
|
|
|
222
|
3
|
|
|
|
|
131
|
eval "require Time::HiRes"; |
223
|
3
|
50
|
|
|
|
9
|
$self->_fatal("Time::HiRes failed to load. " . "Please install Time::HiRes via CPAN : $@") |
224
|
|
|
|
|
|
|
if $@; |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# Set {timestamp_format} to default high precision |
227
|
|
|
|
|
|
|
# format if necessary. |
228
|
|
|
|
|
|
|
$self->{timestamp_format} = $self->LOG_TIMESTAMP_FORMAT_PRECISE |
229
|
|
|
|
|
|
|
unless (defined $self->{timestamp_format} |
230
|
3
|
100
|
66
|
|
|
23
|
and $self->{timestamp_format} =~ /\w+/); |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
# Set {precision} to default if necessary |
233
|
|
|
|
|
|
|
$self->{precision} = $self->LOG_TIMESTAMP_DEFAULT_PRECISION |
234
|
|
|
|
|
|
|
unless (defined $self->{precision} |
235
|
3
|
100
|
66
|
|
|
17
|
and $self->{precision} =~ /^\d+$/); |
236
|
|
|
|
|
|
|
|
237
|
3
|
|
|
|
|
33
|
$self->{_precision_format_str} = "%.0" . $self->{precision} . "f"; |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
} else { |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# Set {timestamp_format} to the default if necessary |
242
|
|
|
|
|
|
|
$self->{timestamp_format} = $self->LOG_TIMESTAMP_FORMAT |
243
|
|
|
|
|
|
|
unless (defined $self->{timestamp_format} |
244
|
421
|
100
|
66
|
|
|
3610
|
and $self->{timestamp_format} =~ /\w+/); |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
424
|
|
|
|
|
610
|
return $self; |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
} # _init() |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
## |
253
|
|
|
|
|
|
|
# Formats the time string returned |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
sub _formatTime |
256
|
|
|
|
|
|
|
{ |
257
|
35
|
|
|
35
|
|
35
|
my $seconds; |
258
|
|
|
|
|
|
|
|
259
|
35
|
|
|
|
|
34
|
my $self = shift; |
260
|
35
|
|
|
|
|
43
|
my $fmt = $self->{timestamp_format}; |
261
|
|
|
|
|
|
|
|
262
|
35
|
100
|
|
|
|
71
|
if ($self->{hires}) { |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
# use Time::HiRes to get seconds and milliseconds |
265
|
5
|
|
|
|
|
43
|
my $time = sprintf($self->{_precision_format_str}, &Time::HiRes::time); |
266
|
5
|
|
|
|
|
17
|
my @t = split /\./, $time; |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
# and format |
269
|
5
|
|
|
|
|
22
|
$fmt =~ s/%%millis%%/$t[1]/ig; |
270
|
5
|
|
|
|
|
8
|
$seconds = $time; |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
} else { |
273
|
30
|
|
|
|
|
55
|
$seconds = time; |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
|
276
|
35
|
|
|
|
|
1818
|
return strftime($fmt, localtime($seconds)); |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
} # _formatTime() |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head1 BUGS |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
283
|
|
|
|
|
|
|
C, or through the web interface at |
284
|
|
|
|
|
|
|
L. |
285
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
286
|
|
|
|
|
|
|
your bug as I make changes. |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=head1 SUPPORT |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
perldoc Log::Fine |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
You can also look for information at: |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=over 4 |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
L |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=item * CPAN Ratings |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
L |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
L |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=item * Search CPAN |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
L |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
=back |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=head1 AUTHOR |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
Christopher M. Fuhrman, C<< >> |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
=head1 SEE ALSO |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
L, L, L, L |
323
|
|
|
|
|
|
|
|
324
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
Copyright (c) 2008-2010, 2013 Christopher M. Fuhrman, |
327
|
|
|
|
|
|
|
All rights reserved. |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
This program is free software licensed under the... |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
The BSD License |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
The full text of the license can be found in the |
334
|
|
|
|
|
|
|
LICENSE file included with this module. |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
=cut |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
1; # End of Log::Fine::Formatter |
339
|
|
|
|
|
|
|
|