… | |
… | |
8 | |
8 | |
9 | use AnyEvent; |
9 | use AnyEvent; |
10 | |
10 | |
11 | AE::log fatal => "No config found, cannot continue!"; # never returns |
11 | AE::log fatal => "No config found, cannot continue!"; # never returns |
12 | AE::log alert => "The battery died!"; |
12 | AE::log alert => "The battery died!"; |
13 | AE::log crit => "The battery temperature is too hot!"; |
13 | AE::log crit => "The battery is too hot!"; |
14 | AE::log error => "Division by zero attempted."; |
14 | AE::log error => "Division by zero attempted."; |
15 | AE::log warn => "Couldn't delete the file."; |
15 | AE::log warn => "Couldn't delete the file."; |
16 | AE::log note => "Wanted to create config, but config already exists."; |
16 | AE::log note => "Attempted to create config, but config already exists."; |
17 | AE::log info => "File soandso successfully deleted."; |
17 | AE::log info => "File soandso successfully deleted."; |
18 | AE::log debug => "the function returned 3"; |
18 | AE::log debug => "the function returned 3"; |
19 | AE::log trace => "going to call function abc"; |
19 | AE::log trace => "going to call function abc"; |
20 | |
20 | |
21 | Log level overview: |
21 | Log level overview: |
… | |
… | |
33 | |
33 | |
34 | "Complex" uses (for speed sensitive code, e.g. trace/debug messages): |
34 | "Complex" uses (for speed sensitive code, e.g. trace/debug messages): |
35 | |
35 | |
36 | use AnyEvent::Log; |
36 | use AnyEvent::Log; |
37 | |
37 | |
38 | my $tracer = AnyEvent::Log::logger trace => \$my $trace; |
38 | my $tracer = AnyEvent::Log::logger trace => \my $trace; |
39 | |
39 | |
40 | $tracer->("i am here") if $trace; |
40 | $tracer->("i am here") if $trace; |
41 | $tracer->(sub { "lots of data: " . Dumper $self }) if $trace; |
41 | $tracer->(sub { "lots of data: " . Dumper $self }) if $trace; |
42 | |
42 | |
43 | Configuration (also look at the EXAMPLES section): |
43 | Configuration (also look at the EXAMPLES section): |
44 | |
44 | |
|
|
45 | # set default logging level to suppress anything below "notice" |
|
|
46 | # i.e. enable logging at "notice" or above - the default is to |
|
|
47 | # to not log anything at all. |
|
|
48 | $AnyEvent::Log::FILTER->level ("notice"); |
|
|
49 | |
45 | # set logging for the current package to errors and higher only |
50 | # set logging for the current package to errors and higher only |
46 | AnyEvent::Log::ctx->level ("error"); |
51 | AnyEvent::Log::ctx->level ("error"); |
47 | |
52 | |
48 | # set logging level to suppress anything below "notice" |
53 | # enable logging for the current package, regardless of global logging level |
49 | $AnyEvent::Log::FILTER->level ("notice"); |
54 | AnyEvent::Log::ctx->attach ($AnyEvent::Log::LOG); |
|
|
55 | |
|
|
56 | # enable debug logging for module some::mod and enable logging by default |
|
|
57 | (AnyEvent::Log::ctx "some::mod")->level ("debug"); |
|
|
58 | (AnyEvent::Log::ctx "some::mod")->attach ($AnyEvent::Log::LOG); |
50 | |
59 | |
51 | # send all critical and higher priority messages to syslog, |
60 | # send all critical and higher priority messages to syslog, |
52 | # regardless of (most) other settings |
61 | # regardless of (most) other settings |
53 | $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx |
62 | $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx |
54 | level => "critical", |
63 | level => "critical", |
… | |
… | |
121 | For example, a program that finds an unknown switch on the commandline |
130 | For example, a program that finds an unknown switch on the commandline |
122 | might well use a fatal logging level to tell users about it - the "system" |
131 | might well use a fatal logging level to tell users about it - the "system" |
123 | in this case would be the program, or module. |
132 | in this case would be the program, or module. |
124 | |
133 | |
125 | Some methods also offer some extra levels, such as C<0>, C<off>, C<none> |
134 | Some methods also offer some extra levels, such as C<0>, C<off>, C<none> |
126 | or C<all> - these are only valid for the methods that documented them. |
135 | or C<all> - these are only valid for the methods that document them. |
127 | |
136 | |
128 | =head1 LOGGING FUNCTIONS |
137 | =head1 LOGGING FUNCTIONS |
129 | |
138 | |
130 | The following functions allow you to log messages. They always use the |
139 | The following functions allow you to log messages. They always use the |
131 | caller's package as a "logging context". Also, the main logging function, |
140 | caller's package as a "logging context". Also, the main logging function, |
… | |
… | |
449 | time on each log message. This only makes a difference for event loops |
458 | time on each log message. This only makes a difference for event loops |
450 | that actually cache the time (such as L<EV> or L<AnyEvent::Loop>). |
459 | that actually cache the time (such as L<EV> or L<AnyEvent::Loop>). |
451 | |
460 | |
452 | This setting can be changed at any time by calling this function. |
461 | This setting can be changed at any time by calling this function. |
453 | |
462 | |
454 | Since C<AnyEvent::Log> has to work even before the L<AnyEvent> has been |
463 | Since C<AnyEvent::Log> has to work even before L<AnyEvent> has been |
455 | initialised, this switch will also decide whether to use C<CORE::time> or |
464 | initialised, this switch will also decide whether to use C<CORE::time> or |
456 | C<Time::HiRes::time> when logging a message before L<AnyEvent> becomes |
465 | C<Time::HiRes::time> when logging a message before L<AnyEvent> becomes |
457 | available. |
466 | available. |
458 | |
467 | |
459 | =item AnyEvent::Log::format_time $timestamp |
468 | =item AnyEvent::Log::format_time $timestamp |
… | |
… | |
478 | |
487 | |
479 | In your main program (as opposed to in your module) you can override the |
488 | In your main program (as opposed to in your module) you can override the |
480 | default message format by loading this module and then redefining this |
489 | default message format by loading this module and then redefining this |
481 | function. |
490 | function. |
482 | |
491 | |
483 | =item AnyEvent::Log::fatal_exit |
492 | =item AnyEvent::Log::fatal_exit() |
484 | |
493 | |
485 | This is the function that is called after logging a C<fatal> log |
494 | This is the function that is called after logging a C<fatal> log |
486 | message. It must not return. |
495 | message. It must not return. |
487 | |
496 | |
488 | The default implementation simply calls C<exit 1>. |
497 | The default implementation simply calls C<exit 1>. |
… | |
… | |
521 | context, so it does not matter (much) if there are cycles or if the |
530 | context, so it does not matter (much) if there are cycles or if the |
522 | message can arrive at the same context via multiple paths. |
531 | message can arrive at the same context via multiple paths. |
523 | |
532 | |
524 | =head2 DEFAULTS |
533 | =head2 DEFAULTS |
525 | |
534 | |
526 | By default, all logging contexts have an full set of log levels ("all"), a |
535 | By default, all logging contexts have a full set of log levels ("all"), a |
527 | disabled logging callback and the default formatting callback. |
536 | disabled logging callback and the default formatting callback. |
528 | |
537 | |
529 | Package contexts have the package name as logging title by default. |
538 | Package contexts have the package name as logging title by default. |
530 | |
539 | |
531 | They have exactly one slave - the context of the "parent" package. The |
540 | They have exactly one slave - the context of the "parent" package. The |
… | |
… | |
585 | |
594 | |
586 | =item $ctx = AnyEvent::Log::ctx [$pkg] |
595 | =item $ctx = AnyEvent::Log::ctx [$pkg] |
587 | |
596 | |
588 | This function creates or returns a logging context (which is an object). |
597 | This function creates or returns a logging context (which is an object). |
589 | |
598 | |
590 | If a package name is given, then the context for that packlage is |
599 | If a package name is given, then the context for that package is |
591 | returned. If it is called without any arguments, then the context for the |
600 | returned. If it is called without any arguments, then the context for the |
592 | callers package is returned (i.e. the same context as a C<AE::log> call |
601 | callers package is returned (i.e. the same context as a C<AE::log> call |
593 | would use). |
602 | would use). |
594 | |
603 | |
595 | If C<undef> is given, then it creates a new anonymous context that is not |
604 | If C<undef> is given, then it creates a new anonymous context that is not |
… | |
… | |
875 | sub attach { |
884 | sub attach { |
876 | my $ctx = shift; |
885 | my $ctx = shift; |
877 | |
886 | |
878 | $ctx->[2]{$_+0} = $_ |
887 | $ctx->[2]{$_+0} = $_ |
879 | for map { AnyEvent::Log::ctx $_ } @_; |
888 | for map { AnyEvent::Log::ctx $_ } @_; |
|
|
889 | AnyEvent::Log::_reassess; |
880 | } |
890 | } |
881 | |
891 | |
882 | sub detach { |
892 | sub detach { |
883 | my $ctx = shift; |
893 | my $ctx = shift; |
884 | |
894 | |
885 | delete $ctx->[2]{$_+0} |
895 | delete $ctx->[2]{$_+0} |
886 | for map { AnyEvent::Log::ctx $_ } @_; |
896 | for map { AnyEvent::Log::ctx $_ } @_; |
|
|
897 | AnyEvent::Log::_reassess; |
887 | } |
898 | } |
888 | |
899 | |
889 | sub slaves { |
900 | sub slaves { |
890 | undef $_[0][2]; |
901 | undef $_[0][2]; |
891 | &attach; |
902 | &attach; |
|
|
903 | AnyEvent::Log::_reassess; |
892 | } |
904 | } |
893 | |
905 | |
894 | =back |
906 | =back |
895 | |
907 | |
896 | =head3 LOG TARGETS |
908 | =head3 LOG TARGETS |