… | |
… | |
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 |
… | |
… | |
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}++) { |
… | |
… | |
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 | |
… | |
… | |
774 | logging context, the (numeric) logging level and the raw message string |
789 | logging context, the (numeric) logging level and the raw message string |
775 | 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 |
776 | 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 |
777 | the values. |
792 | the values. |
778 | |
793 | |
779 | 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 |
780 | 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 |
781 | inside the C<AnyEvent::Log> package. |
796 | inside the C<AnyEvent::Log> package. |
782 | |
797 | |
783 | 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 |
784 | brackets. |
799 | brackets. |