… | |
… | |
45 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
45 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
46 | module more or less exposes the mechanism, with some extra spiff to allow |
46 | module more or less exposes the mechanism, with some extra spiff to allow |
47 | using it from other modules as well. |
47 | using it from other modules as well. |
48 | |
48 | |
49 | Remember that the default verbosity level is C<0>, so nothing will be |
49 | Remember that the default verbosity level is C<0>, so nothing will be |
50 | logged, ever, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number |
50 | logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number before |
51 | before starting your program, or change the logging level at runtime wiht |
51 | starting your program, or change the logging level at runtime with |
52 | something like: |
52 | something like: |
53 | |
53 | |
54 | use AnyEvent; |
54 | use AnyEvent; |
55 | (AnyEvent::Log::ctx "")->level ("info"); |
55 | (AnyEvent::Log::ctx "")->level ("info"); |
56 | |
56 | |
… | |
… | |
96 | # creates a default package context object for the given package |
96 | # creates a default package context object for the given package |
97 | sub _pkg_ctx($) { |
97 | sub _pkg_ctx($) { |
98 | my $ctx = bless [$_[0], (1 << 10) - 1 - 1, {}], "AnyEvent::Log::Ctx"; |
98 | my $ctx = bless [$_[0], (1 << 10) - 1 - 1, {}], "AnyEvent::Log::Ctx"; |
99 | |
99 | |
100 | # link "parent" package |
100 | # link "parent" package |
101 | my $pkg = $_[0] =~ /^(.+)::/ ? $1 : ""; |
101 | my $pkg = $_[0] =~ /^(.+)::/ ? $1 : "AE::Log::Top"; |
102 | |
102 | |
103 | $pkg = $CTX{$pkg} ||= &_pkg_ctx ($pkg); |
103 | $pkg = $CTX{$pkg} ||= &_pkg_ctx ($pkg); |
104 | $ctx->[2]{$pkg+0} = $pkg; |
104 | $ctx->[2]{$pkg+0} = $pkg; |
105 | |
105 | |
106 | $ctx |
106 | $ctx |
… | |
… | |
132 | Note that you can (and should) call this function as C<AnyEvent::log> or |
132 | Note that you can (and should) call this function as C<AnyEvent::log> or |
133 | C<AE::log>, without C<use>-ing this module if possible (i.e. you don't |
133 | C<AE::log>, without C<use>-ing this module if possible (i.e. you don't |
134 | need any additional functionality), as those functions will load the |
134 | need any additional functionality), as those functions will load the |
135 | logging module on demand only. They are also much shorter to write. |
135 | logging module on demand only. They are also much shorter to write. |
136 | |
136 | |
137 | Also, if you otpionally generate a lot of debug messages (such as when |
137 | Also, if you optionally generate a lot of debug messages (such as when |
138 | tracing some code), you should look into using a logger callback and a |
138 | tracing some code), you should look into using a logger callback and a |
139 | boolean enabler (see C<logger>, below). |
139 | boolean enabler (see C<logger>, below). |
140 | |
140 | |
141 | Example: log something at error level. |
141 | Example: log something at error level. |
142 | |
142 | |
… | |
… | |
173 | |
173 | |
174 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
174 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
175 | |
175 | |
176 | # time, ctx, level, msg |
176 | # time, ctx, level, msg |
177 | sub _format($$$$) { |
177 | sub _format($$$$) { |
178 | my $pfx = ft $_[0]; |
178 | my $ts = ft $_[0]; |
|
|
179 | my $ct = " "; |
|
|
180 | |
179 | my @res; |
181 | my @res; |
180 | |
182 | |
181 | for (split /\n/, sprintf "%-5s %s: %s", $LEVEL2STR[$_[2]], $_[1][0], $_[3]) { |
183 | for (split /\n/, sprintf "%-5s %s: %s", $LEVEL2STR[$_[2]], $_[1][0], $_[3]) { |
182 | push @res, "$pfx $_\n"; |
184 | push @res, "$ts$ct$_\n"; |
183 | $pfx = "\t"; |
185 | $ct = " + "; |
184 | } |
186 | } |
185 | |
187 | |
186 | join "", @res |
188 | join "", @res |
187 | } |
189 | } |
188 | |
190 | |
189 | sub _log { |
191 | sub _log { |
190 | my ($ctx, $level, $format, @args) = @_; |
192 | my ($ctx, $level, $format, @args) = @_; |
191 | |
193 | |
|
|
194 | $level = $level > 0 && $level <= 9 |
|
|
195 | ? $level+0 |
192 | $level = $level > 0 && $level <= 9 ? $level+0 : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; |
196 | : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; |
193 | |
197 | |
194 | my $mask = 1 << $level; |
198 | my $mask = 1 << $level; |
195 | |
199 | |
196 | my (@ctx, $now, $fmt); |
200 | my (%seen, @ctx, $now, $fmt); |
197 | |
201 | |
198 | do { |
202 | do |
|
|
203 | { |
199 | # skip if masked |
204 | # skip if masked |
200 | next unless $ctx->[1] & $mask; |
205 | if ($ctx->[1] & $mask && !$seen{$ctx+0}++) { |
201 | |
|
|
202 | if ($ctx->[3]) { |
206 | if ($ctx->[3]) { |
203 | # logging target found |
207 | # logging target found |
204 | |
208 | |
205 | # now get raw message, unless we have it already |
209 | # now get raw message, unless we have it already |
206 | unless ($now) { |
210 | unless ($now) { |
207 | $format = $format->() if ref $format; |
211 | $format = $format->() if ref $format; |
208 | $format = sprintf $format, @args if @args; |
212 | $format = sprintf $format, @args if @args; |
209 | $format =~ s/\n$//; |
213 | $format =~ s/\n$//; |
210 | $now = AE::now; |
214 | $now = AE::now; |
211 | }; |
215 | }; |
212 | |
216 | |
213 | # format msg |
217 | # format msg |
214 | my $str = $ctx->[4] |
218 | my $str = $ctx->[4] |
215 | ? $ctx->[4]($now, $_[0], $level, $format) |
219 | ? $ctx->[4]($now, $_[0], $level, $format) |
216 | : $fmt ||= _format $now, $_[0], $level, $format; |
220 | : $fmt ||= _format $now, $_[0], $level, $format; |
217 | |
221 | |
218 | $ctx->[3]($str) |
222 | $ctx->[3]($str); |
219 | and next; |
223 | } |
|
|
224 | |
|
|
225 | # not masked, not consumed - propagate to parent contexts |
|
|
226 | push @ctx, values %{ $ctx->[2] }; |
|
|
227 | } |
220 | } |
228 | } |
221 | |
|
|
222 | # not masked, not consume - propagate to parent contexts |
|
|
223 | push @ctx, values %{ $ctx->[2] }; |
|
|
224 | } while $ctx = pop @ctx; |
229 | while $ctx = pop @ctx; |
225 | |
230 | |
226 | exit 1 if $level <= 1; |
231 | exit 1 if $level <= 1; |
227 | } |
232 | } |
228 | |
233 | |
229 | sub log($$;@) { |
234 | sub log($$;@) { |
… | |
… | |
282 | # re-assess logging status for all loggers |
287 | # re-assess logging status for all loggers |
283 | sub _reassess { |
288 | sub _reassess { |
284 | for (@_ ? $LOGGER{$_[0]} : values %LOGGER) { |
289 | for (@_ ? $LOGGER{$_[0]} : values %LOGGER) { |
285 | my ($ctx, $level, $renabled) = @$_; |
290 | my ($ctx, $level, $renabled) = @$_; |
286 | |
291 | |
287 | # to detetc whether a message would be logged, we # actually |
292 | # to detect whether a message would be logged, we # actually |
288 | # try to log one and die. this isn't # fast, but we can be |
293 | # try to log one and die. this isn't fast, but we can be |
289 | # sure that the logging decision is correct :) |
294 | # sure that the logging decision is correct :) |
290 | |
295 | |
291 | $$renabled = !eval { |
296 | $$renabled = !eval { |
292 | local $SIG{__DIE__}; |
297 | local $SIG{__DIE__}; |
293 | |
298 | |
… | |
… | |
356 | For propagation, a context can have any number of attached I<parent |
361 | For propagation, a context can have any number of attached I<parent |
357 | contexts>. Any message that is neither masked by the logging mask nor |
362 | contexts>. Any message that is neither masked by the logging mask nor |
358 | masked by the logging callback returning true will be passed to all parent |
363 | masked by the logging callback returning true will be passed to all parent |
359 | contexts. |
364 | contexts. |
360 | |
365 | |
|
|
366 | Each call to a logging function will log the message at most once per |
|
|
367 | context, so it does not matter (much) if there are cycles or if the |
|
|
368 | message can arrive at the same context via multiple paths. |
|
|
369 | |
361 | =head2 DEFAULTS |
370 | =head2 DEFAULTS |
362 | |
371 | |
363 | By default, all logging contexts have an full set of log levels ("all"), a |
372 | By default, all logging contexts have an full set of log levels ("all"), a |
364 | disabled logging callback and the default formatting callback. |
373 | disabled logging callback and the default formatting callback. |
365 | |
374 | |
366 | Package contexts have the package name as logging title by default. |
375 | Package contexts have the package name as logging title by default. |
367 | |
376 | |
368 | They have exactly one parent - the context of the "parent" package. The |
377 | They have exactly one parent - the context of the "parent" package. The |
369 | parent package is simply defined to be the package name without the last |
378 | parent package is simply defined to be the package name without the last |
370 | component, i.e. C<AnyEvent::Debug::Wrapped> becomes C<AnyEvent::Debug>, |
379 | component, i.e. C<AnyEvent::Debug::Wrapped> becomes C<AnyEvent::Debug>, |
371 | and C<AnyEvent> becomes the empty string. |
380 | and C<AnyEvent> becomes ... C<AnyEvent::Log::Top> which is the |
|
|
381 | exception of the rule - just like the parent of any package name in |
|
|
382 | Perl is C<main>, the default parent of any toplevel package context is |
|
|
383 | C<AnyEvent::Log::Top>. |
372 | |
384 | |
373 | Since perl packages form only an approximate hierarchy, this parent |
385 | Since perl packages form only an approximate hierarchy, this parent |
374 | context can of course be removed. |
386 | context can of course be removed. |
375 | |
387 | |
376 | All other (anonymous) contexts have no parents and an empty title by |
388 | All other (anonymous) contexts have no parents and an empty title by |
377 | default. |
389 | default. |
378 | |
390 | |
379 | When the module is first loaded, it configures the root context (the one |
391 | When the module is loaded it creates the default context called |
380 | with the empty string) to simply dump all log messages to C<STDERR>, |
392 | C<AnyEvent::Log::Default>, which simply logs everything to STDERR and |
|
|
393 | doesn't propagate anything anywhere by default. The purpose of the default |
|
|
394 | context is to provide a convenient place to override the global logging |
|
|
395 | target or to attach additional log targets. It's not meant for filtering. |
|
|
396 | |
|
|
397 | It then creates the root context called C<AnyEvent::Log::Root> and |
381 | and sets it's log level set to all levels up to the one specified by |
398 | sets its log level set to all levels up to the one specified by |
382 | C<$ENV{PERL_ANYEVENT_VERBOSE}>. |
399 | C<$ENV{PERL_ANYEVENT_VERBOSE}>. It then attached the default logging |
|
|
400 | context to it. The purpose of the root context is to simply provide |
|
|
401 | filtering according to some global log level. |
383 | |
402 | |
|
|
403 | Finally it creates the toplevel package context called |
|
|
404 | C<AnyEvent::Log::Top> and attached the root context but otherwise leaves |
|
|
405 | it at default config. It's purpose is simply to collect all log messages |
|
|
406 | system-wide. |
|
|
407 | |
|
|
408 | These three special contexts can also be referred to by the names |
|
|
409 | C<AE::Log::Default>, C<AE::Log::Root> and C<AE::Log::Top>. |
|
|
410 | |
384 | The effect of all this is that log messages, by default, wander up to the |
411 | The effect of all this is that log messages, by default, wander up |
385 | root context and will be logged to STDERR if their log level is less than |
412 | to the root context where log messages with lower priority then |
386 | or equal to C<$ENV{PERL_ANYEVENT_VERBOSE}>. |
413 | C<$ENV{PERL_ANYEVENT_VERBOSE}> will be filtered away and then to the |
|
|
414 | AnyEvent::Log::Default context to be logged to STDERR. |
387 | |
415 | |
|
|
416 | Splitting the top level context into three contexts makes it easy to set |
|
|
417 | a global logging level (by modifying the root context), but still allow |
|
|
418 | other contexts to log, for example, their debug and trace messages to the |
|
|
419 | default target despite the global logging level, or to attach additional |
|
|
420 | log targets that log messages, regardless of the global logging level. |
|
|
421 | |
|
|
422 | It also makes it easy to replace the default STDERR-logger by something |
|
|
423 | that logs to a file, or to attach additional logging targets. |
|
|
424 | |
388 | =head2 CREATING/FINDING A CONTEXT |
425 | =head2 CREATING/FINDING/DESTROYING CONTEXTS |
389 | |
426 | |
390 | =over 4 |
427 | =over 4 |
391 | |
428 | |
392 | =item $ctx = AnyEvent::Log::ctx [$pkg] |
429 | =item $ctx = AnyEvent::Log::ctx [$pkg] |
393 | |
430 | |
… | |
… | |
411 | : defined $pkg |
448 | : defined $pkg |
412 | ? $CTX{$pkg} ||= AnyEvent::Log::_pkg_ctx $pkg |
449 | ? $CTX{$pkg} ||= AnyEvent::Log::_pkg_ctx $pkg |
413 | : bless [undef, (1 << 10) - 1 - 1], "AnyEvent::Log::Ctx" |
450 | : bless [undef, (1 << 10) - 1 - 1], "AnyEvent::Log::Ctx" |
414 | } |
451 | } |
415 | |
452 | |
416 | # create default root context |
453 | =item AnyEvent::Log::reset |
417 | { |
454 | |
|
|
455 | Deletes all contexts and recreates the default hierarchy, i.e. resets the |
|
|
456 | logging subsystem to defaults. |
|
|
457 | |
|
|
458 | This can be used to implement config-file (re-)loading: before loading a |
|
|
459 | configuration, reset all contexts. |
|
|
460 | |
|
|
461 | =cut |
|
|
462 | |
|
|
463 | sub reset { |
|
|
464 | @$_ = () for values %CTX; # just to be sure - to kill circular logging dependencies |
|
|
465 | %CTX = (); |
|
|
466 | |
418 | my $root = ctx undef; |
467 | my $default = ctx undef; |
419 | $root->[0] = ""; |
468 | $default->title ("AnyEvent::Log::Default"); |
420 | $root->title ("default"); |
|
|
421 | $root->level ($AnyEvent::VERBOSE); undef $AnyEvent::VERBOSE; |
|
|
422 | $root->log_cb (sub { |
469 | $default->log_cb (sub { |
423 | print STDERR shift; |
470 | print STDERR shift; |
424 | 0 |
471 | 0 |
425 | }); |
472 | }); |
426 | $CTX{""} = $root; |
473 | $CTX{"AnyEvent::Log::Default"} = $CTX{"AE::Log::Default"} = $default; |
|
|
474 | |
|
|
475 | my $root = ctx undef; |
|
|
476 | $root->title ("AnyEvent::Log::Root"); |
|
|
477 | $root->level ($AnyEvent::VERBOSE); |
|
|
478 | $root->attach ($default); |
|
|
479 | $CTX{"AnyEvent::Log::Root"} = $CTX{"AE::Log::Root"} = $root; |
|
|
480 | |
|
|
481 | my $top = ctx undef; |
|
|
482 | $top->title ("AnyEvent::Log::Top"); |
|
|
483 | $top->attach ($root); |
|
|
484 | $CTX{"AnyEvent::Log::Top"} = $CTX{"AE::Log::Top"} = $top; |
427 | } |
485 | } |
|
|
486 | |
|
|
487 | AnyEvent::Log::reset; |
|
|
488 | |
|
|
489 | package AnyEvent::Log::Default; |
|
|
490 | package AE::Log::Default; |
|
|
491 | package AnyEvent::Log::Root; |
|
|
492 | package AE::Log::Root; |
|
|
493 | package AnyEvent::Log::Top; |
|
|
494 | package AE::Log::Top; |
428 | |
495 | |
429 | =back |
496 | =back |
430 | |
497 | |
431 | =cut |
498 | =cut |
432 | |
499 | |
… | |
… | |
559 | Removes the given parents from this context - it's not an error to attempt |
626 | Removes the given parents from this context - it's not an error to attempt |
560 | to remove a context that hasn't been added. |
627 | to remove a context that hasn't been added. |
561 | |
628 | |
562 | A context can be specified either as package name or as a context object. |
629 | A context can be specified either as package name or as a context object. |
563 | |
630 | |
|
|
631 | =item $ctx->parents ($ctx2[, $ctx3...]) |
|
|
632 | |
|
|
633 | Replaces all parents attached to this context by the ones given. |
|
|
634 | |
564 | =cut |
635 | =cut |
565 | |
636 | |
566 | sub attach { |
637 | sub attach { |
567 | my $ctx = shift; |
638 | my $ctx = shift; |
568 | |
639 | |
… | |
… | |
573 | sub detach { |
644 | sub detach { |
574 | my $ctx = shift; |
645 | my $ctx = shift; |
575 | |
646 | |
576 | delete $ctx->[2]{$_+0} |
647 | delete $ctx->[2]{$_+0} |
577 | for map { AnyEvent::Log::ctx $_ } @_; |
648 | for map { AnyEvent::Log::ctx $_ } @_; |
|
|
649 | } |
|
|
650 | |
|
|
651 | sub parents { |
|
|
652 | undef $_[0][2]; |
|
|
653 | &attach; |
578 | } |
654 | } |
579 | |
655 | |
580 | =back |
656 | =back |
581 | |
657 | |
582 | =head3 MESSAGE LOGGING |
658 | =head3 MESSAGE LOGGING |