… | |
… | |
10 | AE::log debug => "hit my knee"; |
10 | AE::log debug => "hit my knee"; |
11 | AE::log warn => "it's a bit too hot"; |
11 | AE::log warn => "it's a bit too hot"; |
12 | AE::log error => "the flag was false!"; |
12 | AE::log error => "the flag was false!"; |
13 | AE::log fatal => "the bit toggled! run!"; |
13 | AE::log fatal => "the bit toggled! run!"; |
14 | |
14 | |
15 | # complex use |
15 | # "complex" use |
16 | use AnyEvent::Log; |
16 | use AnyEvent::Log; |
17 | |
17 | |
18 | my $tracer = AnyEvent::Log::logger trace => \$my $trace; |
18 | my $tracer = AnyEvent::Log::logger trace => \$my $trace; |
19 | |
19 | |
20 | $tracer->("i am here") if $trace; |
20 | $tracer->("i am here") if $trace; |
21 | $tracer->(sub { "lots of data: " . Dumper $self }) if $trace; |
21 | $tracer->(sub { "lots of data: " . Dumper $self }) if $trace; |
22 | |
22 | |
23 | #TODO: config |
23 | # configuration |
24 | #TODO: ctx () becomes caller[0]... |
24 | |
|
|
25 | # set logging for this package to maximum |
|
|
26 | AnyEvent::Log::ctx->level ("all"); |
|
|
27 | |
|
|
28 | # set logging globally to anything below debug |
|
|
29 | (AnyEvent::Log::ctx "")->level ("notice"); |
|
|
30 | |
|
|
31 | # see also EXAMPLES, below |
|
|
32 | |
|
|
33 | # disable logging for package "AnyEvent" and all packages below it |
|
|
34 | AnyEvent->AnyEvent::Log::ctx->level (0); |
|
|
35 | |
|
|
36 | # log everything below debug to a file, for the whole program |
|
|
37 | my $ctx = AnyEvent::Log::ctx; |
|
|
38 | $ctx->log_cb (sub { print FILE shift; 0 }); |
|
|
39 | (AnyEvent::Log::ctx "")->add ($ctx); |
25 | |
40 | |
26 | =head1 DESCRIPTION |
41 | =head1 DESCRIPTION |
27 | |
42 | |
28 | This module implements a relatively simple "logging framework". It doesn't |
43 | This module implements a relatively simple "logging framework". It doesn't |
29 | attempt to be "the" logging solution or even "a" logging solution for |
44 | attempt to be "the" logging solution or even "a" logging solution for |
30 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
45 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
31 | module more or less exposes the mechanism, with some extra spiff to allow |
46 | module more or less exposes the mechanism, with some extra spiff to allow |
32 | using it from other modules as well. |
47 | using it from other modules as well. |
33 | |
48 | |
34 | Remember that the default verbosity level is C<0>, so nothing will be |
49 | Remember that the default verbosity level is C<0>, so nothing will be |
35 | logged, ever, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number |
50 | logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number before |
36 | before starting your program.#TODO |
51 | starting your program, or change the logging level at runtime with |
|
|
52 | something like: |
37 | |
53 | |
38 | Possible future extensions are to allow custom log targets (where the |
54 | use AnyEvent; |
39 | level is an object), log filtering based on package, formatting, aliasing |
55 | (AnyEvent::Log::ctx "")->level ("info"); |
40 | or package groups. |
|
|
41 | |
56 | |
|
|
57 | The design goal behind this module was to keep it simple (and small), |
|
|
58 | but make it powerful enough to be potentially useful for any module, and |
|
|
59 | extensive enough for the most common tasks, such as logging to multiple |
|
|
60 | targets, or being able to log into a database. |
|
|
61 | |
42 | =head1 LOG FUNCTIONS |
62 | =head1 LOGGING FUNCTIONS |
43 | |
63 | |
44 | These functions allow you to log messages. They always use the caller's |
64 | These functions allow you to log messages. They always use the caller's |
45 | package as a "logging module/source". Also, the main logging function is |
65 | package as a "logging module/source". Also, the main logging function is |
46 | callable as C<AnyEvent::log> or C<AE::log> when the C<AnyEvent> module is |
66 | callable as C<AnyEvent::log> or C<AE::log> when the C<AnyEvent> module is |
47 | loaded. |
67 | loaded. |
… | |
… | |
71 | "$now_str1$f$now_str2" |
91 | "$now_str1$f$now_str2" |
72 | } |
92 | } |
73 | |
93 | |
74 | our %CTX; # all logging contexts |
94 | our %CTX; # all logging contexts |
75 | |
95 | |
76 | my $default_log_cb = sub { 0 }; |
|
|
77 | |
|
|
78 | # creates a default package context object for the given package |
96 | # creates a default package context object for the given package |
79 | sub _pkg_ctx($) { |
97 | sub _pkg_ctx($) { |
80 | my $ctx = bless [$_[0], 0, {}, $default_log_cb], "AnyEvent::Log::Ctx"; |
98 | my $ctx = bless [$_[0], (1 << 10) - 1 - 1, {}], "AnyEvent::Log::Ctx"; |
81 | |
99 | |
82 | # link "parent" package |
100 | # link "parent" package |
83 | my $pkg = $_[0] =~ /^(.+)::/ ? $1 : ""; |
101 | my $pkg = $_[0] =~ /^(.+)::/ ? $1 : "AE::Log::Top"; |
84 | |
102 | |
85 | $pkg = $CTX{$pkg} ||= &_pkg_ctx ($pkg); |
103 | $pkg = $CTX{$pkg} ||= &_pkg_ctx ($pkg); |
86 | $ctx->[2]{$pkg+0} = $pkg; |
104 | $ctx->[2]{$pkg+0} = $pkg; |
87 | |
105 | |
88 | $ctx |
106 | $ctx |
… | |
… | |
114 | Note that you can (and should) call this function as C<AnyEvent::log> or |
132 | Note that you can (and should) call this function as C<AnyEvent::log> or |
115 | C<AE::log>, without C<use>-ing this module if possible (i.e. you don't |
133 | C<AE::log>, without C<use>-ing this module if possible (i.e. you don't |
116 | need any additional functionality), as those functions will load the |
134 | need any additional functionality), as those functions will load the |
117 | logging module on demand only. They are also much shorter to write. |
135 | logging module on demand only. They are also much shorter to write. |
118 | |
136 | |
119 | Also, if you otpionally generate a lot of debug messages (such as when |
137 | Also, if you optionally generate a lot of debug messages (such as when |
120 | tracing some code), you should look into using a logger callback and a |
138 | tracing some code), you should look into using a logger callback and a |
121 | boolean enabler (see C<logger>, below). |
139 | boolean enabler (see C<logger>, below). |
122 | |
140 | |
123 | Example: log something at error level. |
141 | Example: log something at error level. |
124 | |
142 | |
… | |
… | |
146 | debug => 8, |
164 | debug => 8, |
147 | trace => 9, |
165 | trace => 9, |
148 | ); |
166 | ); |
149 | |
167 | |
150 | sub now () { time } |
168 | sub now () { time } |
|
|
169 | |
151 | AnyEvent::post_detect { |
170 | AnyEvent::post_detect { |
152 | *now = \&AE::now; |
171 | *now = \&AE::now; |
153 | }; |
172 | }; |
154 | |
173 | |
155 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
174 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
156 | |
175 | |
157 | # time, ctx, level, msg |
176 | # time, ctx, level, msg |
158 | sub _format($$$$) { |
177 | sub _format($$$$) { |
159 | my $pfx = ft $_[0]; |
178 | my $ts = ft $_[0]; |
|
|
179 | my $ct = " "; |
160 | |
180 | |
161 | join "", |
181 | my @res; |
162 | map "$pfx $_\n", |
182 | |
163 | split /\n/, |
|
|
164 | sprintf "%-5s %s: %s", $LEVEL2STR[$_[2]], $_[1][0], $_[3] |
183 | for (split /\n/, sprintf "%-5s %s: %s", $LEVEL2STR[$_[2]], $_[1][0], $_[3]) { |
|
|
184 | push @res, "$ts$ct$_\n"; |
|
|
185 | $ct = " + "; |
|
|
186 | } |
|
|
187 | |
|
|
188 | join "", @res |
165 | } |
189 | } |
166 | |
190 | |
167 | sub _log { |
191 | sub _log { |
168 | my ($ctx, $level, $format, @args) = @_; |
192 | my ($ctx, $level, $format, @args) = @_; |
169 | |
193 | |
|
|
194 | $level = $level > 0 && $level <= 9 |
|
|
195 | ? $level+0 |
170 | $level = $level > 0 && $level <= 9 ? $level+0 : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; |
196 | : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; |
171 | |
197 | |
172 | my $mask = 1 << $level; |
198 | my $mask = 1 << $level; |
173 | my $now = AE::now; |
|
|
174 | |
199 | |
175 | my (@ctx, $did_format, $fmt); |
200 | my (%seen, @ctx, $now, $fmt); |
176 | |
201 | |
177 | do { |
202 | do |
178 | if ($ctx->[1] & $mask) { |
203 | { |
|
|
204 | # skip if masked |
|
|
205 | if ($ctx->[1] & $mask && !$seen{$ctx+0}++) { |
|
|
206 | if ($ctx->[3]) { |
179 | # logging target found |
207 | # logging target found |
180 | |
208 | |
181 | # get raw message |
209 | # now get raw message, unless we have it already |
182 | unless ($did_format) { |
210 | unless ($now) { |
183 | $format = $format->() if ref $format; |
211 | $format = $format->() if ref $format; |
184 | $format = sprintf $format, @args if @args; |
212 | $format = sprintf $format, @args if @args; |
185 | $format =~ s/\n$//; |
213 | $format =~ s/\n$//; |
186 | $did_format = 1; |
214 | $now = AE::now; |
187 | }; |
215 | }; |
188 | |
216 | |
189 | # format msg |
217 | # format msg |
190 | my $str = $ctx->[4] |
218 | my $str = $ctx->[4] |
191 | ? $ctx->[4]($now, $_[0], $level, $format) |
219 | ? $ctx->[4]($now, $_[0], $level, $format) |
192 | : $fmt ||= _format $now, $_[0], $level, $format; |
220 | : $fmt ||= _format $now, $_[0], $level, $format; |
193 | |
221 | |
194 | $ctx->[3]($str) |
222 | $ctx->[3]($str); |
195 | and next; |
223 | } |
|
|
224 | |
|
|
225 | # not masked, not consumed - propagate to parent contexts |
|
|
226 | push @ctx, values %{ $ctx->[2] }; |
|
|
227 | } |
196 | } |
228 | } |
197 | |
|
|
198 | # not consume - push parent contexts |
|
|
199 | push @ctx, values %{ $ctx->[2] }; |
|
|
200 | } while $ctx = pop @ctx; |
229 | while $ctx = pop @ctx; |
201 | |
230 | |
202 | exit 1 if $level <= 1; |
231 | exit 1 if $level <= 1; |
203 | } |
232 | } |
204 | |
233 | |
205 | sub log($$;@) { |
234 | sub log($$;@) { |
… | |
… | |
258 | # re-assess logging status for all loggers |
287 | # re-assess logging status for all loggers |
259 | sub _reassess { |
288 | sub _reassess { |
260 | for (@_ ? $LOGGER{$_[0]} : values %LOGGER) { |
289 | for (@_ ? $LOGGER{$_[0]} : values %LOGGER) { |
261 | my ($ctx, $level, $renabled) = @$_; |
290 | my ($ctx, $level, $renabled) = @$_; |
262 | |
291 | |
263 | # to detetc whether a message would be logged, we # actually |
292 | # to detect whether a message would be logged, we # actually |
264 | # try to log one and die. this isn't # fast, but we can be |
293 | # try to log one and die. this isn't fast, but we can be |
265 | # sure that the logging decision is correct :) |
294 | # sure that the logging decision is correct :) |
266 | |
295 | |
267 | $$renabled = !eval { |
296 | $$renabled = !eval { |
268 | local $SIG{__DIE__}; |
297 | local $SIG{__DIE__}; |
269 | |
298 | |
… | |
… | |
306 | _logger |
335 | _logger |
307 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
336 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
308 | @_ |
337 | @_ |
309 | } |
338 | } |
310 | |
339 | |
311 | #TODO |
|
|
312 | |
|
|
313 | =back |
340 | =back |
314 | |
341 | |
315 | =head1 CONFIGURATION FUNCTIONALITY |
342 | =head1 LOGGING CONTEXTS |
316 | |
343 | |
317 | None, yet, except for C<PERL_ANYEVENT_VERBOSE>, described in the L<AnyEvent> manpage. |
344 | This module associates every log message with a so-called I<logging |
|
|
345 | context>, based on the package of the caller. Every perl package has its |
|
|
346 | own logging context. |
318 | |
347 | |
319 | #TODO: wahst a context |
348 | A logging context has three major responsibilities: filtering, logging and |
320 | #TODO |
349 | propagating the message. |
|
|
350 | |
|
|
351 | For the first purpose, filtering, each context has a set of logging |
|
|
352 | levels, called the log level mask. Messages not in the set will be ignored |
|
|
353 | by this context (masked). |
|
|
354 | |
|
|
355 | For logging, the context stores a formatting callback (which takes the |
|
|
356 | timestamp, context, level and string message and formats it in the way |
|
|
357 | it should be logged) and a logging callback (which is responsible for |
|
|
358 | actually logging the formatted message and telling C<AnyEvent::Log> |
|
|
359 | whether it has consumed the message, or whether it should be propagated). |
|
|
360 | |
|
|
361 | For propagation, a context can have any number of attached I<parent |
|
|
362 | contexts>. Any message that is neither masked by the logging mask nor |
|
|
363 | masked by the logging callback returning true will be passed to all parent |
|
|
364 | contexts. |
|
|
365 | |
|
|
366 | Each call to a logging function will log the message at most once per |
|
|
367 | context, so it does not matter (much) if there are cycles or if the |
|
|
368 | message can arrive at the same context via multiple paths. |
|
|
369 | |
|
|
370 | =head2 DEFAULTS |
|
|
371 | |
|
|
372 | By default, all logging contexts have an full set of log levels ("all"), a |
|
|
373 | disabled logging callback and the default formatting callback. |
|
|
374 | |
|
|
375 | Package contexts have the package name as logging title by default. |
|
|
376 | |
|
|
377 | They have exactly one parent - the context of the "parent" package. The |
|
|
378 | parent package is simply defined to be the package name without the last |
|
|
379 | component, i.e. C<AnyEvent::Debug::Wrapped> becomes C<AnyEvent::Debug>, |
|
|
380 | and C<AnyEvent> becomes ... C<AnyEvent::Log::Top> which is the |
|
|
381 | exception of the rule - just like the parent of any package name in |
|
|
382 | Perl is C<main>, the default parent of any top-level package context is |
|
|
383 | C<AnyEvent::Log::Top>. |
|
|
384 | |
|
|
385 | Since perl packages form only an approximate hierarchy, this parent |
|
|
386 | context can of course be removed. |
|
|
387 | |
|
|
388 | All other (anonymous) contexts have no parents and an empty title by |
|
|
389 | default. |
|
|
390 | |
|
|
391 | When the module is loaded it creates the default context called |
|
|
392 | C<AnyEvent::Log::Default> (also stored in C<$AnyEvent::Log::Default>), |
|
|
393 | which simply logs everything to STDERR and doesn't propagate anything |
|
|
394 | anywhere by default. The purpose of the default context is to provide |
|
|
395 | a convenient place to override the global logging target or to attach |
|
|
396 | additional log targets. It's not meant for filtering. |
|
|
397 | |
|
|
398 | It then creates the root context called C<AnyEvent::Log::Root> (also |
|
|
399 | stored in C<$AnyEvent::Log::Root>) and sets its log level set to all |
|
|
400 | levels up to the one specified by C<$ENV{PERL_ANYEVENT_VERBOSE}>. It |
|
|
401 | then attached the default logging context to it. The purpose of the root |
|
|
402 | context is to simply provide filtering according to some global log level. |
|
|
403 | |
|
|
404 | Finally it creates the top-level package context called |
|
|
405 | C<AnyEvent::Log::Top> (also stored in, you might have guessed, |
|
|
406 | C<$AnyEvent::Log::Top>) and attached the root context but otherwise leaves |
|
|
407 | it at default config. It's purpose is simply to collect all log messages |
|
|
408 | system-wide. |
|
|
409 | |
|
|
410 | These three special contexts can also be referred to by the |
|
|
411 | package/context names C<AE::Log::Default>, C<AE::Log::Root> and |
|
|
412 | C<AE::Log::Top>. |
|
|
413 | |
|
|
414 | The effect of all this is that log messages, by default, wander up |
|
|
415 | to the root context where log messages with lower priority then |
|
|
416 | C<$ENV{PERL_ANYEVENT_VERBOSE}> will be filtered away and then to the |
|
|
417 | AnyEvent::Log::Default context to be logged to STDERR. |
|
|
418 | |
|
|
419 | Splitting the top level context into three contexts makes it easy to set |
|
|
420 | a global logging level (by modifying the root context), but still allow |
|
|
421 | other contexts to log, for example, their debug and trace messages to the |
|
|
422 | default target despite the global logging level, or to attach additional |
|
|
423 | log targets that log messages, regardless of the global logging level. |
|
|
424 | |
|
|
425 | It also makes it easy to replace the default STDERR-logger by something |
|
|
426 | that logs to a file, or to attach additional logging targets. |
|
|
427 | |
|
|
428 | =head2 CREATING/FINDING/DESTROYING CONTEXTS |
321 | |
429 | |
322 | =over 4 |
430 | =over 4 |
323 | |
431 | |
324 | =item $ctx = AnyEvent::Log::ctx [$pkg] |
432 | =item $ctx = AnyEvent::Log::ctx [$pkg] |
325 | |
433 | |
326 | Returns a I<config> object for the given package name. |
434 | This function creates or returns a logging context (which is an object). |
327 | |
435 | |
328 | If no package name is given, returns the context for the current perl |
436 | If a package name is given, then the context for that packlage is |
|
|
437 | returned. If it is called without any arguments, then the context for the |
329 | package (i.e. the same context as a C<AE::log> call would use). |
438 | callers package is returned (i.e. the same context as a C<AE::log> call |
|
|
439 | would use). |
330 | |
440 | |
331 | If C<undef> is given, then it creates a new anonymous context that is not |
441 | If C<undef> is given, then it creates a new anonymous context that is not |
332 | tied to any package and is destroyed when no longer referenced. |
442 | tied to any package and is destroyed when no longer referenced. |
333 | |
443 | |
334 | =cut |
444 | =cut |
… | |
… | |
338 | |
448 | |
339 | ref $pkg |
449 | ref $pkg |
340 | ? $pkg |
450 | ? $pkg |
341 | : defined $pkg |
451 | : defined $pkg |
342 | ? $CTX{$pkg} ||= AnyEvent::Log::_pkg_ctx $pkg |
452 | ? $CTX{$pkg} ||= AnyEvent::Log::_pkg_ctx $pkg |
343 | : bless [undef, 0, undef, $default_log_cb], "AnyEvent::Log::Ctx" |
453 | : bless [undef, (1 << 10) - 1 - 1], "AnyEvent::Log::Ctx" |
344 | } |
454 | } |
345 | |
455 | |
346 | # create default root context |
456 | =item AnyEvent::Log::reset |
347 | { |
457 | |
|
|
458 | Deletes all contexts and recreates the default hierarchy, i.e. resets the |
|
|
459 | logging subsystem to defaults. |
|
|
460 | |
|
|
461 | This can be used to implement config-file (re-)loading: before loading a |
|
|
462 | configuration, reset all contexts. |
|
|
463 | |
|
|
464 | =cut |
|
|
465 | |
|
|
466 | sub reset { |
|
|
467 | @$_ = () for values %CTX; # just to be sure - to kill circular logging dependencies |
|
|
468 | %CTX = (); |
|
|
469 | |
348 | my $root = ctx undef; |
470 | my $default = ctx undef; |
349 | $root->[0] = ""; |
471 | $default->title ("AnyEvent::Log::Default"); |
350 | $root->title ("default"); |
|
|
351 | $root->level ($AnyEvent::VERBOSE); |
|
|
352 | $root->log_cb (sub { |
472 | $default->log_cb (sub { |
353 | print STDERR shift; |
473 | print STDERR shift; |
354 | 0 |
474 | 0 |
355 | }); |
475 | }); |
356 | $CTX{""} = $root; |
476 | $AnyEvent::Log::Default = $CTX{"AnyEvent::Log::Default"} = $CTX{"AE::Log::Default"} = $default; |
|
|
477 | |
|
|
478 | my $root = ctx undef; |
|
|
479 | $root->title ("AnyEvent::Log::Root"); |
|
|
480 | $root->level ($AnyEvent::VERBOSE); |
|
|
481 | $root->attach ($default); |
|
|
482 | $AnyEvent::Log::Root = $CTX{"AnyEvent::Log::Root"} = $CTX{"AE::Log::Root"} = $root; |
|
|
483 | |
|
|
484 | my $top = ctx undef; |
|
|
485 | $top->title ("AnyEvent::Log::Top"); |
|
|
486 | $top->attach ($root); |
|
|
487 | $AnyEvent::Log::Top = $CTX{"AnyEvent::Log::Top"} = $CTX{"AE::Log::Top"} = $top; |
357 | } |
488 | } |
|
|
489 | |
|
|
490 | AnyEvent::Log::reset; |
|
|
491 | |
|
|
492 | # hello, CPAN, please catch me |
|
|
493 | package AnyEvent::Log::Default; |
|
|
494 | package AE::Log::Default; |
|
|
495 | package AnyEvent::Log::Root; |
|
|
496 | package AE::Log::Root; |
|
|
497 | package AnyEvent::Log::Top; |
|
|
498 | package AE::Log::Top; |
358 | |
499 | |
359 | package AnyEvent::Log::Ctx; |
500 | package AnyEvent::Log::Ctx; |
360 | |
501 | |
361 | # 0 1 2 3 4 |
502 | # 0 1 2 3 4 |
362 | # [$title, $level, %$parents, &$logcb, &$fmtcb] |
503 | # [$title, $level, %$parents, &$logcb, &$fmtcb] |
|
|
504 | |
|
|
505 | =item $ctx = new AnyEvent::Log::Ctx methodname => param... |
|
|
506 | |
|
|
507 | This is a convenience constructor that makes it simpler to construct |
|
|
508 | anonymous logging contexts. |
|
|
509 | |
|
|
510 | Each key-value pair results in an invocation of the method of the same |
|
|
511 | name as the key with the value as parameter, unless the value is an |
|
|
512 | arrayref, in which case it calls the method with the contents of the |
|
|
513 | array. The methods are called in the same order as specified. |
|
|
514 | |
|
|
515 | Example: create a new logging context and set both the default logging |
|
|
516 | level, some parent contexts and a logging callback. |
|
|
517 | |
|
|
518 | $ctx = new AnyEvent::Log::Ctx |
|
|
519 | title => "dubious messages", |
|
|
520 | level => "error", |
|
|
521 | log_cb => sub { print STDOUT shift; 0 }, |
|
|
522 | parents => [$ctx1, $ctx, $ctx2], |
|
|
523 | ; |
|
|
524 | |
|
|
525 | =back |
|
|
526 | |
|
|
527 | =cut |
|
|
528 | |
|
|
529 | sub new { |
|
|
530 | my $class = shift; |
|
|
531 | |
|
|
532 | my $ctx = AnyEvent::Log::ctx undef; |
|
|
533 | |
|
|
534 | while (@_) { |
|
|
535 | my ($k, $v) = splice @_, 0, 2; |
|
|
536 | $ctx->$k (ref $v eq "ARRAY" ? @$v : $v); |
|
|
537 | } |
|
|
538 | |
|
|
539 | bless $ctx, $class # do we really support subclassing, hmm? |
|
|
540 | } |
|
|
541 | |
|
|
542 | |
|
|
543 | =head2 CONFIGURING A LOG CONTEXT |
|
|
544 | |
|
|
545 | The following methods can be used to configure the logging context. |
|
|
546 | |
|
|
547 | =over 4 |
363 | |
548 | |
364 | =item $ctx->title ([$new_title]) |
549 | =item $ctx->title ([$new_title]) |
365 | |
550 | |
366 | Returns the title of the logging context - this is the package name, for |
551 | Returns the title of the logging context - this is the package name, for |
367 | package contexts, and a user defined string for all others. |
552 | package contexts, and a user defined string for all others. |
… | |
… | |
373 | sub title { |
558 | sub title { |
374 | $_[0][0] = $_[1] if @_ > 1; |
559 | $_[0][0] = $_[1] if @_ > 1; |
375 | $_[0][0] |
560 | $_[0][0] |
376 | } |
561 | } |
377 | |
562 | |
|
|
563 | =back |
|
|
564 | |
|
|
565 | =head3 LOGGING LEVELS |
|
|
566 | |
|
|
567 | The following methods deal with the logging level set associated with the |
|
|
568 | log context. |
|
|
569 | |
|
|
570 | The most common method to use is probably C<< $ctx->level ($level) >>, |
|
|
571 | which configures the specified and any higher priority levels. |
|
|
572 | |
|
|
573 | All functions which accept a list of levels also accept the special string |
|
|
574 | C<all> which expands to all logging levels. |
|
|
575 | |
|
|
576 | =over 4 |
|
|
577 | |
378 | =item $ctx->levels ($level[, $level...) |
578 | =item $ctx->levels ($level[, $level...) |
379 | |
579 | |
380 | Enables logging fot the given levels and disables it for all others. |
580 | Enables logging for the given levels and disables it for all others. |
381 | |
581 | |
382 | =item $ctx->level ($level) |
582 | =item $ctx->level ($level) |
383 | |
583 | |
384 | Enables logging for the given level and all lower level (higher priority) |
584 | Enables logging for the given level and all lower level (higher priority) |
385 | ones. Specifying a level of C<0> or C<off> disables all logging for this |
585 | ones. In addition to normal logging levels, specifying a level of C<0> or |
386 | level. |
586 | C<off> disables all logging for this level. |
387 | |
587 | |
388 | Example: log warnings, errors and higher priority messages. |
588 | Example: log warnings, errors and higher priority messages. |
389 | |
589 | |
390 | $ctx->level ("warn"); |
590 | $ctx->level ("warn"); |
391 | $ctx->level (5); # same thing, just numeric |
591 | $ctx->level (5); # same thing, just numeric |
… | |
… | |
399 | Disables logging for the given levels, leaving all others unchanged. |
599 | Disables logging for the given levels, leaving all others unchanged. |
400 | |
600 | |
401 | =cut |
601 | =cut |
402 | |
602 | |
403 | sub _lvl_lst { |
603 | sub _lvl_lst { |
|
|
604 | map { |
|
|
605 | $_ > 0 && $_ <= 9 ? $_+0 |
|
|
606 | : $_ eq "all" ? (1 .. 9) |
404 | map { $_ > 0 && $_ <= 9 ? $_+0 : $STR2LEVEL{$_} || Carp::croak "$_: not a valid logging level, caught" } |
607 | : $STR2LEVEL{$_} || Carp::croak "$_: not a valid logging level, caught" |
405 | @_ |
608 | } @_ |
406 | } |
609 | } |
407 | |
610 | |
408 | our $NOP_CB = sub { 0 }; |
611 | our $NOP_CB = sub { 0 }; |
409 | |
612 | |
410 | sub levels { |
613 | sub levels { |
… | |
… | |
415 | AnyEvent::Log::_reassess; |
618 | AnyEvent::Log::_reassess; |
416 | } |
619 | } |
417 | |
620 | |
418 | sub level { |
621 | sub level { |
419 | my $ctx = shift; |
622 | my $ctx = shift; |
420 | my $lvl = $_[0] =~ /^(?:0|off|none)$/ ? 0 : (_lvl_lst $_[0])[0]; |
623 | my $lvl = $_[0] =~ /^(?:0|off|none)$/ ? 0 : (_lvl_lst $_[0])[-1]; |
|
|
624 | |
421 | $ctx->[1] = ((1 << $lvl) - 1) << 1; |
625 | $ctx->[1] = ((1 << $lvl) - 1) << 1; |
422 | AnyEvent::Log::_reassess; |
626 | AnyEvent::Log::_reassess; |
423 | } |
627 | } |
424 | |
628 | |
425 | sub enable { |
629 | sub enable { |
… | |
… | |
434 | $ctx->[1] &= ~(1 << $_) |
638 | $ctx->[1] &= ~(1 << $_) |
435 | for &_lvl_lst; |
639 | for &_lvl_lst; |
436 | AnyEvent::Log::_reassess; |
640 | AnyEvent::Log::_reassess; |
437 | } |
641 | } |
438 | |
642 | |
|
|
643 | =back |
|
|
644 | |
|
|
645 | =head3 PARENT CONTEXTS |
|
|
646 | |
|
|
647 | The following methods attach and detach another logging context to a |
|
|
648 | logging context. |
|
|
649 | |
|
|
650 | Log messages are propagated to all parent contexts, unless the logging |
|
|
651 | callback consumes the message. |
|
|
652 | |
|
|
653 | =over 4 |
|
|
654 | |
439 | =item $ctx->attach ($ctx2[, $ctx3...]) |
655 | =item $ctx->attach ($ctx2[, $ctx3...]) |
440 | |
656 | |
441 | Attaches the given contexts as parents to this context. It is not an error |
657 | Attaches the given contexts as parents to this context. It is not an error |
442 | to add a context twice (the second add will be ignored). |
658 | to add a context twice (the second add will be ignored). |
443 | |
659 | |
… | |
… | |
448 | Removes the given parents from this context - it's not an error to attempt |
664 | Removes the given parents from this context - it's not an error to attempt |
449 | to remove a context that hasn't been added. |
665 | to remove a context that hasn't been added. |
450 | |
666 | |
451 | A context can be specified either as package name or as a context object. |
667 | A context can be specified either as package name or as a context object. |
452 | |
668 | |
|
|
669 | =item $ctx->parents ($ctx2[, $ctx3...]) |
|
|
670 | |
|
|
671 | Replaces all parents attached to this context by the ones given. |
|
|
672 | |
453 | =cut |
673 | =cut |
454 | |
674 | |
455 | sub attach { |
675 | sub attach { |
456 | my $ctx = shift; |
676 | my $ctx = shift; |
457 | |
677 | |
… | |
… | |
463 | my $ctx = shift; |
683 | my $ctx = shift; |
464 | |
684 | |
465 | delete $ctx->[2]{$_+0} |
685 | delete $ctx->[2]{$_+0} |
466 | for map { AnyEvent::Log::ctx $_ } @_; |
686 | for map { AnyEvent::Log::ctx $_ } @_; |
467 | } |
687 | } |
|
|
688 | |
|
|
689 | sub parents { |
|
|
690 | undef $_[0][2]; |
|
|
691 | &attach; |
|
|
692 | } |
|
|
693 | |
|
|
694 | =back |
|
|
695 | |
|
|
696 | =head3 MESSAGE LOGGING |
|
|
697 | |
|
|
698 | The following methods configure how the logging context actually does |
|
|
699 | the logging (which consists of formatting the message and printing it or |
|
|
700 | whatever it wants to do with it) and also allows you to log messages |
|
|
701 | directly to a context, without going via your package context. |
|
|
702 | |
|
|
703 | =over 4 |
468 | |
704 | |
469 | =item $ctx->log_cb ($cb->($str)) |
705 | =item $ctx->log_cb ($cb->($str)) |
470 | |
706 | |
471 | Replaces the logging callback on the context (C<undef> disables the |
707 | Replaces the logging callback on the context (C<undef> disables the |
472 | logging callback). |
708 | logging callback). |
… | |
… | |
482 | Example: a very simple logging callback, simply dump the message to STDOUT |
718 | Example: a very simple logging callback, simply dump the message to STDOUT |
483 | and do not consume it. |
719 | and do not consume it. |
484 | |
720 | |
485 | $ctx->log_cb (sub { print STDERR shift; 0 }); |
721 | $ctx->log_cb (sub { print STDERR shift; 0 }); |
486 | |
722 | |
|
|
723 | You can filter messages by having a log callback that simply returns C<1> |
|
|
724 | and does not do anything with the message, but this counts as "message |
|
|
725 | being logged" and might not be very efficient. |
|
|
726 | |
|
|
727 | Example: propagate all messages except for log levels "debug" and |
|
|
728 | "trace". The messages will still be generated, though, which can slow down |
|
|
729 | your program. |
|
|
730 | |
|
|
731 | $ctx->levels ("debug", "trace"); |
|
|
732 | $ctx->log_cb (sub { 1 }); # do not log, but eat debug and trace messages |
|
|
733 | |
487 | =item $ctx->fmt_cb ($fmt_cb->($timestamp, $ctx, $level, $message)) |
734 | =item $ctx->fmt_cb ($fmt_cb->($timestamp, $ctx, $level, $message)) |
488 | |
735 | |
489 | Replaces the fornatting callback on the cobntext (C<undef> restores the |
736 | Replaces the formatting callback on the context (C<undef> restores the |
490 | default formatter). |
737 | default formatter). |
491 | |
738 | |
492 | The callback is passed the (possibly fractional) timestamp, the original |
739 | The callback is passed the (possibly fractional) timestamp, the original |
493 | logging context, the (numeric) logging level and the raw message string and needs to |
740 | logging context, the (numeric) logging level and the raw message string and needs to |
494 | return a formatted log message. In most cases this will be a string, but |
741 | return a formatted log message. In most cases this will be a string, but |
… | |
… | |
522 | =cut |
769 | =cut |
523 | |
770 | |
524 | sub log_cb { |
771 | sub log_cb { |
525 | my ($ctx, $cb) = @_; |
772 | my ($ctx, $cb) = @_; |
526 | |
773 | |
527 | $ctx->[3] = $cb || $default_log_cb; |
774 | $ctx->[3] = $cb; |
528 | } |
775 | } |
529 | |
776 | |
530 | sub fmt_cb { |
777 | sub fmt_cb { |
531 | my ($ctx, $cb) = @_; |
778 | my ($ctx, $cb) = @_; |
532 | |
779 | |
… | |
… | |
549 | |
796 | |
550 | 1; |
797 | 1; |
551 | |
798 | |
552 | =back |
799 | =back |
553 | |
800 | |
|
|
801 | =head1 EXAMPLES |
|
|
802 | |
|
|
803 | This section shows some common configurations. |
|
|
804 | |
|
|
805 | =over 4 |
|
|
806 | |
|
|
807 | =item Setting the global logging level. |
|
|
808 | |
|
|
809 | Either put PERL_ANYEVENT_VERBOSE=<number> into your environment before |
|
|
810 | running your program, or modify the log level of the root context: |
|
|
811 | |
|
|
812 | PERL_ANYEVENT_VERBOSE=5 ./myprog |
|
|
813 | |
|
|
814 | $AnyEvent::Log::Root->level ("warn"); |
|
|
815 | |
|
|
816 | =item Append all messages to a file instead of sending them to STDERR. |
|
|
817 | |
|
|
818 | This is affected by the global logging level. |
|
|
819 | |
|
|
820 | open my $fh, ">>", $path |
|
|
821 | or die "$path: $!"; |
|
|
822 | |
|
|
823 | $AnyEvent::Log::Default->log_cb (sub { |
|
|
824 | syswrite $fh, shift; |
|
|
825 | 0 |
|
|
826 | }); |
|
|
827 | |
|
|
828 | =item Write all messages with priority C<error> and higher to a file. |
|
|
829 | |
|
|
830 | This writes them only when the global logging level allows it, because |
|
|
831 | it is attached to the default context which is invoked I<after> global |
|
|
832 | filtering. |
|
|
833 | |
|
|
834 | open my $fh, ">>", $path |
|
|
835 | or die "$path: $!"; |
|
|
836 | |
|
|
837 | $AnyEvent::Log::Default->attach (new AnyEvent::Log::Ctx |
|
|
838 | log_cb => sub { syswrite $fh, shift; 0 }); |
|
|
839 | |
|
|
840 | This writes them regardless of the global logging level, because it is |
|
|
841 | attached to the toplevel context, which receives all messages I<before> |
|
|
842 | the global filtering. |
|
|
843 | |
|
|
844 | $AnyEvent::Log::Top->attach (new AnyEvent::Log::Ctx |
|
|
845 | log_cb => sub { syswrite $fh, shift; 0 }); |
|
|
846 | |
|
|
847 | In both cases, messages are still written to STDOUT. |
|
|
848 | |
|
|
849 | =item Write trace messages (only) from L<AnyEvent::Debug> to the default logging target(s). |
|
|
850 | |
|
|
851 | Attach the CyAnyEvent::Log::Default> context to the C<AnyEvent::Debug> |
|
|
852 | context and increase the C<AnyEvent::Debug> logging level - this simply |
|
|
853 | circumvents the global filtering for trace messages. |
|
|
854 | |
|
|
855 | my $debug = AnyEvent::Debug->AnyEvent::Log::ctx; |
|
|
856 | $debug->attach ($AnyEvent::Log::Default); |
|
|
857 | $debug->levels ("trace"); # not "level"! |
|
|
858 | |
|
|
859 | This of course works for any package. |
|
|
860 | |
|
|
861 | =back |
|
|
862 | |
554 | =head1 AUTHOR |
863 | =head1 AUTHOR |
555 | |
864 | |
556 | Marc Lehmann <schmorp@schmorp.de> |
865 | Marc Lehmann <schmorp@schmorp.de> |
557 | http://home.schmorp.de/ |
866 | http://home.schmorp.de/ |
558 | |
867 | |