… | |
… | |
31 | module more or less exposes the mechanism, with some extra spiff to allow |
31 | module more or less exposes the mechanism, with some extra spiff to allow |
32 | using it from other modules as well. |
32 | using it from other modules as well. |
33 | |
33 | |
34 | Remember that the default verbosity level is C<0>, so nothing will be |
34 | 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 |
35 | logged, ever, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number |
36 | before starting your program.#TODO |
36 | before starting your program, or change the logging level at runtime wiht |
|
|
37 | something like: |
37 | |
38 | |
38 | Possible future extensions are to allow custom log targets (where the |
39 | use AnyEvent; |
39 | level is an object), log filtering based on package, formatting, aliasing |
40 | (AnyEvent::Log::ctx "")->level ("info"); |
40 | or package groups. |
|
|
41 | |
41 | |
42 | =head1 LOG FUNCTIONS |
42 | =head1 LOGGING FUNCTIONS |
43 | |
43 | |
44 | These functions allow you to log messages. They always use the caller's |
44 | 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 |
45 | 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 |
46 | callable as C<AnyEvent::log> or C<AE::log> when the C<AnyEvent> module is |
47 | loaded. |
47 | loaded. |
… | |
… | |
310 | |
310 | |
311 | #TODO |
311 | #TODO |
312 | |
312 | |
313 | =back |
313 | =back |
314 | |
314 | |
315 | =head1 CONFIGURATION FUNCTIONALITY |
315 | =head1 LOGGING CONTEXTS |
316 | |
316 | |
317 | None, yet, except for C<PERL_ANYEVENT_VERBOSE>, described in the L<AnyEvent> manpage. |
317 | This module associates every log message with a so-called I<logging |
|
|
318 | context>, based on the package of the caller. Every perl package has its |
|
|
319 | own logging context. |
318 | |
320 | |
319 | #TODO: wahst a context |
321 | A logging context has two major responsibilities: logging the message and |
320 | #TODO |
322 | propagating the message to other contexts. |
|
|
323 | |
|
|
324 | For logging, the context stores a set of logging levels that it |
|
|
325 | potentially wishes to log, a formatting callback that takes the timestamp, |
|
|
326 | context, level and string emssage and formats it in the way it should be |
|
|
327 | logged, and a logging callback, which is responsible for actually logging |
|
|
328 | the formatted message and telling C<AnyEvent::Log> whether it has consumed |
|
|
329 | the message, or whether it should be propagated. |
|
|
330 | |
|
|
331 | For propagation, a context can have any number of attached I<parent |
|
|
332 | contexts>. They will be ignored if the logging callback consumes the |
|
|
333 | message, but in all other cases, the log message will be passed to all |
|
|
334 | parent contexts attached to a context. |
|
|
335 | |
|
|
336 | =head2 DEFAULTS |
|
|
337 | |
|
|
338 | By default, all logging contexts have an empty set of log levels, a |
|
|
339 | disabled logging callback and the default formatting callback. |
|
|
340 | |
|
|
341 | Package contexts have the package name as logging title by default. |
|
|
342 | |
|
|
343 | They have exactly one parent - the context of the "parent" package. The |
|
|
344 | parent package is simply defined to be the package name without the last |
|
|
345 | component, i.e. C<AnyEvent::Debug::Wrapped> becomes C<AnyEvent::Debug>, |
|
|
346 | and C<AnyEvent> becomes the empty string. |
|
|
347 | |
|
|
348 | Since perl packages form only an approximate hierarchy, this parent |
|
|
349 | context can of course be removed. |
|
|
350 | |
|
|
351 | All other (anonymous) contexts have no parents and an empty title by |
|
|
352 | default. |
|
|
353 | |
|
|
354 | When the module is first loaded, it configures the root context (the one |
|
|
355 | with the empty string) to simply dump all log messages to C<STDERR>, |
|
|
356 | and sets it's log level set to all levels up to the one specified by |
|
|
357 | C<$ENV{PERL_ANYEVENT_VERBOSE}>. |
|
|
358 | |
|
|
359 | The effetc of all this is that log messages, by default, wander up to the |
|
|
360 | root context and will be logged to STDERR if their log level is less than |
|
|
361 | or equal to C<$ENV{PERL_ANYEVENT_VERBOSE}>. |
|
|
362 | |
|
|
363 | =head2 CREATING/FINDING A CONTEXT |
321 | |
364 | |
322 | =over 4 |
365 | =over 4 |
323 | |
366 | |
324 | =item $ctx = AnyEvent::Log::ctx [$pkg] |
367 | =item $ctx = AnyEvent::Log::ctx [$pkg] |
325 | |
368 | |
326 | Returns a I<config> object for the given package name. |
369 | This function creates or returns a logging context (which is an object). |
327 | |
370 | |
328 | If no package name is given, returns the context for the current perl |
371 | If a package name is given, then the context for that packlage is |
|
|
372 | 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). |
373 | callers package is returned (i.e. the same context as a C<AE::log> call |
|
|
374 | would use). |
330 | |
375 | |
331 | If C<undef> is given, then it creates a new anonymous context that is not |
376 | 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. |
377 | tied to any package and is destroyed when no longer referenced. |
333 | |
378 | |
334 | =cut |
379 | =cut |
… | |
… | |
346 | # create default root context |
391 | # create default root context |
347 | { |
392 | { |
348 | my $root = ctx undef; |
393 | my $root = ctx undef; |
349 | $root->[0] = ""; |
394 | $root->[0] = ""; |
350 | $root->title ("default"); |
395 | $root->title ("default"); |
351 | $root->level ($AnyEvent::VERBOSE); |
396 | $root->level ($AnyEvent::VERBOSE); undef $AnyEvent::VERBOSE; |
352 | $root->log_cb (sub { |
397 | $root->log_cb (sub { |
353 | print STDERR shift; |
398 | print STDERR shift; |
354 | 0 |
399 | 0 |
355 | }); |
400 | }); |
356 | $CTX{""} = $root; |
401 | $CTX{""} = $root; |
357 | } |
402 | } |
358 | |
403 | |
|
|
404 | =back |
|
|
405 | |
|
|
406 | =cut |
|
|
407 | |
359 | package AnyEvent::Log::Ctx; |
408 | package AnyEvent::Log::Ctx; |
360 | |
409 | |
361 | # 0 1 2 3 4 |
410 | # 0 1 2 3 4 |
362 | # [$title, $level, %$parents, &$logcb, &$fmtcb] |
411 | # [$title, $level, %$parents, &$logcb, &$fmtcb] |
|
|
412 | |
|
|
413 | =head2 CONFIGURING A LOG CONTEXT |
|
|
414 | |
|
|
415 | The following methods can be used to configure the logging context. |
|
|
416 | |
|
|
417 | =over 4 |
363 | |
418 | |
364 | =item $ctx->title ([$new_title]) |
419 | =item $ctx->title ([$new_title]) |
365 | |
420 | |
366 | Returns the title of the logging context - this is the package name, for |
421 | Returns the title of the logging context - this is the package name, for |
367 | package contexts, and a user defined string for all others. |
422 | package contexts, and a user defined string for all others. |
… | |
… | |
372 | |
427 | |
373 | sub title { |
428 | sub title { |
374 | $_[0][0] = $_[1] if @_ > 1; |
429 | $_[0][0] = $_[1] if @_ > 1; |
375 | $_[0][0] |
430 | $_[0][0] |
376 | } |
431 | } |
|
|
432 | |
|
|
433 | =back |
|
|
434 | |
|
|
435 | =head3 LOGGING LEVELS |
|
|
436 | |
|
|
437 | The following methods deal with the logging level set associated wiht the log context. |
|
|
438 | |
|
|
439 | The most common method to use is probably C<< $ctx->level ($level) >>, |
|
|
440 | which configures the specified and any higher priority levels. |
|
|
441 | |
|
|
442 | =over 4 |
377 | |
443 | |
378 | =item $ctx->levels ($level[, $level...) |
444 | =item $ctx->levels ($level[, $level...) |
379 | |
445 | |
380 | Enables logging fot the given levels and disables it for all others. |
446 | Enables logging fot the given levels and disables it for all others. |
381 | |
447 | |
… | |
… | |
434 | $ctx->[1] &= ~(1 << $_) |
500 | $ctx->[1] &= ~(1 << $_) |
435 | for &_lvl_lst; |
501 | for &_lvl_lst; |
436 | AnyEvent::Log::_reassess; |
502 | AnyEvent::Log::_reassess; |
437 | } |
503 | } |
438 | |
504 | |
|
|
505 | =back |
|
|
506 | |
|
|
507 | =head3 PARENT CONTEXTS |
|
|
508 | |
|
|
509 | The following methods attach and detach another logging context to a |
|
|
510 | logging context. |
|
|
511 | |
|
|
512 | Log messages are propagated to all parent contexts, unless the logging |
|
|
513 | callback consumes the message. |
|
|
514 | |
|
|
515 | =over 4 |
|
|
516 | |
439 | =item $ctx->attach ($ctx2[, $ctx3...]) |
517 | =item $ctx->attach ($ctx2[, $ctx3...]) |
440 | |
518 | |
441 | Attaches the given contexts as parents to this context. It is not an error |
519 | 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). |
520 | to add a context twice (the second add will be ignored). |
443 | |
521 | |
… | |
… | |
463 | my $ctx = shift; |
541 | my $ctx = shift; |
464 | |
542 | |
465 | delete $ctx->[2]{$_+0} |
543 | delete $ctx->[2]{$_+0} |
466 | for map { AnyEvent::Log::ctx $_ } @_; |
544 | for map { AnyEvent::Log::ctx $_ } @_; |
467 | } |
545 | } |
|
|
546 | |
|
|
547 | =back |
|
|
548 | |
|
|
549 | =head3 MESSAGE LOGGING |
|
|
550 | |
|
|
551 | The following methods configure how the logging context actually does |
|
|
552 | the logging (which consists of foratting the message and printing it or |
|
|
553 | whatever it wants to do with it) and also allows you to log messages |
|
|
554 | directly to a context, without going via your package context. |
|
|
555 | |
|
|
556 | =over 4 |
468 | |
557 | |
469 | =item $ctx->log_cb ($cb->($str)) |
558 | =item $ctx->log_cb ($cb->($str)) |
470 | |
559 | |
471 | Replaces the logging callback on the context (C<undef> disables the |
560 | Replaces the logging callback on the context (C<undef> disables the |
472 | logging callback). |
561 | logging callback). |