… | |
… | |
36 | attempt to be "the" logging solution or even "a" logging solution for |
36 | attempt to be "the" logging solution or even "a" logging solution for |
37 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
37 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
38 | module more or less exposes the mechanism, with some extra spiff to allow |
38 | module more or less exposes the mechanism, with some extra spiff to allow |
39 | using it from other modules as well. |
39 | using it from other modules as well. |
40 | |
40 | |
41 | Remember that the default verbosity level is C<0>, so nothing will be |
41 | 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 |
42 | 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 |
43 | before starting your program, or change the logging level at runtime with |
44 | something like: |
44 | something 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, $_[0], $level) |
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 |
255 | } |
255 | } |
256 | } |
256 | } |
… | |
… | |
734 | the logging (which consists of formatting the message and printing it or |
734 | the logging (which consists of formatting the message and printing it or |
735 | whatever it wants to do with it). |
735 | whatever 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, $orig_ctx, $level)) |
740 | |
740 | |
741 | Replaces the logging callback on the context (C<undef> disables the |
741 | Replaces the logging callback on the context (C<undef> disables the |
742 | logging callback). |
742 | logging callback). |
743 | |
743 | |
744 | The logging callback is responsible for handling formatted log messages |
744 | The logging callback is responsible for handling formatted log messages |
745 | (see C<fmt_cb> below) - normally simple text strings that end with a |
745 | (see C<fmt_cb> below) - normally simple text strings that end with a |
746 | newline (and are possibly multiline themselves). |
746 | newline (and are possibly multiline themselves). In addition to the |
|
|
747 | message, which is often the only argument you need to look at, it is |
|
|
748 | passed the numeric log level and originating context. |
747 | |
749 | |
748 | It also has to return true iff it has consumed the log message, and false |
750 | It also has to return true iff it has consumed the log message, and false |
749 | if it hasn't. Consuming a message means that it will not be sent to any |
751 | if it hasn't. Consuming a message means that it will not be sent to any |
750 | slave context. When in doubt, return C<0> from your logging callback. |
752 | slave context. When in doubt, return C<0> from your logging callback. |
751 | |
753 | |
… | |
… | |
773 | |
775 | |
774 | Same as C<< ->log_to_file >>, but opens the file for each message. This |
776 | Same as C<< ->log_to_file >>, but opens the file for each message. This |
775 | is much slower, but allows you to change/move/rename/delete the file at |
777 | is much slower, but allows you to change/move/rename/delete the file at |
776 | basically any time. |
778 | basically any time. |
777 | |
779 | |
|
|
780 | =item $ctx->log_to_syslog ([$log_flags]) |
|
|
781 | |
|
|
782 | Logs all messages via L<Sys::Syslog>, mapping C<trace> to C<debug> and all |
|
|
783 | the others in the obvious way. If specified, then the C<$log_flags> are |
|
|
784 | simply or'ed onto the priority argument and can contain any C<LOG_xxx> |
|
|
785 | flags valid for Sys::Syslog::syslog, except for the priority levels. |
|
|
786 | |
|
|
787 | Note that the default logging format includes a verbose timestamp, which |
|
|
788 | is not so suited for syslog, so a simpler C<fmt_cb> might be useful: |
|
|
789 | |
|
|
790 | $ctx->log_to_syslog; |
|
|
791 | $ctx->fmt_cb (sub { "($_[1][0]) $_[3]" }); |
|
|
792 | |
778 | =item $ctx->fmt_cb ($fmt_cb->($timestamp, $ctx, $level, $message)) |
793 | =item $ctx->fmt_cb ($fmt_cb->($timestamp, $orig_ctx, $level, $message)) |
779 | |
794 | |
780 | Replaces the formatting callback on the context (C<undef> restores the |
795 | Replaces the formatting callback on the context (C<undef> restores the |
781 | default formatter). |
796 | default formatter). |
782 | |
797 | |
783 | The callback is passed the (possibly fractional) timestamp, the original |
798 | The callback is passed the (possibly fractional) timestamp, the original |
… | |
… | |
851 | syswrite $fh, shift; |
866 | syswrite $fh, shift; |
852 | 0 |
867 | 0 |
853 | }); |
868 | }); |
854 | } |
869 | } |
855 | |
870 | |
|
|
871 | sub log_to_syslog { |
|
|
872 | my ($ctx, $flags) = @_; |
|
|
873 | |
|
|
874 | require Sys::Syslog; |
|
|
875 | |
|
|
876 | $ctx->log_cb (sub { |
|
|
877 | my $lvl = $_[2] < 9 ? $_[2] : 8; |
|
|
878 | |
|
|
879 | Sys::Syslog::syslog ($flags | ($lvl - 1), $_) |
|
|
880 | for split /\n/, shift; |
|
|
881 | |
|
|
882 | 0 |
|
|
883 | }); |
|
|
884 | } |
|
|
885 | |
856 | =back |
886 | =back |
857 | |
887 | |
858 | =head3 MESSAGE LOGGING |
888 | =head3 MESSAGE LOGGING |
859 | |
889 | |
860 | These methods allow you to log messages directly to a context, without |
890 | These methods allow you to log messages directly to a context, without |