… | |
… | |
6 | |
6 | |
7 | Simple uses: |
7 | Simple uses: |
8 | |
8 | |
9 | use AnyEvent; |
9 | use AnyEvent; |
10 | |
10 | |
11 | AE::log debug => "hit my knee"; |
11 | AE::log fatal => "no config found, cannot continue"; # never returns |
12 | AE::log warn => "it's a bit too hot"; |
12 | AE::log alert => "the battery died"; |
13 | AE::log error => "the flag was false!"; |
13 | AE::log crit => "the battery temperature is too hot"; |
14 | AE::log fatal => "the bit toggled! run!"; # never returns |
14 | AE::log error => "division by zero attempted"; |
|
|
15 | AE::log warn => "couldn't delete the file"; |
|
|
16 | AE::log note => "wanted to create config, but config already exists"; |
|
|
17 | AE::log info => "file soandso successfully deleted"; |
|
|
18 | AE::log debug => "the function returned 3"; |
|
|
19 | AE::log trace => "going to call function abc"; |
15 | |
20 | |
16 | # available log levels in order: |
21 | Log level overview: |
17 | # fatal alert critical error warn note info debug trace |
|
|
18 | |
|
|
19 | "Complex" uses (for speed sensitive code): |
|
|
20 | |
|
|
21 | use AnyEvent::Log; |
|
|
22 | |
|
|
23 | my $tracer = AnyEvent::Log::logger trace => \$my $trace; |
|
|
24 | |
|
|
25 | $tracer->("i am here") if $trace; |
|
|
26 | $tracer->(sub { "lots of data: " . Dumper $self }) if $trace; |
|
|
27 | |
|
|
28 | Configuration (also look at the EXAMPLES section): |
|
|
29 | |
|
|
30 | # set logging for the current package to errors and higher only |
|
|
31 | AnyEvent::Log::ctx->level ("error"); |
|
|
32 | |
|
|
33 | # set logging level to suppress anything below "notice" |
|
|
34 | $AnyEvent::Log::FILTER->level ("notice"); |
|
|
35 | |
|
|
36 | # send all critical and higher priority messages to syslog, |
|
|
37 | # regardless of (most) other settings |
|
|
38 | $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx |
|
|
39 | level => "critical", |
|
|
40 | log_to_syslog => "user", |
|
|
41 | ); |
|
|
42 | |
|
|
43 | =head1 DESCRIPTION |
|
|
44 | |
|
|
45 | This module implements a relatively simple "logging framework". It doesn't |
|
|
46 | attempt to be "the" logging solution or even "a" logging solution for |
|
|
47 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
|
|
48 | module more or less exposes the mechanism, with some extra spiff to allow |
|
|
49 | using it from other modules as well. |
|
|
50 | |
|
|
51 | Remember that the default verbosity level is C<0> (C<off>), so nothing |
|
|
52 | will be logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number |
|
|
53 | before starting your program, or change the logging level at runtime with |
|
|
54 | something like: |
|
|
55 | |
|
|
56 | use AnyEvent::Log; |
|
|
57 | $AnyEvent::Log::FILTER->level ("info"); |
|
|
58 | |
|
|
59 | The design goal behind this module was to keep it simple (and small), |
|
|
60 | but make it powerful enough to be potentially useful for any module, and |
|
|
61 | extensive enough for the most common tasks, such as logging to multiple |
|
|
62 | targets, or being able to log into a database. |
|
|
63 | |
|
|
64 | The module is also usable before AnyEvent itself is initialised, in which |
|
|
65 | case some of the functionality might be reduced. |
|
|
66 | |
|
|
67 | The amount of documentation might indicate otherwise, but the runtime part |
|
|
68 | of the module is still just below 300 lines of code. |
|
|
69 | |
|
|
70 | =head1 LOGGING LEVELS |
|
|
71 | |
|
|
72 | Logging levels in this module range from C<1> (highest priority) to C<9> |
|
|
73 | (lowest priority). Note that the lowest numerical value is the highest |
|
|
74 | priority, so when this document says "higher priority" it means "lower |
|
|
75 | numerical value". |
|
|
76 | |
|
|
77 | Instead of specifying levels by name you can also specify them by aliases: |
|
|
78 | |
22 | |
79 | LVL NAME SYSLOG PERL NOTE |
23 | LVL NAME SYSLOG PERL NOTE |
80 | 1 fatal emerg exit system unusable, aborts program! |
24 | 1 fatal emerg exit system unusable, aborts program! |
81 | 2 alert failure in primary system |
25 | 2 alert failure in primary system |
82 | 3 critical crit failure in backup system |
26 | 3 critical crit failure in backup system |
… | |
… | |
85 | 6 note notice unusual conditions |
29 | 6 note notice unusual conditions |
86 | 7 info normal messages, no action required |
30 | 7 info normal messages, no action required |
87 | 8 debug debugging messages for development |
31 | 8 debug debugging messages for development |
88 | 9 trace copious tracing output |
32 | 9 trace copious tracing output |
89 | |
33 | |
|
|
34 | "Complex" uses (for speed sensitive code, e.g. trace/debug messages): |
|
|
35 | |
|
|
36 | use AnyEvent::Log; |
|
|
37 | |
|
|
38 | my $tracer = AnyEvent::Log::logger trace => \$my $trace; |
|
|
39 | |
|
|
40 | $tracer->("i am here") if $trace; |
|
|
41 | $tracer->(sub { "lots of data: " . Dumper $self }) if $trace; |
|
|
42 | |
|
|
43 | Configuration (also look at the EXAMPLES section): |
|
|
44 | |
|
|
45 | # set logging for the current package to errors and higher only |
|
|
46 | AnyEvent::Log::ctx->level ("error"); |
|
|
47 | |
|
|
48 | # set logging level to suppress anything below "notice" |
|
|
49 | $AnyEvent::Log::FILTER->level ("notice"); |
|
|
50 | |
|
|
51 | # send all critical and higher priority messages to syslog, |
|
|
52 | # regardless of (most) other settings |
|
|
53 | $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx |
|
|
54 | level => "critical", |
|
|
55 | log_to_syslog => "user", |
|
|
56 | ); |
|
|
57 | |
|
|
58 | =head1 DESCRIPTION |
|
|
59 | |
|
|
60 | This module implements a relatively simple "logging framework". It doesn't |
|
|
61 | attempt to be "the" logging solution or even "a" logging solution for |
|
|
62 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
|
|
63 | module more or less exposes the mechanism, with some extra spiff to allow |
|
|
64 | using it from other modules as well. |
|
|
65 | |
|
|
66 | Remember that the default verbosity level is C<4> (C<error>), so only |
|
|
67 | errors and more important messages will be logged, unless you set |
|
|
68 | C<PERL_ANYEVENT_VERBOSE> to a higher number before starting your program |
|
|
69 | (C<AE_VERBOSE=5> is recommended during development), or change the logging |
|
|
70 | level at runtime with something like: |
|
|
71 | |
|
|
72 | use AnyEvent::Log; |
|
|
73 | $AnyEvent::Log::FILTER->level ("info"); |
|
|
74 | |
|
|
75 | The design goal behind this module was to keep it simple (and small), |
|
|
76 | but make it powerful enough to be potentially useful for any module, and |
|
|
77 | extensive enough for the most common tasks, such as logging to multiple |
|
|
78 | targets, or being able to log into a database. |
|
|
79 | |
|
|
80 | The module is also usable before AnyEvent itself is initialised, in which |
|
|
81 | case some of the functionality might be reduced. |
|
|
82 | |
|
|
83 | The amount of documentation might indicate otherwise, but the runtime part |
|
|
84 | of the module is still just below 300 lines of code. |
|
|
85 | |
|
|
86 | =head1 LOGGING LEVELS |
|
|
87 | |
|
|
88 | Logging levels in this module range from C<1> (highest priority) to C<9> |
|
|
89 | (lowest priority). Note that the lowest numerical value is the highest |
|
|
90 | priority, so when this document says "higher priority" it means "lower |
|
|
91 | numerical value". |
|
|
92 | |
|
|
93 | Instead of specifying levels by name you can also specify them by aliases: |
|
|
94 | |
|
|
95 | LVL NAME SYSLOG PERL NOTE |
|
|
96 | 1 fatal emerg exit system unusable, aborts program! |
|
|
97 | 2 alert failure in primary system |
|
|
98 | 3 critical crit failure in backup system |
|
|
99 | 4 error err die non-urgent program errors, a bug |
|
|
100 | 5 warn warning possible problem, not necessarily error |
|
|
101 | 6 note notice unusual conditions |
|
|
102 | 7 info normal messages, no action required |
|
|
103 | 8 debug debugging messages for development |
|
|
104 | 9 trace copious tracing output |
|
|
105 | |
90 | As you can see, some logging levels have multiple aliases - the first one |
106 | As you can see, some logging levels have multiple aliases - the first one |
91 | is the "official" name, the second one the "syslog" name (if it differs) |
107 | is the "official" name, the second one the "syslog" name (if it differs) |
92 | and the third one the "perl" name, suggesting (only!) that you log C<die> |
108 | and the third one the "perl" name, suggesting (only!) that you log C<die> |
93 | messages at C<error> priority. The NOTE column tries to provide some |
109 | messages at C<error> priority. The NOTE column tries to provide some |
94 | rationale on how to chose a logging level. |
110 | rationale on how to chose a logging level. |
95 | |
111 | |
96 | As a rough guideline, levels 1..3 are primarily meant for users of |
112 | As a rough guideline, levels 1..3 are primarily meant for users of the |
97 | the program (admins, staff), and are the only logged to STDERR by |
113 | program (admins, staff), and are the only ones logged to STDERR by |
98 | default. Levels 4..6 are meant for users and developers alike, while |
114 | default. Levels 4..6 are meant for users and developers alike, while |
99 | levels 7..9 are usually meant for developers. |
115 | levels 7..9 are usually meant for developers. |
100 | |
116 | |
101 | You can normally only log a single message at highest priority level |
117 | You can normally only log a message once at highest priority level (C<1>, |
102 | (C<1>, C<fatal>), because logging a fatal message will also quit the |
118 | C<fatal>), because logging a fatal message will also quit the program - so |
103 | program - so use it sparingly :) |
119 | use it sparingly :) |
|
|
120 | |
|
|
121 | For example, a program that finds an unknown switch on the commandline |
|
|
122 | might well use a fatal logging level to tell users about it - the "system" |
|
|
123 | in this case would be the program, or module. |
104 | |
124 | |
105 | Some methods also offer some extra levels, such as C<0>, C<off>, C<none> |
125 | Some methods also offer some extra levels, such as C<0>, C<off>, C<none> |
106 | or C<all> - these are only valid in the methods they are documented for. |
126 | or C<all> - these are only valid for the methods that documented them. |
107 | |
127 | |
108 | =head1 LOGGING FUNCTIONS |
128 | =head1 LOGGING FUNCTIONS |
109 | |
129 | |
110 | These functions allow you to log messages. They always use the caller's |
130 | The following functions allow you to log messages. They always use the |
111 | package as a "logging context". Also, the main logging function C<log> is |
131 | caller's package as a "logging context". Also, the main logging function, |
112 | callable as C<AnyEvent::log> or C<AE::log> when the C<AnyEvent> module is |
132 | C<log>, is aliased to C<AnyEvent::log> and C<AE::log> when the C<AnyEvent> |
113 | loaded. |
133 | module is loaded. |
114 | |
134 | |
115 | =over 4 |
135 | =over 4 |
116 | |
136 | |
117 | =cut |
137 | =cut |
118 | |
138 | |
119 | package AnyEvent::Log; |
139 | package AnyEvent::Log; |
120 | |
140 | |
121 | use Carp (); |
141 | use Carp (); |
122 | use POSIX (); |
142 | use POSIX (); |
|
|
143 | |
|
|
144 | # layout of a context |
|
|
145 | # 0 1 2 3 4, 5 |
|
|
146 | # [$title, $level, %$slaves, &$logcb, &$fmtcb, $cap] |
123 | |
147 | |
124 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
148 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
125 | #use AnyEvent::Util (); need to load this in a delayed fashion, as it uses AE::log |
149 | #use AnyEvent::Util (); need to load this in a delayed fashion, as it uses AE::log |
126 | |
150 | |
127 | our $VERSION = $AnyEvent::VERSION; |
151 | our $VERSION = $AnyEvent::VERSION; |
… | |
… | |
172 | |
196 | |
173 | Last not least, C<$msg> might be a code reference, in which case it is |
197 | Last not least, C<$msg> might be a code reference, in which case it is |
174 | supposed to return the message. It will be called only then the message |
198 | supposed to return the message. It will be called only then the message |
175 | actually gets logged, which is useful if it is costly to create the |
199 | actually gets logged, which is useful if it is costly to create the |
176 | message in the first place. |
200 | message in the first place. |
|
|
201 | |
|
|
202 | This function takes care of saving and restoring C<$!> and C<$@>, so you |
|
|
203 | don't have to. |
177 | |
204 | |
178 | Whether the given message will be logged depends on the maximum log level |
205 | Whether the given message will be logged depends on the maximum log level |
179 | and the caller's package. The return value can be used to ensure that |
206 | and the caller's package. The return value can be used to ensure that |
180 | messages or not "lost" - for example, when L<AnyEvent::Debug> detects a |
207 | messages or not "lost" - for example, when L<AnyEvent::Debug> detects a |
181 | runtime error it tries to log it at C<die> level, but if that message is |
208 | runtime error it tries to log it at C<die> level, but if that message is |
… | |
… | |
262 | ? $level+0 |
289 | ? $level+0 |
263 | : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; |
290 | : $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; |
264 | |
291 | |
265 | my $mask = 1 << $level; |
292 | my $mask = 1 << $level; |
266 | |
293 | |
267 | my ($success, %seen, @ctx, $now, $fmt); |
294 | my ($success, %seen, @ctx, $now, @fmt); |
268 | |
295 | |
269 | do |
296 | do |
270 | { |
297 | { |
271 | # skip if masked |
298 | # if !ref, then it's a level number |
|
|
299 | if (!ref $ctx) { |
|
|
300 | $level = $ctx; |
272 | if ($ctx->[1] & $mask && !$seen{$ctx+0}++) { |
301 | } elsif ($ctx->[1] & $mask and !$seen{$ctx+0}++) { |
|
|
302 | # logging/recursing into this context |
|
|
303 | |
|
|
304 | # level cap |
|
|
305 | if ($ctx->[5] > $level) { |
|
|
306 | push @ctx, $level; # restore level when going up in tree |
|
|
307 | $level = $ctx->[5]; |
|
|
308 | } |
|
|
309 | |
|
|
310 | # log if log cb |
273 | if ($ctx->[3]) { |
311 | if ($ctx->[3]) { |
274 | # logging target found |
312 | # logging target found |
|
|
313 | |
|
|
314 | local ($!, $@); |
275 | |
315 | |
276 | # now get raw message, unless we have it already |
316 | # now get raw message, unless we have it already |
277 | unless ($now) { |
317 | unless ($now) { |
278 | $format = $format->() if ref $format; |
318 | $format = $format->() if ref $format; |
279 | $format = sprintf $format, @args if @args; |
319 | $format = sprintf $format, @args if @args; |
… | |
… | |
282 | }; |
322 | }; |
283 | |
323 | |
284 | # format msg |
324 | # format msg |
285 | my $str = $ctx->[4] |
325 | my $str = $ctx->[4] |
286 | ? $ctx->[4]($now, $_[0], $level, $format) |
326 | ? $ctx->[4]($now, $_[0], $level, $format) |
287 | : ($fmt ||= _format $now, $_[0], $level, $format); |
327 | : ($fmt[$level] ||= _format $now, $_[0], $level, $format); |
288 | |
328 | |
289 | $success = 1; |
329 | $success = 1; |
290 | |
330 | |
291 | $ctx->[3]($str) |
331 | $ctx->[3]($str) |
292 | or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate |
332 | or push @ctx, values %{ $ctx->[2] }; # not consumed - propagate |
… | |
… | |
596 | package AnyEvent::Log::COLLECT; |
636 | package AnyEvent::Log::COLLECT; |
597 | package AE::Log::COLLECT; |
637 | package AE::Log::COLLECT; |
598 | |
638 | |
599 | package AnyEvent::Log::Ctx; |
639 | package AnyEvent::Log::Ctx; |
600 | |
640 | |
601 | # 0 1 2 3 4 |
|
|
602 | # [$title, $level, %$slaves, &$logcb, &$fmtcb] |
|
|
603 | |
|
|
604 | =item $ctx = new AnyEvent::Log::Ctx methodname => param... |
641 | =item $ctx = new AnyEvent::Log::Ctx methodname => param... |
605 | |
642 | |
606 | This is a convenience constructor that makes it simpler to construct |
643 | This is a convenience constructor that makes it simpler to construct |
607 | anonymous logging contexts. |
644 | anonymous logging contexts. |
608 | |
645 | |
… | |
… | |
695 | |
732 | |
696 | =item $ctx->disable ($level[, $level...]) |
733 | =item $ctx->disable ($level[, $level...]) |
697 | |
734 | |
698 | Disables logging for the given levels, leaving all others unchanged. |
735 | Disables logging for the given levels, leaving all others unchanged. |
699 | |
736 | |
|
|
737 | =item $ctx->cap ($level) |
|
|
738 | |
|
|
739 | Caps the maximum priority to the given level, for all messages logged |
|
|
740 | to, or passing through, this context. That is, while this doesn't affect |
|
|
741 | whether a message is logged or passed on, the maximum priority of messages |
|
|
742 | will be limited to the specified level - messages with a higher priority |
|
|
743 | will be set to the specified priority. |
|
|
744 | |
|
|
745 | Another way to view this is that C<< ->level >> filters out messages with |
|
|
746 | a too low priority, while C<< ->cap >> modifies messages with a too high |
|
|
747 | priority. |
|
|
748 | |
|
|
749 | This is useful when different log targets have different interpretations |
|
|
750 | of priority. For example, for a specific command line program, a wrong |
|
|
751 | command line switch might well result in a C<fatal> log message, while the |
|
|
752 | same message, logged to syslog, is likely I<not> fatal to the system or |
|
|
753 | syslog facility as a whole, but more likely a mere C<error>. |
|
|
754 | |
|
|
755 | This can be modeled by having a stderr logger that logs messages "as-is" |
|
|
756 | and a syslog logger that logs messages with a level cap of, say, C<error>, |
|
|
757 | or, for truly system-critical components, actually C<critical>. |
|
|
758 | |
700 | =cut |
759 | =cut |
701 | |
760 | |
702 | sub _lvl_lst { |
761 | sub _lvl_lst { |
703 | map { |
762 | map { |
704 | $_ > 0 && $_ <= 9 ? $_+0 |
763 | $_ > 0 && $_ <= 9 ? $_+0 |
705 | : $_ eq "all" ? (1 .. 9) |
764 | : $_ eq "all" ? (1 .. 9) |
706 | : $STR2LEVEL{$_} || Carp::croak "$_: not a valid logging level, caught" |
765 | : $STR2LEVEL{$_} || Carp::croak "$_: not a valid logging level, caught" |
707 | } @_ |
766 | } @_ |
708 | } |
767 | } |
709 | |
768 | |
|
|
769 | sub _lvl { |
|
|
770 | $_[0] =~ /^(?:0|off|none)$/ ? 0 : (_lvl_lst $_[0])[-1] |
|
|
771 | } |
|
|
772 | |
710 | our $NOP_CB = sub { 0 }; |
773 | our $NOP_CB = sub { 0 }; |
711 | |
774 | |
712 | sub levels { |
775 | sub levels { |
713 | my $ctx = shift; |
776 | my $ctx = shift; |
714 | $ctx->[1] = 0; |
777 | $ctx->[1] = 0; |
… | |
… | |
717 | AnyEvent::Log::_reassess; |
780 | AnyEvent::Log::_reassess; |
718 | } |
781 | } |
719 | |
782 | |
720 | sub level { |
783 | sub level { |
721 | my $ctx = shift; |
784 | my $ctx = shift; |
722 | my $lvl = $_[0] =~ /^(?:0|off|none)$/ ? 0 : (_lvl_lst $_[0])[-1]; |
|
|
723 | |
|
|
724 | $ctx->[1] = ((1 << $lvl) - 1) << 1; |
785 | $ctx->[1] = ((1 << &_lvl) - 1) << 1; |
725 | AnyEvent::Log::_reassess; |
786 | AnyEvent::Log::_reassess; |
726 | } |
787 | } |
727 | |
788 | |
728 | sub enable { |
789 | sub enable { |
729 | my $ctx = shift; |
790 | my $ctx = shift; |
… | |
… | |
737 | $ctx->[1] &= ~(1 << $_) |
798 | $ctx->[1] &= ~(1 << $_) |
738 | for &_lvl_lst; |
799 | for &_lvl_lst; |
739 | AnyEvent::Log::_reassess; |
800 | AnyEvent::Log::_reassess; |
740 | } |
801 | } |
741 | |
802 | |
|
|
803 | sub cap { |
|
|
804 | my $ctx = shift; |
|
|
805 | $ctx->[5] = &_lvl; |
|
|
806 | } |
|
|
807 | |
742 | =back |
808 | =back |
743 | |
809 | |
744 | =head3 SLAVE CONTEXTS |
810 | =head3 SLAVE CONTEXTS |
745 | |
811 | |
746 | The following methods attach and detach another logging context to a |
812 | The following methods attach and detach another logging context to a |
… | |
… | |
833 | |
899 | |
834 | Replaces the formatting callback on the context (C<undef> restores the |
900 | Replaces the formatting callback on the context (C<undef> restores the |
835 | default formatter). |
901 | default formatter). |
836 | |
902 | |
837 | The callback is passed the (possibly fractional) timestamp, the original |
903 | The callback is passed the (possibly fractional) timestamp, the original |
838 | logging context, the (numeric) logging level and the raw message string |
904 | logging context (object, not title), the (numeric) logging level and |
839 | and needs to return a formatted log message. In most cases this will be a |
905 | the raw message string and needs to return a formatted log message. In |
840 | string, but it could just as well be an array reference that just stores |
906 | most cases this will be a string, but it could just as well be an array |
841 | the values. |
907 | reference that just stores the values. |
842 | |
908 | |
843 | If, for some reason, you want to use C<caller> to find out more baout the |
909 | If, for some reason, you want to use C<caller> to find out more about the |
844 | logger then you should walk up the call stack until you are no longer |
910 | logger then you should walk up the call stack until you are no longer |
845 | inside the C<AnyEvent::Log> package. |
911 | inside the C<AnyEvent::Log> package. |
846 | |
912 | |
847 | Example: format just the raw message, with numeric log level in angle |
913 | Example: format just the raw message, with numeric log level in angle |
848 | brackets. |
914 | brackets. |
… | |
… | |
852 | |
918 | |
853 | "<$lvl>$msg\n" |
919 | "<$lvl>$msg\n" |
854 | }); |
920 | }); |
855 | |
921 | |
856 | Example: return an array reference with just the log values, and use |
922 | Example: return an array reference with just the log values, and use |
857 | C<PApp::SQL::sql_exec> to store the emssage in a database. |
923 | C<PApp::SQL::sql_exec> to store the message in a database. |
858 | |
924 | |
859 | $ctx->fmt_cb (sub { \@_ }); |
925 | $ctx->fmt_cb (sub { \@_ }); |
860 | $ctx->log_cb (sub { |
926 | $ctx->log_cb (sub { |
861 | my ($msg) = @_; |
927 | my ($msg) = @_; |
862 | |
928 | |
… | |
… | |
980 | |
1046 | |
981 | =item $ctx->log ($level, $msg[, @params]) |
1047 | =item $ctx->log ($level, $msg[, @params]) |
982 | |
1048 | |
983 | Same as C<AnyEvent::Log::log>, but uses the given context as log context. |
1049 | Same as C<AnyEvent::Log::log>, but uses the given context as log context. |
984 | |
1050 | |
|
|
1051 | Example: log a message in the context of another package. |
|
|
1052 | |
|
|
1053 | (AnyEvent::Log::ctx "Other::Package")->log (warn => "heely bo"); |
|
|
1054 | |
985 | =item $logger = $ctx->logger ($level[, \$enabled]) |
1055 | =item $logger = $ctx->logger ($level[, \$enabled]) |
986 | |
1056 | |
987 | Same as C<AnyEvent::Log::logger>, but uses the given context as log |
1057 | Same as C<AnyEvent::Log::logger>, but uses the given context as log |
988 | context. |
1058 | context. |
989 | |
1059 | |
… | |
… | |
1076 | =item C<nolog> |
1146 | =item C<nolog> |
1077 | |
1147 | |
1078 | Configures the context to not log anything by itself, which is the |
1148 | Configures the context to not log anything by itself, which is the |
1079 | default. Same as C<< $ctx->log_cb (undef) >>. |
1149 | default. Same as C<< $ctx->log_cb (undef) >>. |
1080 | |
1150 | |
|
|
1151 | =item C<cap=>I<level> |
|
|
1152 | |
|
|
1153 | Caps logging messages entering this context at the given level, i.e. |
|
|
1154 | reduces the priority of messages with higher priority than this level. The |
|
|
1155 | default is C<0> (or C<off>), meaning the priority will not be touched. |
|
|
1156 | |
1081 | =item C<0> or C<off> |
1157 | =item C<0> or C<off> |
1082 | |
1158 | |
1083 | Sets the logging level of the context ot C<0>, i.e. all messages will be |
1159 | Sets the logging level of the context to C<0>, i.e. all messages will be |
1084 | filtered out. |
1160 | filtered out. |
1085 | |
1161 | |
1086 | =item C<all> |
1162 | =item C<all> |
1087 | |
1163 | |
1088 | Enables all logging levels, i.e. filtering will effectively be switched |
1164 | Enables all logging levels, i.e. filtering will effectively be switched |
… | |
… | |
1130 | |
1206 | |
1131 | Attaches the named context as slave to the context. |
1207 | Attaches the named context as slave to the context. |
1132 | |
1208 | |
1133 | =item C<+> |
1209 | =item C<+> |
1134 | |
1210 | |
1135 | A line C<+> detaches all contexts, i.e. clears the slave list from the |
1211 | A lone C<+> detaches all contexts, i.e. clears the slave list from the |
1136 | context. Anonymous (C<%name>) contexts have no attached slaves by default, |
1212 | context. Anonymous (C<%name>) contexts have no attached slaves by default, |
1137 | but package contexts have the parent context as slave by default. |
1213 | but package contexts have the parent context as slave by default. |
1138 | |
1214 | |
1139 | Example: log messages from My::Module to a file, do not send them to the |
1215 | Example: log messages from My::Module to a file, do not send them to the |
1140 | default log collector. |
1216 | default log collector. |
… | |
… | |
1171 | |
1247 | |
1172 | my $pkg = sub { |
1248 | my $pkg = sub { |
1173 | $_[0] eq "log" ? $LOG |
1249 | $_[0] eq "log" ? $LOG |
1174 | : $_[0] eq "filter" ? $FILTER |
1250 | : $_[0] eq "filter" ? $FILTER |
1175 | : $_[0] eq "collect" ? $COLLECT |
1251 | : $_[0] eq "collect" ? $COLLECT |
1176 | : $_[0] =~ /^%(.+)$/ ? ($anon{$1} ||= ctx undef) |
1252 | : $_[0] =~ /^%(.+)$/ ? ($anon{$1} ||= do { my $ctx = ctx undef; $ctx->[0] = $_[0]; $ctx }) |
1177 | : $_[0] =~ /^(.*?)(?:::)?$/ ? ctx "$1" # egad :/ |
1253 | : $_[0] =~ /^(.*?)(?:::)?$/ ? ctx "$1" # egad :/ |
1178 | : die # never reached? |
1254 | : die # never reached? |
1179 | }; |
1255 | }; |
1180 | |
1256 | |
1181 | /\G[[:space:]]+/gc; # skip initial whitespace |
1257 | /\G[[:space:]]+/gc; # skip initial whitespace |
… | |
… | |
1187 | while (/\G((?:[^,:[:space:]]+|::|\\.)+)/gc) { |
1263 | while (/\G((?:[^,:[:space:]]+|::|\\.)+)/gc) { |
1188 | for ("$1") { |
1264 | for ("$1") { |
1189 | if ($_ eq "stderr" ) { $ctx->log_to_warn; |
1265 | if ($_ eq "stderr" ) { $ctx->log_to_warn; |
1190 | } elsif (/^file=(.+)/ ) { $ctx->log_to_file ("$1"); |
1266 | } elsif (/^file=(.+)/ ) { $ctx->log_to_file ("$1"); |
1191 | } elsif (/^path=(.+)/ ) { $ctx->log_to_path ("$1"); |
1267 | } elsif (/^path=(.+)/ ) { $ctx->log_to_path ("$1"); |
1192 | } elsif (/syslog(?:=(.*))?/ ) { require Sys::Syslog; $ctx->log_to_syslog ($1); |
1268 | } elsif (/^syslog(?:=(.*))?/ ) { require Sys::Syslog; $ctx->log_to_syslog ("$1"); |
1193 | } elsif ($_ eq "nolog" ) { $ctx->log_cb (undef); |
1269 | } elsif ($_ eq "nolog" ) { $ctx->log_cb (undef); |
|
|
1270 | } elsif (/^cap=(.+)/ ) { $ctx->cap ("$1"); |
1194 | } elsif (/^\+(.+)$/ ) { $ctx->attach ($pkg->("$1")); |
1271 | } elsif (/^\+(.+)$/ ) { $ctx->attach ($pkg->("$1")); |
1195 | } elsif ($_ eq "+" ) { $ctx->slaves; |
1272 | } elsif ($_ eq "+" ) { $ctx->slaves; |
1196 | } elsif ($_ eq "off" or $_ eq "0") { $ctx->level (0); |
1273 | } elsif ($_ eq "off" or $_ eq "0") { $ctx->level (0); |
1197 | } elsif ($_ eq "all" ) { $ctx->level ("all"); |
1274 | } elsif ($_ eq "all" ) { $ctx->level ("all"); |
1198 | } elsif ($_ eq "level" ) { $ctx->level ("all"); $level = "level"; |
1275 | } elsif ($_ eq "level" ) { $ctx->level ("all"); $level = "level"; |
… | |
… | |
1266 | |
1343 | |
1267 | PERL_ANYEVENT_LOG=%filelogger=file=/some/path:collect=+%filelogger |
1344 | PERL_ANYEVENT_LOG=%filelogger=file=/some/path:collect=+%filelogger |
1268 | |
1345 | |
1269 | In both cases, messages are still written to STDERR. |
1346 | In both cases, messages are still written to STDERR. |
1270 | |
1347 | |
|
|
1348 | =item Additionally log all messages with C<warn> and higher priority to |
|
|
1349 | C<syslog>, but cap at C<error>. |
|
|
1350 | |
|
|
1351 | This logs all messages to the default log target, but also logs messages |
|
|
1352 | with priority C<warn> or higher (and not filtered otherwise) to syslog |
|
|
1353 | facility C<user>. Messages with priority higher than C<error> will be |
|
|
1354 | logged with level C<error>. |
|
|
1355 | |
|
|
1356 | $AnyEvent::Log::LOG->attach ( |
|
|
1357 | new AnyEvent::Log::Ctx |
|
|
1358 | level => "warn", |
|
|
1359 | cap => "error", |
|
|
1360 | syslog => "user", |
|
|
1361 | ); |
|
|
1362 | |
|
|
1363 | PERL_ANYEVENT_LOG=log=+%syslog:%syslog=warn,cap=error,syslog |
|
|
1364 | |
1271 | =item Write trace messages (only) from L<AnyEvent::Debug> to the default logging target(s). |
1365 | =item Write trace messages (only) from L<AnyEvent::Debug> to the default logging target(s). |
1272 | |
1366 | |
1273 | Attach the C<$AnyEvent::Log::LOG> context to the C<AnyEvent::Debug> |
1367 | Attach the C<$AnyEvent::Log::LOG> context to the C<AnyEvent::Debug> |
1274 | context - this simply circumvents the global filtering for trace messages. |
1368 | context - this simply circumvents the global filtering for trace messages. |
1275 | |
1369 | |