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
|
|
6437
|
use strict; |
|
18
|
|
|
|
|
171
|
|
|
18
|
|
|
|
|
661
|
|
90
|
18
|
|
|
18
|
|
1350
|
use warnings; |
|
18
|
|
|
|
|
32
|
|
|
18
|
|
|
|
|
671
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
package Log::Fine::Formatter; |
93
|
|
|
|
|
|
|
|
94
|
18
|
|
|
18
|
|
96
|
use base qw( Log::Fine ); |
|
18
|
|
|
|
|
38
|
|
|
18
|
|
|
|
|
2258
|
|
95
|
|
|
|
|
|
|
|
96
|
18
|
|
|
18
|
|
116
|
use Log::Fine::Logger; |
|
18
|
|
|
|
|
118
|
|
|
18
|
|
|
|
|
589
|
|
97
|
18
|
|
|
18
|
|
114
|
use POSIX qw( strftime ); |
|
18
|
|
|
|
|
42
|
|
|
18
|
|
|
|
|
156
|
|
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
|
|
1881
|
use constant LOG_TIMESTAMP_FORMAT => "%c"; |
|
18
|
|
|
|
|
38
|
|
|
18
|
|
|
|
|
1404
|
|
106
|
18
|
|
|
18
|
|
98
|
use constant LOG_TIMESTAMP_FORMAT_PRECISE => "%H:%M:%S.%%millis%%"; |
|
18
|
|
|
|
|
48
|
|
|
18
|
|
|
|
|
900
|
|
107
|
18
|
|
|
18
|
|
117
|
use constant LOG_TIMESTAMP_DEFAULT_PRECISION => 5; |
|
18
|
|
|
|
|
48
|
|
|
18
|
|
|
|
|
15318
|
|
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
|
|
|
|
|
2
|
my $class = ref $self; |
126
|
|
|
|
|
|
|
|
127
|
1
|
50
|
|
|
|
5
|
if ($class eq 'Log::Fine::Formatter') { |
128
|
1
|
|
|
|
|
7
|
$self->_fatal("direct call to abstract method format()!"); |
129
|
|
|
|
|
|
|
} else { |
130
|
0
|
|
|
|
|
0
|
$self->_fatal("call to abstract method ${class}::format()"); |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# |
134
|
|
|
|
|
|
|
# NOT REACHED |
135
|
|
|
|
|
|
|
# |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
} # format() |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 testFormat |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Special method used for unit tests only. |
142
|
|
|
|
|
|
|
I |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head3 Parameters |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=over |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item * level |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Level at which to log |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item * message |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Message to log |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=back |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head3 Returns |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
The formatted string |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub testFormat |
165
|
|
|
|
|
|
|
{ |
166
|
|
|
|
|
|
|
|
167
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
168
|
1
|
|
|
|
|
2
|
my $lvl = shift; |
169
|
1
|
|
|
|
|
1
|
my $msg = shift; |
170
|
|
|
|
|
|
|
|
171
|
1
|
|
|
|
|
5
|
return $self->format($lvl, $msg, 0); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
} # testFormat() |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 timeStamp |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Getter/Setter for a L format string. |
178
|
|
|
|
|
|
|
If passed with an argument, sets the objects strftime compatible |
179
|
|
|
|
|
|
|
string. Otherwise, returns the objects format string. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head3 Parameters |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=over |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=item * string |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
B<[optional]> L compatible string to set |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=back |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head3 Returns |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
L compatible string |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=cut |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
sub timeStamp |
198
|
|
|
|
|
|
|
{ |
199
|
|
|
|
|
|
|
|
200
|
7
|
|
|
7
|
1
|
1031
|
my $self = shift; |
201
|
7
|
|
|
|
|
13
|
my $str = shift; |
202
|
|
|
|
|
|
|
|
203
|
7
|
100
|
|
|
|
23
|
$self->{timestamp_format} = $str |
204
|
|
|
|
|
|
|
if (defined $str); |
205
|
|
|
|
|
|
|
|
206
|
7
|
|
|
|
|
55
|
return $self->{timestamp_format}; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
} # timeStamp() |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
# -------------------------------------------------------------------- |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
## |
213
|
|
|
|
|
|
|
# Initializer for this object |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub _init |
216
|
|
|
|
|
|
|
{ |
217
|
|
|
|
|
|
|
|
218
|
424
|
|
|
424
|
|
890
|
my $self = shift; |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
# Perform any necessary upper class initializations |
221
|
424
|
|
|
|
|
1620
|
$self->SUPER::_init(); |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
# Verify that we can load the Time::HiRes module |
224
|
424
|
100
|
|
|
|
1897
|
if ($self->{hires}) { |
225
|
|
|
|
|
|
|
|
226
|
3
|
|
|
|
|
218
|
eval "require Time::HiRes"; |
227
|
3
|
50
|
|
|
|
18
|
$self->_fatal( "Time::HiRes failed to load. " |
228
|
|
|
|
|
|
|
. "Please install Time::HiRes via CPAN : $@") |
229
|
|
|
|
|
|
|
if $@; |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
# Set {timestamp_format} to default high precision |
232
|
|
|
|
|
|
|
# format if necessary. |
233
|
3
|
100
|
66
|
|
|
37
|
$self->{timestamp_format} = $self->LOG_TIMESTAMP_FORMAT_PRECISE |
234
|
|
|
|
|
|
|
unless (defined $self->{timestamp_format} |
235
|
|
|
|
|
|
|
and $self->{timestamp_format} =~ /\w+/); |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
# Set {precision} to default if necessary |
238
|
3
|
100
|
66
|
|
|
34
|
$self->{precision} = $self->LOG_TIMESTAMP_DEFAULT_PRECISION |
239
|
|
|
|
|
|
|
unless (defined $self->{precision} |
240
|
|
|
|
|
|
|
and $self->{precision} =~ /^\d+$/); |
241
|
|
|
|
|
|
|
|
242
|
3
|
|
|
|
|
15
|
$self->{_precision_format_str} = |
243
|
|
|
|
|
|
|
"%.0" . $self->{precision} . "f"; |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
} else { |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
# Set {timestamp_format} to the default if necessary |
248
|
421
|
100
|
66
|
|
|
2914
|
$self->{timestamp_format} = $self->LOG_TIMESTAMP_FORMAT |
249
|
|
|
|
|
|
|
unless (defined $self->{timestamp_format} |
250
|
|
|
|
|
|
|
and $self->{timestamp_format} =~ /\w+/); |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
|
254
|
424
|
|
|
|
|
1263
|
return $self; |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
} # _init() |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
## |
259
|
|
|
|
|
|
|
# Formats the time string returned |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
sub _formatTime |
262
|
|
|
|
|
|
|
{ |
263
|
35
|
|
|
35
|
|
56
|
my $seconds; |
264
|
|
|
|
|
|
|
|
265
|
35
|
|
|
|
|
60
|
my $self = shift; |
266
|
35
|
|
|
|
|
87
|
my $fmt = $self->{timestamp_format}; |
267
|
|
|
|
|
|
|
|
268
|
35
|
100
|
|
|
|
107
|
if ($self->{hires}) { |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
# use Time::HiRes to get seconds and milliseconds |
271
|
5
|
|
|
|
|
82
|
my $time = |
272
|
|
|
|
|
|
|
sprintf($self->{_precision_format_str}, &Time::HiRes::time); |
273
|
5
|
|
|
|
|
20
|
my @t = split /\./, $time; |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
# and format |
276
|
5
|
|
|
|
|
39
|
$fmt =~ s/%%millis%%/$t[1]/ig; |
277
|
5
|
|
|
|
|
16
|
$seconds = $time; |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
} else { |
280
|
30
|
|
|
|
|
117
|
$seconds = time; |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
|
283
|
35
|
|
|
|
|
4611
|
return strftime($fmt, localtime($seconds)); |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
} # _formatTime() |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=head1 BUGS |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
290
|
|
|
|
|
|
|
C, or through the web interface at |
291
|
|
|
|
|
|
|
L. |
292
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
293
|
|
|
|
|
|
|
your bug as I make changes. |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=head1 SUPPORT |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
perldoc Log::Fine |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
You can also look for information at: |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=over 4 |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
L |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=item * CPAN Ratings |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
L |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
L |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=item * Search CPAN |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
L |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
=back |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=head1 AUTHOR |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
Christopher M. Fuhrman, C<< >> |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
=head1 SEE ALSO |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
L, L, L, L |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
332
|
|
|
|
|
|
|
|
333
|
|
|
|
|
|
|
Copyright (c) 2008-2010, 2013 Christopher M. Fuhrman, |
334
|
|
|
|
|
|
|
All rights reserved. |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
This program is free software licensed under the... |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
The BSD License |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
The full text of the license can be found in the |
341
|
|
|
|
|
|
|
LICENSE file included with this module. |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
=cut |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
1; # End of Log::Fine::Formatter |
346
|
|
|
|
|
|
|
|