ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Log.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Log.pm (file contents):
Revision 1.18 by root, Sat Aug 20 15:57:35 2011 UTC vs.
Revision 1.24 by root, Sun Aug 21 03:24:59 2011 UTC

2 2
3AnyEvent::Log - simple logging "framework" 3AnyEvent::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
36attempt to be "the" logging solution or even "a" logging solution for 45attempt to be "the" logging solution or even "a" logging solution for
37AnyEvent - AnyEvent simply creates logging messages internally, and this 46AnyEvent - AnyEvent simply creates logging messages internally, and this
38module more or less exposes the mechanism, with some extra spiff to allow 47module more or less exposes the mechanism, with some extra spiff to allow
39using it from other modules as well. 48using it from other modules as well.
40 49
41Remember that the default verbosity level is C<0>, so nothing will be 50Remember that the default verbosity level is C<0> (C<off>), so nothing
42logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number before 51will be logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number
43starting your program, or change the logging level at runtime with 52before starting your program, or change the logging level at runtime with
44something like: 53something 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
143Requests logging of the given C<$msg> with the given log level. 152Requests logging of the given C<$msg> with the given log level, and
153returns true if the message was logged I<somewhere>.
144 154
145For C<fatal> log levels, the program will abort. 155For C<fatal> log levels, the program will abort.
146 156
147If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the 157If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the
148C<$msg> is interpreted as an sprintf format string. 158C<$msg> is interpreted as an sprintf format string.
154supposed to return the message. It will be called only then the message 164supposed to return the message. It will be called only then the message
155actually gets logged, which is useful if it is costly to create the 165actually gets logged, which is useful if it is costly to create the
156message in the first place. 166message in the first place.
157 167
158Whether the given message will be logged depends on the maximum log level 168Whether the given message will be logged depends on the maximum log level
159and the caller's package. 169and the caller's package. The return value can be used to ensure that
170messages or not "lost" - for example, when L<AnyEvent::Debug> detects a
171runtime error it tries to log it at C<die> level, but if that message is
172lost it simply uses warn.
160 173
161Note that you can (and should) call this function as C<AnyEvent::log> or 174Note that you can (and should) call this function as C<AnyEvent::log> or
162C<AE::log>, without C<use>-ing this module if possible (i.e. you don't 175C<AE::log>, without C<use>-ing this module if possible (i.e. you don't
163need any additional functionality), as those functions will load the 176need any additional functionality), as those functions will load the
164logging module on demand only. They are also much shorter to write. 177logging 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
263sub log($$;@) { 280sub 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
273Creates a code reference that, when called, acts as if the 290Creates a code reference that, when called, acts as if the
274C<AnyEvent::Log::log> function was called at this point with the givne 291C<AnyEvent::Log::log> function was called at this point with the given
275level. C<$logger> is passed a C<$msg> and optional C<@args>, just as with 292level. C<$logger> is passed a C<$msg> and optional C<@args>, just as with
276the C<AnyEvent::Log::log> function: 293the 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
491sub reset { 508sub 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
731the logging (which consists of formatting the message and printing it or 751the logging (which consists of formatting the message and printing it or
732whatever it wants to do with it). 752whatever 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
738Replaces the logging callback on the context (C<undef> disables the 758Replaces the logging callback on the context (C<undef> disables the
739logging callback). 759logging callback).
740 760
741The logging callback is responsible for handling formatted log messages 761The logging callback is responsible for handling formatted log messages
760your program. 780your 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
767Sets the C<log_cb> to log to a file (by appending), unbuffered.
768
769=item $ctx->log_to_path ($path)
770
771Same as C<< ->log_to_file >>, but opens the file for each message. This
772is much slower, but allows you to change/move/rename/delete the file at
773basically 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
777Replaces the formatting callback on the context (C<undef> restores the 787Replaces the formatting callback on the context (C<undef> restores the
778default formatter). 788default formatter).
779 789
780The callback is passed the (possibly fractional) timestamp, the original 790The 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
827Sets the C<log_cb> to log to a file (by appending), unbuffered.
828
829=item $ctx->log_to_path ($path)
830
831Same as C<< ->log_to_file >>, but opens the file for each message. This
832is much slower, but allows you to change/move/rename/delete the file at
833basically any time.
834
835=item $ctx->log_to_syslog ([$log_flags])
836
837Logs all messages via L<Sys::Syslog>, mapping C<trace> to C<debug> and all
838the others in the obvious way. If specified, then the C<$log_flags> are
839simply or'ed onto the priority argument and can contain any C<LOG_xxx>
840flags valid for Sys::Syslog::syslog, except for the priority levels.
841
842Note that this function also sets a C<fmt_cb> - the logging part requires
843an array reference with [$level, $str] as input.
844
815=cut 845=cut
816 846
817sub log_cb { 847sub 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
883sub 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
857These methods allow you to log messages directly to a context, without 909These methods allow you to log messages directly to a context, without

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines