… | |
… | |
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 uses: |
|
|
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" uses (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 (also look at the EXAMPLES section): |
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"); |
30 | |
32 | |
31 | # see also EXAMPLES, below |
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 | ); |
32 | |
39 | |
33 | =head1 DESCRIPTION |
40 | =head1 DESCRIPTION |
34 | |
41 | |
35 | This module implements a relatively simple "logging framework". It doesn't |
42 | This module implements a relatively simple "logging framework". It doesn't |
36 | attempt to be "the" logging solution or even "a" logging solution for |
43 | attempt to be "the" logging solution or even "a" logging solution for |
37 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
44 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
38 | module more or less exposes the mechanism, with some extra spiff to allow |
45 | module more or less exposes the mechanism, with some extra spiff to allow |
39 | using it from other modules as well. |
46 | using it from other modules as well. |
40 | |
47 | |
41 | Remember that the default verbosity level is C<0>, so nothing will be |
48 | 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 |
49 | 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 |
50 | before starting your program, or change the logging level at runtime with |
44 | something like: |
51 | something like: |
45 | |
52 | |
46 | use AnyEvent::Log; |
53 | use AnyEvent::Log; |
47 | AnyEvent::Log::FILTER->level ("info"); |
54 | AnyEvent::Log::FILTER->level ("info"); |
48 | |
55 | |
… | |
… | |
138 | $ctx |
145 | $ctx |
139 | } |
146 | } |
140 | |
147 | |
141 | =item AnyEvent::Log::log $level, $msg[, @args] |
148 | =item AnyEvent::Log::log $level, $msg[, @args] |
142 | |
149 | |
143 | Requests logging of the given C<$msg> with the given log level. |
150 | Requests logging of the given C<$msg> with the given log level, and |
|
|
151 | returns true if the message was logged I<somewhere>. |
144 | |
152 | |
145 | For C<fatal> log levels, the program will abort. |
153 | For C<fatal> log levels, the program will abort. |
146 | |
154 | |
147 | If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the |
155 | 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. |
156 | C<$msg> is interpreted as an sprintf format string. |
… | |
… | |
154 | supposed to return the message. It will be called only then the message |
162 | 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 |
163 | actually gets logged, which is useful if it is costly to create the |
156 | message in the first place. |
164 | message in the first place. |
157 | |
165 | |
158 | Whether the given message will be logged depends on the maximum log level |
166 | Whether the given message will be logged depends on the maximum log level |
159 | and the caller's package. |
167 | and the caller's package. The return value can be used to ensure that |
|
|
168 | messages or not "lost" - for example, when L<AnyEvent::Debug> detects a |
|
|
169 | runtime error it tries to log it at C<die> level, but if that message is |
|
|
170 | lost it simply uses warn. |
160 | |
171 | |
161 | Note that you can (and should) call this function as C<AnyEvent::log> or |
172 | 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 |
173 | 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 |
174 | need any additional functionality), as those functions will load the |
164 | logging module on demand only. They are also much shorter to write. |
175 | logging module on demand only. They are also much shorter to write. |
… | |
… | |
224 | ? $level+0 |
235 | ? $level+0 |
225 | : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; |
236 | : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; |
226 | |
237 | |
227 | my $mask = 1 << $level; |
238 | my $mask = 1 << $level; |
228 | |
239 | |
229 | my (%seen, @ctx, $now, $fmt); |
240 | my ($success, %seen, @ctx, $now, $fmt); |
230 | |
241 | |
231 | do |
242 | do |
232 | { |
243 | { |
233 | # skip if masked |
244 | # skip if masked |
234 | if ($ctx->[1] & $mask && !$seen{$ctx+0}++) { |
245 | if ($ctx->[1] & $mask && !$seen{$ctx+0}++) { |
… | |
… | |
244 | }; |
255 | }; |
245 | |
256 | |
246 | # format msg |
257 | # format msg |
247 | my $str = $ctx->[4] |
258 | my $str = $ctx->[4] |
248 | ? $ctx->[4]($now, $_[0], $level, $format) |
259 | ? $ctx->[4]($now, $_[0], $level, $format) |
249 | : $fmt ||= _format $now, $_[0], $level, $format; |
260 | : ($fmt ||= _format $now, $_[0], $level, $format); |
|
|
261 | |
|
|
262 | $success = 1; |
250 | |
263 | |
251 | $ctx->[3]($str) |
264 | $ctx->[3]($str) |
252 | or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate |
265 | or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate |
253 | } else { |
266 | } else { |
254 | push @ctx, values %{ $ctx->[2] }; # not masked - propagate |
267 | push @ctx, values %{ $ctx->[2] }; # not masked - propagate |
… | |
… | |
256 | } |
269 | } |
257 | } |
270 | } |
258 | while $ctx = pop @ctx; |
271 | while $ctx = pop @ctx; |
259 | |
272 | |
260 | exit 1 if $level <= 1; |
273 | exit 1 if $level <= 1; |
|
|
274 | |
|
|
275 | $success |
261 | } |
276 | } |
262 | |
277 | |
263 | sub log($$;@) { |
278 | sub log($$;@) { |
264 | _log |
279 | _log |
265 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
280 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
… | |
… | |
269 | *AnyEvent::log = *AE::log = \&log; |
284 | *AnyEvent::log = *AE::log = \&log; |
270 | |
285 | |
271 | =item $logger = AnyEvent::Log::logger $level[, \$enabled] |
286 | =item $logger = AnyEvent::Log::logger $level[, \$enabled] |
272 | |
287 | |
273 | Creates a code reference that, when called, acts as if the |
288 | 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 |
289 | 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 |
290 | level. C<$logger> is passed a C<$msg> and optional C<@args>, just as with |
276 | the C<AnyEvent::Log::log> function: |
291 | the C<AnyEvent::Log::log> function: |
277 | |
292 | |
278 | my $debug_log = AnyEvent::Log::logger "debug"; |
293 | my $debug_log = AnyEvent::Log::logger "debug"; |
279 | |
294 | |
… | |
… | |
734 | the logging (which consists of formatting the message and printing it or |
749 | the logging (which consists of formatting the message and printing it or |
735 | whatever it wants to do with it). |
750 | whatever it wants to do with it). |
736 | |
751 | |
737 | =over 4 |
752 | =over 4 |
738 | |
753 | |
739 | =item $ctx->log_cb ($cb->($str)) |
754 | =item $ctx->log_cb ($cb->($str) |
740 | |
755 | |
741 | Replaces the logging callback on the context (C<undef> disables the |
756 | Replaces the logging callback on the context (C<undef> disables the |
742 | logging callback). |
757 | logging callback). |
743 | |
758 | |
744 | The logging callback is responsible for handling formatted log messages |
759 | The logging callback is responsible for handling formatted log messages |
… | |
… | |
763 | your program. |
778 | your program. |
764 | |
779 | |
765 | $ctx->levels ("debug", "trace"); |
780 | $ctx->levels ("debug", "trace"); |
766 | $ctx->log_cb (sub { 1 }); # do not log, but eat debug and trace messages |
781 | $ctx->log_cb (sub { 1 }); # do not log, but eat debug and trace messages |
767 | |
782 | |
768 | =item $ctx->log_to_file ($path) |
|
|
769 | |
|
|
770 | Sets the C<log_cb> to log to a file (by appending), unbuffered. |
|
|
771 | |
|
|
772 | =item $ctx->log_to_path ($path) |
|
|
773 | |
|
|
774 | 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 |
|
|
776 | basically any time. |
|
|
777 | |
|
|
778 | =item $ctx->fmt_cb ($fmt_cb->($timestamp, $ctx, $level, $message)) |
783 | =item $ctx->fmt_cb ($fmt_cb->($timestamp, $orig_ctx, $level, $message)) |
779 | |
784 | |
780 | Replaces the formatting callback on the context (C<undef> restores the |
785 | Replaces the formatting callback on the context (C<undef> restores the |
781 | default formatter). |
786 | default formatter). |
782 | |
787 | |
783 | The callback is passed the (possibly fractional) timestamp, the original |
788 | The callback is passed the (possibly fractional) timestamp, the original |
784 | logging context, the (numeric) logging level and the raw message string |
789 | logging context, the (numeric) logging level and the raw message string |
785 | and needs to return a formatted log message. In most cases this will be a |
790 | and needs to return a formatted log message. In most cases this will be a |
786 | string, but it could just as well be an array reference that just stores |
791 | string, but it could just as well be an array reference that just stores |
787 | the values. |
792 | the values. |
788 | |
793 | |
789 | If, for some reaosn, you want to use C<caller> to find out more baout the |
794 | If, for some reason, you want to use C<caller> to find out more baout the |
790 | logger then you should walk up the call stack until you are no longer |
795 | logger then you should walk up the call stack until you are no longer |
791 | inside the C<AnyEvent::Log> package. |
796 | inside the C<AnyEvent::Log> package. |
792 | |
797 | |
793 | Example: format just the raw message, with numeric log level in angle |
798 | Example: format just the raw message, with numeric log level in angle |
794 | brackets. |
799 | brackets. |
… | |
… | |
813 | "$msg->[3]"; |
818 | "$msg->[3]"; |
814 | |
819 | |
815 | 0 |
820 | 0 |
816 | }); |
821 | }); |
817 | |
822 | |
|
|
823 | =item $ctx->log_to_file ($path) |
|
|
824 | |
|
|
825 | Sets the C<log_cb> to log to a file (by appending), unbuffered. |
|
|
826 | |
|
|
827 | =item $ctx->log_to_path ($path) |
|
|
828 | |
|
|
829 | Same as C<< ->log_to_file >>, but opens the file for each message. This |
|
|
830 | is much slower, but allows you to change/move/rename/delete the file at |
|
|
831 | basically any time. |
|
|
832 | |
|
|
833 | =item $ctx->log_to_syslog ([$log_flags]) |
|
|
834 | |
|
|
835 | Logs all messages via L<Sys::Syslog>, mapping C<trace> to C<debug> and all |
|
|
836 | the others in the obvious way. If specified, then the C<$log_flags> are |
|
|
837 | simply or'ed onto the priority argument and can contain any C<LOG_xxx> |
|
|
838 | flags valid for Sys::Syslog::syslog, except for the priority levels. |
|
|
839 | |
|
|
840 | Note that this function also sets a C<fmt_cb> - the logging part requires |
|
|
841 | an array reference with [$level, $str] as input. |
|
|
842 | |
818 | =cut |
843 | =cut |
819 | |
844 | |
820 | sub log_cb { |
845 | sub log_cb { |
821 | my ($ctx, $cb) = @_; |
846 | my ($ctx, $cb) = @_; |
822 | |
847 | |
… | |
… | |
851 | syswrite $fh, shift; |
876 | syswrite $fh, shift; |
852 | 0 |
877 | 0 |
853 | }); |
878 | }); |
854 | } |
879 | } |
855 | |
880 | |
|
|
881 | sub log_to_syslog { |
|
|
882 | my ($ctx, $flags) = @_; |
|
|
883 | |
|
|
884 | require Sys::Syslog; |
|
|
885 | |
|
|
886 | $ctx->fmt_cb (sub { |
|
|
887 | my $str = $_[3]; |
|
|
888 | $str =~ s/\n(?=.)/\n+ /g; |
|
|
889 | |
|
|
890 | [$_[2], "($_[1][0]) $str"] |
|
|
891 | }); |
|
|
892 | |
|
|
893 | $ctx->log_cb (sub { |
|
|
894 | my $lvl = $_[0][0] < 9 ? $_[0][0] : 8; |
|
|
895 | |
|
|
896 | Sys::Syslog::syslog ($flags | ($lvl - 1), $_) |
|
|
897 | for split /\n/, $_[0][1]; |
|
|
898 | |
|
|
899 | 0 |
|
|
900 | }); |
|
|
901 | } |
|
|
902 | |
856 | =back |
903 | =back |
857 | |
904 | |
858 | =head3 MESSAGE LOGGING |
905 | =head3 MESSAGE LOGGING |
859 | |
906 | |
860 | These methods allow you to log messages directly to a context, without |
907 | These methods allow you to log messages directly to a context, without |