… | |
… | |
2 | |
2 | |
3 | AnyEvent::Log - simple logging "framework" |
3 | AnyEvent::Log - simple logging "framework" |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | # simple use |
7 | # simple use: |
|
|
8 | |
8 | use AnyEvent; |
9 | use AnyEvent; |
9 | |
10 | |
10 | AE::log debug => "hit my knee"; |
11 | AE::log debug => "hit my knee"; |
11 | AE::log warn => "it's a bit too hot"; |
12 | AE::log warn => "it's a bit too hot"; |
12 | AE::log error => "the flag was false!"; |
13 | AE::log error => "the flag was false!"; |
13 | AE::log fatal => "the bit toggled! run!"; |
14 | AE::log fatal => "the bit toggled! run!"; # never returns |
14 | |
15 | |
15 | # "complex" use |
16 | # "complex" use (for speed sensitive code): |
|
|
17 | |
16 | use AnyEvent::Log; |
18 | use AnyEvent::Log; |
17 | |
19 | |
18 | my $tracer = AnyEvent::Log::logger trace => \$my $trace; |
20 | my $tracer = AnyEvent::Log::logger trace => \$my $trace; |
19 | |
21 | |
20 | $tracer->("i am here") if $trace; |
22 | $tracer->("i am here") if $trace; |
21 | $tracer->(sub { "lots of data: " . Dumper $self }) if $trace; |
23 | $tracer->(sub { "lots of data: " . Dumper $self }) if $trace; |
22 | |
24 | |
23 | # configuration |
25 | # configuration: |
24 | |
26 | |
25 | # set logging for the current package to errors and higher only |
27 | # set logging for the current package to errors and higher only |
26 | AnyEvent::Log::ctx->level ("error"); |
28 | AnyEvent::Log::ctx->level ("error"); |
27 | |
29 | |
28 | # set logging globally to anything below debug |
30 | # set logging level to suppress anything below "notice" |
29 | $AnyEvent::Log::FILTER->level ("notice"); |
31 | $AnyEvent::Log::FILTER->level ("notice"); |
|
|
32 | |
|
|
33 | # send all critical and higher priority messages to syslog, |
|
|
34 | # regardless of (most) other settings |
|
|
35 | $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx |
|
|
36 | level => "critical", |
|
|
37 | log_to_syslog => 0, |
|
|
38 | ); |
30 | |
39 | |
31 | # see also EXAMPLES, below |
40 | # see also EXAMPLES, below |
32 | |
41 | |
33 | =head1 DESCRIPTION |
42 | =head1 DESCRIPTION |
34 | |
43 | |
… | |
… | |
36 | attempt to be "the" logging solution or even "a" logging solution for |
45 | attempt to be "the" logging solution or even "a" logging solution for |
37 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
46 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
38 | module more or less exposes the mechanism, with some extra spiff to allow |
47 | module more or less exposes the mechanism, with some extra spiff to allow |
39 | using it from other modules as well. |
48 | using it from other modules as well. |
40 | |
49 | |
41 | Remember that the default verbosity level is C<0>, so nothing will be |
50 | Remember that the default verbosity level is C<0> (C<off>), so nothing |
42 | logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number before |
51 | will be logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number |
43 | starting your program, or change the logging level at runtime with |
52 | before starting your program, or change the logging level at runtime with |
44 | something like: |
53 | something like: |
45 | |
54 | |
46 | use AnyEvent::Log; |
55 | use AnyEvent::Log; |
47 | AnyEvent::Log::FILTER->level ("info"); |
56 | AnyEvent::Log::FILTER->level ("info"); |
48 | |
57 | |
… | |
… | |
138 | $ctx |
147 | $ctx |
139 | } |
148 | } |
140 | |
149 | |
141 | =item AnyEvent::Log::log $level, $msg[, @args] |
150 | =item AnyEvent::Log::log $level, $msg[, @args] |
142 | |
151 | |
143 | Requests logging of the given C<$msg> with the given log level. |
152 | Requests logging of the given C<$msg> with the given log level, and |
|
|
153 | returns true if the message was logged I<somewhere>. |
144 | |
154 | |
145 | For C<fatal> log levels, the program will abort. |
155 | For C<fatal> log levels, the program will abort. |
146 | |
156 | |
147 | If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the |
157 | If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the |
148 | C<$msg> is interpreted as an sprintf format string. |
158 | C<$msg> is interpreted as an sprintf format string. |
… | |
… | |
154 | supposed to return the message. It will be called only then the message |
164 | supposed to return the message. It will be called only then the message |
155 | actually gets logged, which is useful if it is costly to create the |
165 | actually gets logged, which is useful if it is costly to create the |
156 | message in the first place. |
166 | message in the first place. |
157 | |
167 | |
158 | Whether the given message will be logged depends on the maximum log level |
168 | Whether the given message will be logged depends on the maximum log level |
159 | and the caller's package. |
169 | and the caller's package. The return value can be used to ensure that |
|
|
170 | messages or not "lost" - for example, when L<AnyEvent::Debug> detects a |
|
|
171 | runtime error it tries to log it at C<die> level, but if that message is |
|
|
172 | lost it simply uses warn. |
160 | |
173 | |
161 | Note that you can (and should) call this function as C<AnyEvent::log> or |
174 | Note that you can (and should) call this function as C<AnyEvent::log> or |
162 | C<AE::log>, without C<use>-ing this module if possible (i.e. you don't |
175 | C<AE::log>, without C<use>-ing this module if possible (i.e. you don't |
163 | need any additional functionality), as those functions will load the |
176 | need any additional functionality), as those functions will load the |
164 | logging module on demand only. They are also much shorter to write. |
177 | logging module on demand only. They are also much shorter to write. |
… | |
… | |
224 | ? $level+0 |
237 | ? $level+0 |
225 | : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; |
238 | : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; |
226 | |
239 | |
227 | my $mask = 1 << $level; |
240 | my $mask = 1 << $level; |
228 | |
241 | |
229 | my (%seen, @ctx, $now, $fmt); |
242 | my ($success, %seen, @ctx, $now, $fmt); |
230 | |
243 | |
231 | do |
244 | do |
232 | { |
245 | { |
233 | # skip if masked |
246 | # skip if masked |
234 | if ($ctx->[1] & $mask && !$seen{$ctx+0}++) { |
247 | if ($ctx->[1] & $mask && !$seen{$ctx+0}++) { |
… | |
… | |
244 | }; |
257 | }; |
245 | |
258 | |
246 | # format msg |
259 | # format msg |
247 | my $str = $ctx->[4] |
260 | my $str = $ctx->[4] |
248 | ? $ctx->[4]($now, $_[0], $level, $format) |
261 | ? $ctx->[4]($now, $_[0], $level, $format) |
249 | : $fmt ||= _format $now, $_[0], $level, $format; |
262 | : ($fmt ||= _format $now, $_[0], $level, $format); |
|
|
263 | |
|
|
264 | $success = 1; |
250 | |
265 | |
251 | $ctx->[3]($str) |
266 | $ctx->[3]($str) |
252 | or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate |
267 | or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate |
253 | } else { |
268 | } else { |
254 | push @ctx, values %{ $ctx->[2] }; # not masked - propagate |
269 | push @ctx, values %{ $ctx->[2] }; # not masked - propagate |
… | |
… | |
256 | } |
271 | } |
257 | } |
272 | } |
258 | while $ctx = pop @ctx; |
273 | while $ctx = pop @ctx; |
259 | |
274 | |
260 | exit 1 if $level <= 1; |
275 | exit 1 if $level <= 1; |
|
|
276 | |
|
|
277 | $success |
261 | } |
278 | } |
262 | |
279 | |
263 | sub log($$;@) { |
280 | sub log($$;@) { |
264 | _log |
281 | _log |
265 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
282 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
… | |
… | |
269 | *AnyEvent::log = *AE::log = \&log; |
286 | *AnyEvent::log = *AE::log = \&log; |
270 | |
287 | |
271 | =item $logger = AnyEvent::Log::logger $level[, \$enabled] |
288 | =item $logger = AnyEvent::Log::logger $level[, \$enabled] |
272 | |
289 | |
273 | Creates a code reference that, when called, acts as if the |
290 | Creates a code reference that, when called, acts as if the |
274 | C<AnyEvent::Log::log> function was called at this point with the givne |
291 | C<AnyEvent::Log::log> function was called at this point with the given |
275 | level. C<$logger> is passed a C<$msg> and optional C<@args>, just as with |
292 | level. C<$logger> is passed a C<$msg> and optional C<@args>, just as with |
276 | the C<AnyEvent::Log::log> function: |
293 | the C<AnyEvent::Log::log> function: |
277 | |
294 | |
278 | my $debug_log = AnyEvent::Log::logger "debug"; |
295 | my $debug_log = AnyEvent::Log::logger "debug"; |
279 | |
296 | |
… | |
… | |
488 | |
505 | |
489 | =cut |
506 | =cut |
490 | |
507 | |
491 | sub reset { |
508 | sub reset { |
492 | # hard to kill complex data structures |
509 | # hard to kill complex data structures |
493 | # we recreate all package loggers and reset the hierarchy |
510 | # we "recreate" all package loggers and reset the hierarchy |
494 | while (my ($k, $v) = each %CTX) { |
511 | while (my ($k, $v) = each %CTX) { |
495 | @$v = ($k, (1 << 10) - 1 - 1, { }); |
512 | @$v = ($k, (1 << 10) - 1 - 1, { }); |
496 | |
513 | |
497 | $v->attach ($k =~ /^(.+)::/ ? $CTX{$1} : $AnyEvent::Log); |
514 | $v->attach ($k =~ /^(.+)::/ ? $CTX{$1} : $AnyEvent::Log::COLLECT); |
498 | } |
515 | } |
|
|
516 | |
|
|
517 | @$_ = ($_->[0], (1 << 10) - 1 - 1) |
|
|
518 | for $LOG, $FILTER, $COLLECT; |
499 | |
519 | |
500 | $LOG->slaves; |
520 | $LOG->slaves; |
501 | $LOG->title ('$AnyEvent::Log::LOG'); |
521 | $LOG->title ('$AnyEvent::Log::LOG'); |
502 | $LOG->log_cb (sub { |
522 | $LOG->log_cb (sub { |
503 | warn shift; |
523 | warn shift; |
… | |
… | |
507 | $FILTER->slaves ($LOG); |
527 | $FILTER->slaves ($LOG); |
508 | $FILTER->title ('$AnyEvent::Log::FILTER'); |
528 | $FILTER->title ('$AnyEvent::Log::FILTER'); |
509 | $FILTER->level ($AnyEvent::VERBOSE); |
529 | $FILTER->level ($AnyEvent::VERBOSE); |
510 | |
530 | |
511 | $COLLECT->slaves ($FILTER); |
531 | $COLLECT->slaves ($FILTER); |
512 | $COLLECT->title ('$AnyEvent::Log::FILTER'); |
532 | $COLLECT->title ('$AnyEvent::Log::COLLECT'); |
513 | |
533 | |
514 | _reassess; |
534 | _reassess; |
515 | } |
535 | } |
516 | |
536 | |
517 | # create the default logger contexts |
537 | # create the default logger contexts |
… | |
… | |
731 | the logging (which consists of formatting the message and printing it or |
751 | the logging (which consists of formatting the message and printing it or |
732 | whatever it wants to do with it). |
752 | whatever it wants to do with it). |
733 | |
753 | |
734 | =over 4 |
754 | =over 4 |
735 | |
755 | |
736 | =item $ctx->log_cb ($cb->($str)) |
756 | =item $ctx->log_cb ($cb->($str) |
737 | |
757 | |
738 | Replaces the logging callback on the context (C<undef> disables the |
758 | Replaces the logging callback on the context (C<undef> disables the |
739 | logging callback). |
759 | logging callback). |
740 | |
760 | |
741 | The logging callback is responsible for handling formatted log messages |
761 | The logging callback is responsible for handling formatted log messages |
… | |
… | |
760 | your program. |
780 | your program. |
761 | |
781 | |
762 | $ctx->levels ("debug", "trace"); |
782 | $ctx->levels ("debug", "trace"); |
763 | $ctx->log_cb (sub { 1 }); # do not log, but eat debug and trace messages |
783 | $ctx->log_cb (sub { 1 }); # do not log, but eat debug and trace messages |
764 | |
784 | |
765 | =item $ctx->log_to_file ($path) |
|
|
766 | |
|
|
767 | Sets the C<log_cb> to log to a file (by appending), unbuffered. |
|
|
768 | |
|
|
769 | =item $ctx->log_to_path ($path) |
|
|
770 | |
|
|
771 | Same as C<< ->log_to_file >>, but opens the file for each message. This |
|
|
772 | is much slower, but allows you to change/move/rename/delete the file at |
|
|
773 | basically any time. |
|
|
774 | |
|
|
775 | =item $ctx->fmt_cb ($fmt_cb->($timestamp, $ctx, $level, $message)) |
785 | =item $ctx->fmt_cb ($fmt_cb->($timestamp, $orig_ctx, $level, $message)) |
776 | |
786 | |
777 | Replaces the formatting callback on the context (C<undef> restores the |
787 | Replaces the formatting callback on the context (C<undef> restores the |
778 | default formatter). |
788 | default formatter). |
779 | |
789 | |
780 | The callback is passed the (possibly fractional) timestamp, the original |
790 | The callback is passed the (possibly fractional) timestamp, the original |
… | |
… | |
810 | "$msg->[3]"; |
820 | "$msg->[3]"; |
811 | |
821 | |
812 | 0 |
822 | 0 |
813 | }); |
823 | }); |
814 | |
824 | |
|
|
825 | =item $ctx->log_to_file ($path) |
|
|
826 | |
|
|
827 | Sets the C<log_cb> to log to a file (by appending), unbuffered. |
|
|
828 | |
|
|
829 | =item $ctx->log_to_path ($path) |
|
|
830 | |
|
|
831 | Same as C<< ->log_to_file >>, but opens the file for each message. This |
|
|
832 | is much slower, but allows you to change/move/rename/delete the file at |
|
|
833 | basically any time. |
|
|
834 | |
|
|
835 | =item $ctx->log_to_syslog ([$log_flags]) |
|
|
836 | |
|
|
837 | Logs all messages via L<Sys::Syslog>, mapping C<trace> to C<debug> and all |
|
|
838 | the others in the obvious way. If specified, then the C<$log_flags> are |
|
|
839 | simply or'ed onto the priority argument and can contain any C<LOG_xxx> |
|
|
840 | flags valid for Sys::Syslog::syslog, except for the priority levels. |
|
|
841 | |
|
|
842 | Note that this function also sets a C<fmt_cb> - the logging part requires |
|
|
843 | an array reference with [$level, $str] as input. |
|
|
844 | |
815 | =cut |
845 | =cut |
816 | |
846 | |
817 | sub log_cb { |
847 | sub log_cb { |
818 | my ($ctx, $cb) = @_; |
848 | my ($ctx, $cb) = @_; |
819 | |
849 | |
… | |
… | |
848 | syswrite $fh, shift; |
878 | syswrite $fh, shift; |
849 | 0 |
879 | 0 |
850 | }); |
880 | }); |
851 | } |
881 | } |
852 | |
882 | |
|
|
883 | sub log_to_syslog { |
|
|
884 | my ($ctx, $flags) = @_; |
|
|
885 | |
|
|
886 | require Sys::Syslog; |
|
|
887 | |
|
|
888 | $ctx->fmt_cb (sub { |
|
|
889 | my $str = $_[3]; |
|
|
890 | $str =~ s/\n(?=.)/\n+ /g; |
|
|
891 | |
|
|
892 | [$_[2], "($_[1][0]) $str"] |
|
|
893 | }); |
|
|
894 | |
|
|
895 | $ctx->log_cb (sub { |
|
|
896 | my $lvl = $_[0][0] < 9 ? $_[0][0] : 8; |
|
|
897 | |
|
|
898 | Sys::Syslog::syslog ($flags | ($lvl - 1), $_) |
|
|
899 | for split /\n/, $_[0][1]; |
|
|
900 | |
|
|
901 | 0 |
|
|
902 | }); |
|
|
903 | } |
|
|
904 | |
853 | =back |
905 | =back |
854 | |
906 | |
855 | =head3 MESSAGE LOGGING |
907 | =head3 MESSAGE LOGGING |
856 | |
908 | |
857 | These methods allow you to log messages directly to a context, without |
909 | These methods allow you to log messages directly to a context, without |