… | |
… | |
55 | |
55 | |
56 | The design goal behind this module was to keep it simple (and small), |
56 | The design goal behind this module was to keep it simple (and small), |
57 | but make it powerful enough to be potentially useful for any module, and |
57 | but make it powerful enough to be potentially useful for any module, and |
58 | extensive enough for the most common tasks, such as logging to multiple |
58 | extensive enough for the most common tasks, such as logging to multiple |
59 | targets, or being able to log into a database. |
59 | targets, or being able to log into a database. |
|
|
60 | |
|
|
61 | The module is also usable before AnyEvent itself is initialised, in which |
|
|
62 | case some of the functionality might be reduced. |
60 | |
63 | |
61 | The amount of documentation might indicate otherwise, but the runtime part |
64 | The amount of documentation might indicate otherwise, but the runtime part |
62 | of the module is still just below 300 lines of code. |
65 | of the module is still just below 300 lines of code. |
63 | |
66 | |
64 | =head1 LOGGING LEVELS |
67 | =head1 LOGGING LEVELS |
… | |
… | |
108 | |
111 | |
109 | use Carp (); |
112 | use Carp (); |
110 | use POSIX (); |
113 | use POSIX (); |
111 | |
114 | |
112 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
115 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
113 | use AnyEvent::Util (); |
116 | #use AnyEvent::Util (); need to load this in a delayed fashion, as it uses AE::log |
114 | |
117 | |
115 | our $VERSION = $AnyEvent::VERSION; |
118 | our $VERSION = $AnyEvent::VERSION; |
116 | |
119 | |
117 | our ($COLLECT, $FILTER, $LOG); |
120 | our ($COLLECT, $FILTER, $LOG); |
118 | |
121 | |
… | |
… | |
203 | info => 7, |
206 | info => 7, |
204 | debug => 8, |
207 | debug => 8, |
205 | trace => 9, |
208 | trace => 9, |
206 | ); |
209 | ); |
207 | |
210 | |
208 | sub now () { time } |
211 | our $TIME_EXACT; |
|
|
212 | |
|
|
213 | sub exact_time($) { |
|
|
214 | $TIME_EXACT = shift; |
|
|
215 | *_ts = $AnyEvent::MODEL |
|
|
216 | ? $TIME_EXACT ? \&AE::now : \&AE::time |
|
|
217 | : sub () { $TIME_EXACT ? do { require Time::HiRes; Time::HiRes::time () } : time }; |
|
|
218 | } |
|
|
219 | |
|
|
220 | BEGIN { |
|
|
221 | exact_time 0; |
|
|
222 | } |
209 | |
223 | |
210 | AnyEvent::post_detect { |
224 | AnyEvent::post_detect { |
211 | *now = \&AE::now; |
225 | exact_time $TIME_EXACT; |
212 | }; |
226 | }; |
213 | |
227 | |
214 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
228 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
215 | |
229 | |
216 | # time, ctx, level, msg |
230 | # time, ctx, level, msg |
… | |
… | |
249 | # now get raw message, unless we have it already |
263 | # now get raw message, unless we have it already |
250 | unless ($now) { |
264 | unless ($now) { |
251 | $format = $format->() if ref $format; |
265 | $format = $format->() if ref $format; |
252 | $format = sprintf $format, @args if @args; |
266 | $format = sprintf $format, @args if @args; |
253 | $format =~ s/\n$//; |
267 | $format =~ s/\n$//; |
254 | $now = now; |
268 | $now = _ts; |
255 | }; |
269 | }; |
256 | |
270 | |
257 | # format msg |
271 | # format msg |
258 | my $str = $ctx->[4] |
272 | my $str = $ctx->[4] |
259 | ? $ctx->[4]($now, $_[0], $level, $format) |
273 | ? $ctx->[4]($now, $_[0], $level, $format) |
… | |
… | |
354 | |
368 | |
355 | $LOGGER{$logger+0} = $logger; |
369 | $LOGGER{$logger+0} = $logger; |
356 | |
370 | |
357 | _reassess $logger+0; |
371 | _reassess $logger+0; |
358 | |
372 | |
|
|
373 | require AnyEvent::Util; |
359 | my $guard = AnyEvent::Util::guard { |
374 | my $guard = AnyEvent::Util::guard (sub { |
360 | # "clean up" |
375 | # "clean up" |
361 | delete $LOGGER{$logger+0}; |
376 | delete $LOGGER{$logger+0}; |
362 | }; |
377 | }); |
363 | |
378 | |
364 | sub { |
379 | sub { |
365 | $guard if 0; # keep guard alive, but don't cause runtime overhead |
380 | $guard if 0; # keep guard alive, but don't cause runtime overhead |
366 | |
381 | |
367 | _log $ctx, $level, @_ |
382 | _log $ctx, $level, @_ |
… | |
… | |
372 | sub logger($;$) { |
387 | sub logger($;$) { |
373 | _logger |
388 | _logger |
374 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
389 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
375 | @_ |
390 | @_ |
376 | } |
391 | } |
|
|
392 | |
|
|
393 | =item AnyEvent::Log::exact_time $on |
|
|
394 | |
|
|
395 | By default, C<AnyEvent::Log> will use C<AE::now>, i.e. the cached |
|
|
396 | eventloop time, for the log timestamps. After calling this function with a |
|
|
397 | true value it will instead resort to C<AE::time>, i.e. fetch the current |
|
|
398 | time on each log message. This only makes a difference for event loops |
|
|
399 | that actually cache the time (such as L<EV> or L<AnyEvent::Loop>). |
|
|
400 | |
|
|
401 | Since C<AnyEvent::Log> has to work even before the L<AnyEvent> has been |
|
|
402 | initialised, this switch will also decide whether to use C<CORE::time> or |
|
|
403 | C<Time::HiRes::time> when logging a message before L<AnyEvent> becomes |
|
|
404 | available. |
377 | |
405 | |
378 | =back |
406 | =back |
379 | |
407 | |
380 | =head1 LOGGING CONTEXTS |
408 | =head1 LOGGING CONTEXTS |
381 | |
409 | |