ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Log.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Log.pm (file contents):
Revision 1.64 by root, Sun Mar 13 04:48:43 2016 UTC vs.
Revision 1.73 by root, Sun Apr 24 21:22:38 2022 UTC

8 8
9 use AnyEvent; 9 use AnyEvent;
10 10
11 AE::log fatal => "No config found, cannot continue!"; # never returns 11 AE::log fatal => "No config found, cannot continue!"; # never returns
12 AE::log alert => "The battery died!"; 12 AE::log alert => "The battery died!";
13 AE::log crit => "The battery temperature is too hot!"; 13 AE::log crit => "The battery is too hot!";
14 AE::log error => "Division by zero attempted."; 14 AE::log error => "Division by zero attempted.";
15 AE::log warn => "Couldn't delete the file."; 15 AE::log warn => "Couldn't delete the file.";
16 AE::log note => "Wanted to create config, but config already exists."; 16 AE::log note => "Attempted to create config, but config already exists.";
17 AE::log info => "File soandso successfully deleted."; 17 AE::log info => "File soandso successfully deleted.";
18 AE::log debug => "the function returned 3"; 18 AE::log debug => "the function returned 3";
19 AE::log trace => "going to call function abc"; 19 AE::log trace => "going to call function abc";
20 20
21Log level overview: 21Log level overview:
33 33
34"Complex" uses (for speed sensitive code, e.g. trace/debug messages): 34"Complex" uses (for speed sensitive code, e.g. trace/debug messages):
35 35
36 use AnyEvent::Log; 36 use AnyEvent::Log;
37 37
38 my $tracer = AnyEvent::Log::logger trace => \$my $trace; 38 my $tracer = AnyEvent::Log::logger trace => \my $trace;
39 39
40 $tracer->("i am here") if $trace; 40 $tracer->("i am here") if $trace;
41 $tracer->(sub { "lots of data: " . Dumper $self }) if $trace; 41 $tracer->(sub { "lots of data: " . Dumper $self }) if $trace;
42 42
43Configuration (also look at the EXAMPLES section): 43Configuration (also look at the EXAMPLES section):
44 44
45 # set default logging level to suppress anything below "notice"
46 # i.e. enable logging at "notice" or above - the default is to
47 # to not log anything at all.
48 $AnyEvent::Log::FILTER->level ("notice");
49
45 # set logging for the current package to errors and higher only 50 # set logging for the current package to errors and higher only
46 AnyEvent::Log::ctx->level ("error"); 51 AnyEvent::Log::ctx->level ("error");
47 52
48 # set logging level to suppress anything below "notice" 53 # enable logging for the current package, regardless of global logging level
49 $AnyEvent::Log::FILTER->level ("notice"); 54 AnyEvent::Log::ctx->attach ($AnyEvent::Log::LOG);
55
56 # enable debug logging for module some::mod and enable logging by default
57 (AnyEvent::Log::ctx "some::mod")->level ("debug");
58 (AnyEvent::Log::ctx "some::mod")->attach ($AnyEvent::Log::LOG);
50 59
51 # send all critical and higher priority messages to syslog, 60 # send all critical and higher priority messages to syslog,
52 # regardless of (most) other settings 61 # regardless of (most) other settings
53 $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx 62 $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx
54 level => "critical", 63 level => "critical",
121For example, a program that finds an unknown switch on the commandline 130For example, a program that finds an unknown switch on the commandline
122might well use a fatal logging level to tell users about it - the "system" 131might well use a fatal logging level to tell users about it - the "system"
123in this case would be the program, or module. 132in this case would be the program, or module.
124 133
125Some methods also offer some extra levels, such as C<0>, C<off>, C<none> 134Some methods also offer some extra levels, such as C<0>, C<off>, C<none>
126or C<all> - these are only valid for the methods that documented them. 135or C<all> - these are only valid for the methods that document them.
127 136
128=head1 LOGGING FUNCTIONS 137=head1 LOGGING FUNCTIONS
129 138
130The following functions allow you to log messages. They always use the 139The following functions allow you to log messages. They always use the
131caller's package as a "logging context". Also, the main logging function, 140caller's package as a "logging context". Also, the main logging function,
449time on each log message. This only makes a difference for event loops 458time on each log message. This only makes a difference for event loops
450that actually cache the time (such as L<EV> or L<AnyEvent::Loop>). 459that actually cache the time (such as L<EV> or L<AnyEvent::Loop>).
451 460
452This setting can be changed at any time by calling this function. 461This setting can be changed at any time by calling this function.
453 462
454Since C<AnyEvent::Log> has to work even before the L<AnyEvent> has been 463Since C<AnyEvent::Log> has to work even before L<AnyEvent> has been
455initialised, this switch will also decide whether to use C<CORE::time> or 464initialised, this switch will also decide whether to use C<CORE::time> or
456C<Time::HiRes::time> when logging a message before L<AnyEvent> becomes 465C<Time::HiRes::time> when logging a message before L<AnyEvent> becomes
457available. 466available.
458 467
459=item AnyEvent::Log::format_time $timestamp 468=item AnyEvent::Log::format_time $timestamp
478 487
479In your main program (as opposed to in your module) you can override the 488In your main program (as opposed to in your module) you can override the
480default message format by loading this module and then redefining this 489default message format by loading this module and then redefining this
481function. 490function.
482 491
483=item AnyEvent::Log::fatal_exit 492=item AnyEvent::Log::fatal_exit()
484 493
485This is the function that is called after logging a C<fatal> log 494This is the function that is called after logging a C<fatal> log
486message. It must not return. 495message. It must not return.
487 496
488The default implementation simply calls C<exit 1>. 497The default implementation simply calls C<exit 1>.
521context, so it does not matter (much) if there are cycles or if the 530context, so it does not matter (much) if there are cycles or if the
522message can arrive at the same context via multiple paths. 531message can arrive at the same context via multiple paths.
523 532
524=head2 DEFAULTS 533=head2 DEFAULTS
525 534
526By default, all logging contexts have an full set of log levels ("all"), a 535By default, all logging contexts have a full set of log levels ("all"), a
527disabled logging callback and the default formatting callback. 536disabled logging callback and the default formatting callback.
528 537
529Package contexts have the package name as logging title by default. 538Package contexts have the package name as logging title by default.
530 539
531They have exactly one slave - the context of the "parent" package. The 540They have exactly one slave - the context of the "parent" package. The
585 594
586=item $ctx = AnyEvent::Log::ctx [$pkg] 595=item $ctx = AnyEvent::Log::ctx [$pkg]
587 596
588This function creates or returns a logging context (which is an object). 597This function creates or returns a logging context (which is an object).
589 598
590If a package name is given, then the context for that packlage is 599If a package name is given, then the context for that package is
591returned. If it is called without any arguments, then the context for the 600returned. If it is called without any arguments, then the context for the
592callers package is returned (i.e. the same context as a C<AE::log> call 601callers package is returned (i.e. the same context as a C<AE::log> call
593would use). 602would use).
594 603
595If C<undef> is given, then it creates a new anonymous context that is not 604If C<undef> is given, then it creates a new anonymous context that is not
875sub attach { 884sub attach {
876 my $ctx = shift; 885 my $ctx = shift;
877 886
878 $ctx->[2]{$_+0} = $_ 887 $ctx->[2]{$_+0} = $_
879 for map { AnyEvent::Log::ctx $_ } @_; 888 for map { AnyEvent::Log::ctx $_ } @_;
889 AnyEvent::Log::_reassess;
880} 890}
881 891
882sub detach { 892sub detach {
883 my $ctx = shift; 893 my $ctx = shift;
884 894
885 delete $ctx->[2]{$_+0} 895 delete $ctx->[2]{$_+0}
886 for map { AnyEvent::Log::ctx $_ } @_; 896 for map { AnyEvent::Log::ctx $_ } @_;
897 AnyEvent::Log::_reassess;
887} 898}
888 899
889sub slaves { 900sub slaves {
890 undef $_[0][2]; 901 undef $_[0][2];
891 &attach; 902 &attach;
903 AnyEvent::Log::_reassess;
892} 904}
893 905
894=back 906=back
895 907
896=head3 LOG TARGETS 908=head3 LOG TARGETS

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines