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.63 by root, Wed Oct 29 20:41:17 2014 UTC vs.
Revision 1.71 by root, Mon Aug 3 20:53:18 2020 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,
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>.
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
1227=item C<%name> 1239=item C<%name>
1228 1240
1229Context names starting with a C<%> are anonymous contexts created when the 1241Context names starting with a C<%> are anonymous contexts created when the
1230name is first mentioned. The difference to package contexts is that by 1242name is first mentioned. The difference to package contexts is that by
1231default they have no attached slaves. 1243default they have no attached slaves.
1244
1245This makes it possible to create new log contexts that can be refered to
1246multiple times by name within the same log specification.
1232 1247
1233=item a perl package name 1248=item a perl package name
1234 1249
1235Any other string references the logging context associated with the given 1250Any other string references the logging context associated with the given
1236Perl C<package>. In the unlikely case where you want to specify a package 1251Perl C<package>. In the unlikely case where you want to specify a package

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines