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.19 by root, Sat Aug 20 15:59:33 2011 UTC vs.
Revision 1.21 by root, Sat Aug 20 22:33:08 2011 UTC

36attempt to be "the" logging solution or even "a" logging solution for 36attempt to be "the" logging solution or even "a" logging solution for
37AnyEvent - AnyEvent simply creates logging messages internally, and this 37AnyEvent - AnyEvent simply creates logging messages internally, and this
38module more or less exposes the mechanism, with some extra spiff to allow 38module more or less exposes the mechanism, with some extra spiff to allow
39using it from other modules as well. 39using it from other modules as well.
40 40
41Remember that the default verbosity level is C<0>, so nothing will be 41Remember 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 42will be logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number
43starting your program, or change the logging level at runtime with 43before starting your program, or change the logging level at runtime with
44something like: 44something like:
45 45
46 use AnyEvent::Log; 46 use AnyEvent::Log;
47 AnyEvent::Log::FILTER->level ("info"); 47 AnyEvent::Log::FILTER->level ("info");
48 48
244 }; 244 };
245 245
246 # format msg 246 # format msg
247 my $str = $ctx->[4] 247 my $str = $ctx->[4]
248 ? $ctx->[4]($now, $_[0], $level, $format) 248 ? $ctx->[4]($now, $_[0], $level, $format)
249 : $fmt ||= _format $now, $_[0], $level, $format; 249 : ($fmt ||= _format $now, $_[0], $level, $format);
250 250
251 $ctx->[3]($str) 251 $ctx->[3]($str)
252 or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate 252 or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate
253 } else { 253 } else {
254 push @ctx, values %{ $ctx->[2] }; # not masked - propagate 254 push @ctx, values %{ $ctx->[2] }; # not masked - propagate
734the logging (which consists of formatting the message and printing it or 734the logging (which consists of formatting the message and printing it or
735whatever it wants to do with it). 735whatever it wants to do with it).
736 736
737=over 4 737=over 4
738 738
739=item $ctx->log_cb ($cb->($str)) 739=item $ctx->log_cb ($cb->($str)
740 740
741Replaces the logging callback on the context (C<undef> disables the 741Replaces the logging callback on the context (C<undef> disables the
742logging callback). 742logging callback).
743 743
744The logging callback is responsible for handling formatted log messages 744The logging callback is responsible for handling formatted log messages
763your program. 763your program.
764 764
765 $ctx->levels ("debug", "trace"); 765 $ctx->levels ("debug", "trace");
766 $ctx->log_cb (sub { 1 }); # do not log, but eat debug and trace messages 766 $ctx->log_cb (sub { 1 }); # do not log, but eat debug and trace messages
767 767
768=item $ctx->log_to_file ($path)
769
770Sets the C<log_cb> to log to a file (by appending), unbuffered.
771
772=item $ctx->log_to_path ($path)
773
774Same as C<< ->log_to_file >>, but opens the file for each message. This
775is much slower, but allows you to change/move/rename/delete the file at
776basically any time.
777
778=item $ctx->fmt_cb ($fmt_cb->($timestamp, $ctx, $level, $message)) 768=item $ctx->fmt_cb ($fmt_cb->($timestamp, $orig_ctx, $level, $message))
779 769
780Replaces the formatting callback on the context (C<undef> restores the 770Replaces the formatting callback on the context (C<undef> restores the
781default formatter). 771default formatter).
782 772
783The callback is passed the (possibly fractional) timestamp, the original 773The callback is passed the (possibly fractional) timestamp, the original
813 "$msg->[3]"; 803 "$msg->[3]";
814 804
815 0 805 0
816 }); 806 });
817 807
808=item $ctx->log_to_file ($path)
809
810Sets the C<log_cb> to log to a file (by appending), unbuffered.
811
812=item $ctx->log_to_path ($path)
813
814Same as C<< ->log_to_file >>, but opens the file for each message. This
815is much slower, but allows you to change/move/rename/delete the file at
816basically any time.
817
818=item $ctx->log_to_syslog ([$log_flags])
819
820Logs all messages via L<Sys::Syslog>, mapping C<trace> to C<debug> and all
821the others in the obvious way. If specified, then the C<$log_flags> are
822simply or'ed onto the priority argument and can contain any C<LOG_xxx>
823flags valid for Sys::Syslog::syslog, except for the priority levels.
824
825Note that this function also sets a C<fmt_cb> - the logging part requires
826an array reference with [$level, $str] as input.
827
818=cut 828=cut
819 829
820sub log_cb { 830sub log_cb {
821 my ($ctx, $cb) = @_; 831 my ($ctx, $cb) = @_;
822 832
851 syswrite $fh, shift; 861 syswrite $fh, shift;
852 0 862 0
853 }); 863 });
854} 864}
855 865
866sub log_to_syslog {
867 my ($ctx, $flags) = @_;
868
869 require Sys::Syslog;
870
871 $ctx->fmt_cb (sub {
872 my $str = $_[3];
873 $str =~ s/\n(?=.)/\n+ /g;
874
875 [$_[2], "($_[1][0]) $str"]
876 });
877
878 $ctx->log_cb (sub {
879 my $lvl = $_[0][0] < 9 ? $_[0][0] : 8;
880
881 Sys::Syslog::syslog ($flags | ($lvl - 1), $_)
882 for split /\n/, $_[0][1];
883
884 0
885 });
886}
887
856=back 888=back
857 889
858=head3 MESSAGE LOGGING 890=head3 MESSAGE LOGGING
859 891
860These methods allow you to log messages directly to a context, without 892These methods allow you to log messages directly to a context, without

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines