1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
|
|
AnyEvent::Log - simple logging "framework" |
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS |
6 |
|
|
|
7 |
root |
1.8 |
# simple use |
8 |
|
|
use AnyEvent; |
9 |
|
|
|
10 |
|
|
AE::log debug => "hit my knee"; |
11 |
|
|
AE::log warn => "it's a bit too hot"; |
12 |
|
|
AE::log error => "the flag was false!"; |
13 |
|
|
AE::log fatal => "the bit toggled! run!"; |
14 |
|
|
|
15 |
root |
1.10 |
# "complex" use |
16 |
root |
1.1 |
use AnyEvent::Log; |
17 |
|
|
|
18 |
root |
1.8 |
my $tracer = AnyEvent::Log::logger trace => \$my $trace; |
19 |
|
|
|
20 |
|
|
$tracer->("i am here") if $trace; |
21 |
|
|
$tracer->(sub { "lots of data: " . Dumper $self }) if $trace; |
22 |
|
|
|
23 |
root |
1.10 |
# configuration |
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); |
40 |
root |
1.8 |
|
41 |
root |
1.1 |
=head1 DESCRIPTION |
42 |
|
|
|
43 |
root |
1.2 |
This module implements a relatively simple "logging framework". It doesn't |
44 |
|
|
attempt to be "the" logging solution or even "a" logging solution for |
45 |
|
|
AnyEvent - AnyEvent simply creates logging messages internally, and this |
46 |
|
|
module more or less exposes the mechanism, with some extra spiff to allow |
47 |
|
|
using it from other modules as well. |
48 |
|
|
|
49 |
root |
1.5 |
Remember that the default verbosity level is C<0>, so nothing will be |
50 |
root |
1.11 |
logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number before |
51 |
|
|
starting your program, or change the logging level at runtime with |
52 |
root |
1.9 |
something like: |
53 |
root |
1.2 |
|
54 |
root |
1.9 |
use AnyEvent; |
55 |
|
|
(AnyEvent::Log::ctx "")->level ("info"); |
56 |
root |
1.2 |
|
57 |
root |
1.10 |
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 |
|
|
|
62 |
root |
1.9 |
=head1 LOGGING FUNCTIONS |
63 |
root |
1.2 |
|
64 |
|
|
These functions allow you to log messages. They always use the caller's |
65 |
root |
1.7 |
package as a "logging module/source". Also, the main logging function is |
66 |
|
|
callable as C<AnyEvent::log> or C<AE::log> when the C<AnyEvent> module is |
67 |
|
|
loaded. |
68 |
root |
1.1 |
|
69 |
|
|
=over 4 |
70 |
|
|
|
71 |
|
|
=cut |
72 |
|
|
|
73 |
|
|
package AnyEvent::Log; |
74 |
|
|
|
75 |
root |
1.2 |
use Carp (); |
76 |
root |
1.1 |
use POSIX (); |
77 |
|
|
|
78 |
|
|
use AnyEvent (); BEGIN { AnyEvent::common_sense } |
79 |
root |
1.3 |
use AnyEvent::Util (); |
80 |
root |
1.1 |
|
81 |
root |
1.2 |
our ($now_int, $now_str1, $now_str2); |
82 |
|
|
|
83 |
|
|
# Format Time, not public - yet? |
84 |
|
|
sub ft($) { |
85 |
|
|
my $i = int $_[0]; |
86 |
|
|
my $f = sprintf "%06d", 1e6 * ($_[0] - $i); |
87 |
|
|
|
88 |
|
|
($now_int, $now_str1, $now_str2) = ($i, split /\x01/, POSIX::strftime "%Y-%m-%d %H:%M:%S.\x01 %z", localtime $i) |
89 |
|
|
if $now_int != $i; |
90 |
|
|
|
91 |
|
|
"$now_str1$f$now_str2" |
92 |
|
|
} |
93 |
|
|
|
94 |
root |
1.5 |
our %CTX; # all logging contexts |
95 |
root |
1.3 |
|
96 |
root |
1.8 |
# creates a default package context object for the given package |
97 |
|
|
sub _pkg_ctx($) { |
98 |
root |
1.10 |
my $ctx = bless [$_[0], (1 << 10) - 1 - 1, {}], "AnyEvent::Log::Ctx"; |
99 |
root |
1.8 |
|
100 |
|
|
# link "parent" package |
101 |
root |
1.11 |
my $pkg = $_[0] =~ /^(.+)::/ ? $1 : "AE::Log::Top"; |
102 |
root |
1.8 |
|
103 |
|
|
$pkg = $CTX{$pkg} ||= &_pkg_ctx ($pkg); |
104 |
|
|
$ctx->[2]{$pkg+0} = $pkg; |
105 |
|
|
|
106 |
|
|
$ctx |
107 |
|
|
} |
108 |
|
|
|
109 |
root |
1.2 |
=item AnyEvent::Log::log $level, $msg[, @args] |
110 |
|
|
|
111 |
|
|
Requests logging of the given C<$msg> with the given log level (1..9). |
112 |
|
|
You can also use the following strings as log level: C<fatal> (1), |
113 |
|
|
C<alert> (2), C<critical> (3), C<error> (4), C<warn> (5), C<note> (6), |
114 |
|
|
C<info> (7), C<debug> (8), C<trace> (9). |
115 |
|
|
|
116 |
|
|
For C<fatal> log levels, the program will abort. |
117 |
|
|
|
118 |
|
|
If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the |
119 |
|
|
C<$msg> is interpreted as an sprintf format string. |
120 |
|
|
|
121 |
|
|
The C<$msg> should not end with C<\n>, but may if that is convenient for |
122 |
|
|
you. Also, multiline messages are handled properly. |
123 |
|
|
|
124 |
root |
1.3 |
Last not least, C<$msg> might be a code reference, in which case it is |
125 |
|
|
supposed to return the message. It will be called only then the message |
126 |
|
|
actually gets logged, which is useful if it is costly to create the |
127 |
|
|
message in the first place. |
128 |
root |
1.2 |
|
129 |
|
|
Whether the given message will be logged depends on the maximum log level |
130 |
|
|
and the caller's package. |
131 |
|
|
|
132 |
|
|
Note that you can (and should) call this function as C<AnyEvent::log> or |
133 |
root |
1.8 |
C<AE::log>, without C<use>-ing this module if possible (i.e. you don't |
134 |
|
|
need any additional functionality), as those functions will load the |
135 |
|
|
logging module on demand only. They are also much shorter to write. |
136 |
|
|
|
137 |
root |
1.11 |
Also, if you optionally generate a lot of debug messages (such as when |
138 |
root |
1.8 |
tracing some code), you should look into using a logger callback and a |
139 |
|
|
boolean enabler (see C<logger>, below). |
140 |
root |
1.2 |
|
141 |
root |
1.3 |
Example: log something at error level. |
142 |
|
|
|
143 |
|
|
AE::log error => "something"; |
144 |
|
|
|
145 |
|
|
Example: use printf-formatting. |
146 |
|
|
|
147 |
|
|
AE::log info => "%5d %-10.10s %s", $index, $category, $msg; |
148 |
|
|
|
149 |
|
|
Example: only generate a costly dump when the message is actually being logged. |
150 |
|
|
|
151 |
|
|
AE::log debug => sub { require Data::Dump; Data::Dump::dump \%cache }; |
152 |
|
|
|
153 |
root |
1.2 |
=cut |
154 |
|
|
|
155 |
|
|
# also allow syslog equivalent names |
156 |
|
|
our %STR2LEVEL = ( |
157 |
|
|
fatal => 1, emerg => 1, |
158 |
|
|
alert => 2, |
159 |
|
|
critical => 3, crit => 3, |
160 |
|
|
error => 4, err => 4, |
161 |
|
|
warn => 5, warning => 5, |
162 |
|
|
note => 6, notice => 6, |
163 |
|
|
info => 7, |
164 |
|
|
debug => 8, |
165 |
|
|
trace => 9, |
166 |
|
|
); |
167 |
|
|
|
168 |
root |
1.4 |
sub now () { time } |
169 |
root |
1.10 |
|
170 |
root |
1.4 |
AnyEvent::post_detect { |
171 |
|
|
*now = \&AE::now; |
172 |
|
|
}; |
173 |
|
|
|
174 |
root |
1.2 |
our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
175 |
|
|
|
176 |
root |
1.8 |
# time, ctx, level, msg |
177 |
|
|
sub _format($$$$) { |
178 |
root |
1.11 |
my $ts = ft $_[0]; |
179 |
|
|
my $ct = " "; |
180 |
|
|
|
181 |
root |
1.10 |
my @res; |
182 |
root |
1.8 |
|
183 |
root |
1.10 |
for (split /\n/, sprintf "%-5s %s: %s", $LEVEL2STR[$_[2]], $_[1][0], $_[3]) { |
184 |
root |
1.11 |
push @res, "$ts$ct$_\n"; |
185 |
|
|
$ct = " + "; |
186 |
root |
1.10 |
} |
187 |
|
|
|
188 |
|
|
join "", @res |
189 |
root |
1.8 |
} |
190 |
|
|
|
191 |
root |
1.3 |
sub _log { |
192 |
root |
1.8 |
my ($ctx, $level, $format, @args) = @_; |
193 |
root |
1.2 |
|
194 |
root |
1.11 |
$level = $level > 0 && $level <= 9 |
195 |
|
|
? $level+0 |
196 |
|
|
: $STR2LEVEL{$level} || Carp::croak "$level: not a valid logging level, caught"; |
197 |
root |
1.2 |
|
198 |
root |
1.8 |
my $mask = 1 << $level; |
199 |
root |
1.2 |
|
200 |
root |
1.11 |
my (%seen, @ctx, $now, $fmt); |
201 |
root |
1.8 |
|
202 |
root |
1.11 |
do |
203 |
|
|
{ |
204 |
|
|
# skip if masked |
205 |
|
|
if ($ctx->[1] & $mask && !$seen{$ctx+0}++) { |
206 |
|
|
if ($ctx->[3]) { |
207 |
|
|
# logging target found |
208 |
|
|
|
209 |
|
|
# now get raw message, unless we have it already |
210 |
|
|
unless ($now) { |
211 |
|
|
$format = $format->() if ref $format; |
212 |
|
|
$format = sprintf $format, @args if @args; |
213 |
|
|
$format =~ s/\n$//; |
214 |
|
|
$now = AE::now; |
215 |
|
|
}; |
216 |
|
|
|
217 |
|
|
# format msg |
218 |
|
|
my $str = $ctx->[4] |
219 |
|
|
? $ctx->[4]($now, $_[0], $level, $format) |
220 |
|
|
: $fmt ||= _format $now, $_[0], $level, $format; |
221 |
|
|
|
222 |
|
|
$ctx->[3]($str); |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
# not masked, not consumed - propagate to parent contexts |
226 |
|
|
push @ctx, values %{ $ctx->[2] }; |
227 |
|
|
} |
228 |
root |
1.8 |
} |
229 |
root |
1.11 |
while $ctx = pop @ctx; |
230 |
root |
1.2 |
|
231 |
|
|
exit 1 if $level <= 1; |
232 |
|
|
} |
233 |
|
|
|
234 |
root |
1.3 |
sub log($$;@) { |
235 |
root |
1.8 |
_log |
236 |
|
|
$CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
237 |
|
|
@_; |
238 |
root |
1.3 |
} |
239 |
|
|
|
240 |
root |
1.2 |
*AnyEvent::log = *AE::log = \&log; |
241 |
|
|
|
242 |
root |
1.3 |
=item $logger = AnyEvent::Log::logger $level[, \$enabled] |
243 |
|
|
|
244 |
|
|
Creates a code reference that, when called, acts as if the |
245 |
|
|
C<AnyEvent::Log::log> function was called at this point with the givne |
246 |
|
|
level. C<$logger> is passed a C<$msg> and optional C<@args>, just as with |
247 |
|
|
the C<AnyEvent::Log::log> function: |
248 |
|
|
|
249 |
|
|
my $debug_log = AnyEvent::Log::logger "debug"; |
250 |
|
|
|
251 |
|
|
$debug_log->("debug here"); |
252 |
|
|
$debug_log->("%06d emails processed", 12345); |
253 |
|
|
$debug_log->(sub { $obj->as_string }); |
254 |
|
|
|
255 |
|
|
The idea behind this function is to decide whether to log before actually |
256 |
|
|
logging - when the C<logger> function is called once, but the returned |
257 |
|
|
logger callback often, then this can be a tremendous speed win. |
258 |
|
|
|
259 |
|
|
Despite this speed advantage, changes in logging configuration will |
260 |
|
|
still be reflected by the logger callback, even if configuration changes |
261 |
|
|
I<after> it was created. |
262 |
|
|
|
263 |
|
|
To further speed up logging, you can bind a scalar variable to the logger, |
264 |
|
|
which contains true if the logger should be called or not - if it is |
265 |
|
|
false, calling the logger can be safely skipped. This variable will be |
266 |
|
|
updated as long as C<$logger> is alive. |
267 |
|
|
|
268 |
|
|
Full example: |
269 |
|
|
|
270 |
|
|
# near the init section |
271 |
|
|
use AnyEvent::Log; |
272 |
|
|
|
273 |
|
|
my $debug_log = AnyEvent:Log::logger debug => \my $debug; |
274 |
|
|
|
275 |
|
|
# and later in your program |
276 |
|
|
$debug_log->("yo, stuff here") if $debug; |
277 |
|
|
|
278 |
|
|
$debug and $debug_log->("123"); |
279 |
|
|
|
280 |
|
|
Note: currently the enabled var is always true - that will be fixed in a |
281 |
|
|
future version :) |
282 |
|
|
|
283 |
|
|
=cut |
284 |
|
|
|
285 |
|
|
our %LOGGER; |
286 |
|
|
|
287 |
|
|
# re-assess logging status for all loggers |
288 |
|
|
sub _reassess { |
289 |
|
|
for (@_ ? $LOGGER{$_[0]} : values %LOGGER) { |
290 |
root |
1.8 |
my ($ctx, $level, $renabled) = @$_; |
291 |
root |
1.3 |
|
292 |
root |
1.11 |
# to detect whether a message would be logged, we # actually |
293 |
|
|
# try to log one and die. this isn't fast, but we can be |
294 |
root |
1.3 |
# sure that the logging decision is correct :) |
295 |
|
|
|
296 |
|
|
$$renabled = !eval { |
297 |
|
|
local $SIG{__DIE__}; |
298 |
|
|
|
299 |
root |
1.8 |
_log $ctx, $level, sub { die }; |
300 |
root |
1.3 |
|
301 |
|
|
1 |
302 |
|
|
}; |
303 |
|
|
|
304 |
|
|
$$renabled = 1; # TODO |
305 |
|
|
} |
306 |
|
|
} |
307 |
|
|
|
308 |
root |
1.8 |
sub _logger($;$) { |
309 |
|
|
my ($ctx, $level, $renabled) = @_; |
310 |
root |
1.3 |
|
311 |
|
|
$renabled ||= \my $enabled; |
312 |
|
|
|
313 |
|
|
$$renabled = 1; |
314 |
|
|
|
315 |
root |
1.8 |
my $logger = [$ctx, $level, $renabled]; |
316 |
root |
1.3 |
|
317 |
|
|
$LOGGER{$logger+0} = $logger; |
318 |
|
|
|
319 |
|
|
_reassess $logger+0; |
320 |
|
|
|
321 |
|
|
my $guard = AnyEvent::Util::guard { |
322 |
|
|
# "clean up" |
323 |
|
|
delete $LOGGER{$logger+0}; |
324 |
|
|
}; |
325 |
|
|
|
326 |
|
|
sub { |
327 |
|
|
$guard if 0; # keep guard alive, but don't cause runtime overhead |
328 |
|
|
|
329 |
root |
1.8 |
_log $ctx, $level, @_ |
330 |
root |
1.3 |
if $$renabled; |
331 |
|
|
} |
332 |
|
|
} |
333 |
|
|
|
334 |
root |
1.8 |
sub logger($;$) { |
335 |
|
|
_logger |
336 |
|
|
$CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
337 |
|
|
@_ |
338 |
|
|
} |
339 |
|
|
|
340 |
root |
1.2 |
=back |
341 |
|
|
|
342 |
root |
1.9 |
=head1 LOGGING CONTEXTS |
343 |
root |
1.2 |
|
344 |
root |
1.9 |
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. |
347 |
root |
1.8 |
|
348 |
root |
1.10 |
A logging context has three major responsibilities: filtering, logging and |
349 |
|
|
propagating the message. |
350 |
root |
1.9 |
|
351 |
root |
1.10 |
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 |
root |
1.9 |
|
361 |
|
|
For propagation, a context can have any number of attached I<parent |
362 |
root |
1.10 |
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 |
root |
1.9 |
|
366 |
root |
1.11 |
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 |
root |
1.9 |
=head2 DEFAULTS |
371 |
|
|
|
372 |
root |
1.10 |
By default, all logging contexts have an full set of log levels ("all"), a |
373 |
root |
1.9 |
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 |
root |
1.11 |
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 |
root |
1.12 |
Perl is C<main>, the default parent of any top-level package context is |
383 |
root |
1.11 |
C<AnyEvent::Log::Top>. |
384 |
root |
1.9 |
|
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 |
root |
1.11 |
When the module is loaded it creates the default context called |
392 |
root |
1.12 |
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 |
root |
1.11 |
it at default config. It's purpose is simply to collect all log messages |
408 |
|
|
system-wide. |
409 |
|
|
|
410 |
root |
1.12 |
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 |
root |
1.11 |
|
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 |
root |
1.9 |
|
428 |
root |
1.11 |
=head2 CREATING/FINDING/DESTROYING CONTEXTS |
429 |
root |
1.2 |
|
430 |
|
|
=over 4 |
431 |
|
|
|
432 |
root |
1.8 |
=item $ctx = AnyEvent::Log::ctx [$pkg] |
433 |
|
|
|
434 |
root |
1.9 |
This function creates or returns a logging context (which is an object). |
435 |
root |
1.8 |
|
436 |
root |
1.9 |
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 |
438 |
|
|
callers package is returned (i.e. the same context as a C<AE::log> call |
439 |
|
|
would use). |
440 |
root |
1.8 |
|
441 |
|
|
If C<undef> is given, then it creates a new anonymous context that is not |
442 |
|
|
tied to any package and is destroyed when no longer referenced. |
443 |
|
|
|
444 |
|
|
=cut |
445 |
|
|
|
446 |
|
|
sub ctx(;$) { |
447 |
|
|
my $pkg = @_ ? shift : (caller)[0]; |
448 |
|
|
|
449 |
|
|
ref $pkg |
450 |
|
|
? $pkg |
451 |
|
|
: defined $pkg |
452 |
|
|
? $CTX{$pkg} ||= AnyEvent::Log::_pkg_ctx $pkg |
453 |
root |
1.10 |
: bless [undef, (1 << 10) - 1 - 1], "AnyEvent::Log::Ctx" |
454 |
root |
1.8 |
} |
455 |
|
|
|
456 |
root |
1.11 |
=item AnyEvent::Log::reset |
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 |
|
|
|
470 |
|
|
my $default = ctx undef; |
471 |
|
|
$default->title ("AnyEvent::Log::Default"); |
472 |
|
|
$default->log_cb (sub { |
473 |
root |
1.8 |
print STDERR shift; |
474 |
|
|
0 |
475 |
|
|
}); |
476 |
root |
1.12 |
$AnyEvent::Log::Default = $CTX{"AnyEvent::Log::Default"} = $CTX{"AE::Log::Default"} = $default; |
477 |
root |
1.11 |
|
478 |
|
|
my $root = ctx undef; |
479 |
|
|
$root->title ("AnyEvent::Log::Root"); |
480 |
|
|
$root->level ($AnyEvent::VERBOSE); |
481 |
|
|
$root->attach ($default); |
482 |
root |
1.12 |
$AnyEvent::Log::Root = $CTX{"AnyEvent::Log::Root"} = $CTX{"AE::Log::Root"} = $root; |
483 |
root |
1.11 |
|
484 |
|
|
my $top = ctx undef; |
485 |
|
|
$top->title ("AnyEvent::Log::Top"); |
486 |
|
|
$top->attach ($root); |
487 |
root |
1.12 |
$AnyEvent::Log::Top = $CTX{"AnyEvent::Log::Top"} = $CTX{"AE::Log::Top"} = $top; |
488 |
root |
1.11 |
} |
489 |
|
|
|
490 |
|
|
AnyEvent::Log::reset; |
491 |
|
|
|
492 |
root |
1.12 |
# hello, CPAN, please catch me |
493 |
root |
1.11 |
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; |
499 |
root |
1.8 |
|
500 |
root |
1.12 |
package AnyEvent::Log::Ctx; |
501 |
|
|
|
502 |
|
|
# 0 1 2 3 4 |
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 |
root |
1.9 |
=back |
526 |
|
|
|
527 |
|
|
=cut |
528 |
|
|
|
529 |
root |
1.12 |
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 |
root |
1.8 |
|
542 |
|
|
|
543 |
root |
1.9 |
=head2 CONFIGURING A LOG CONTEXT |
544 |
|
|
|
545 |
|
|
The following methods can be used to configure the logging context. |
546 |
|
|
|
547 |
|
|
=over 4 |
548 |
|
|
|
549 |
root |
1.8 |
=item $ctx->title ([$new_title]) |
550 |
|
|
|
551 |
|
|
Returns the title of the logging context - this is the package name, for |
552 |
|
|
package contexts, and a user defined string for all others. |
553 |
|
|
|
554 |
|
|
If C<$new_title> is given, then it replaces the package name or title. |
555 |
|
|
|
556 |
|
|
=cut |
557 |
|
|
|
558 |
|
|
sub title { |
559 |
|
|
$_[0][0] = $_[1] if @_ > 1; |
560 |
|
|
$_[0][0] |
561 |
|
|
} |
562 |
|
|
|
563 |
root |
1.9 |
=back |
564 |
|
|
|
565 |
|
|
=head3 LOGGING LEVELS |
566 |
|
|
|
567 |
root |
1.10 |
The following methods deal with the logging level set associated with the |
568 |
|
|
log context. |
569 |
root |
1.9 |
|
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 |
root |
1.10 |
All functions which accept a list of levels also accept the special string |
574 |
|
|
C<all> which expands to all logging levels. |
575 |
|
|
|
576 |
root |
1.9 |
=over 4 |
577 |
|
|
|
578 |
root |
1.8 |
=item $ctx->levels ($level[, $level...) |
579 |
|
|
|
580 |
root |
1.10 |
Enables logging for the given levels and disables it for all others. |
581 |
root |
1.8 |
|
582 |
|
|
=item $ctx->level ($level) |
583 |
|
|
|
584 |
|
|
Enables logging for the given level and all lower level (higher priority) |
585 |
root |
1.10 |
ones. In addition to normal logging levels, specifying a level of C<0> or |
586 |
|
|
C<off> disables all logging for this level. |
587 |
root |
1.8 |
|
588 |
|
|
Example: log warnings, errors and higher priority messages. |
589 |
|
|
|
590 |
|
|
$ctx->level ("warn"); |
591 |
|
|
$ctx->level (5); # same thing, just numeric |
592 |
|
|
|
593 |
|
|
=item $ctx->enable ($level[, $level...]) |
594 |
|
|
|
595 |
|
|
Enables logging for the given levels, leaving all others unchanged. |
596 |
root |
1.5 |
|
597 |
root |
1.8 |
=item $ctx->disable ($level[, $level...]) |
598 |
|
|
|
599 |
|
|
Disables logging for the given levels, leaving all others unchanged. |
600 |
|
|
|
601 |
|
|
=cut |
602 |
|
|
|
603 |
|
|
sub _lvl_lst { |
604 |
root |
1.10 |
map { |
605 |
|
|
$_ > 0 && $_ <= 9 ? $_+0 |
606 |
|
|
: $_ eq "all" ? (1 .. 9) |
607 |
|
|
: $STR2LEVEL{$_} || Carp::croak "$_: not a valid logging level, caught" |
608 |
|
|
} @_ |
609 |
root |
1.8 |
} |
610 |
|
|
|
611 |
|
|
our $NOP_CB = sub { 0 }; |
612 |
|
|
|
613 |
|
|
sub levels { |
614 |
|
|
my $ctx = shift; |
615 |
|
|
$ctx->[1] = 0; |
616 |
|
|
$ctx->[1] |= 1 << $_ |
617 |
|
|
for &_lvl_lst; |
618 |
|
|
AnyEvent::Log::_reassess; |
619 |
|
|
} |
620 |
|
|
|
621 |
|
|
sub level { |
622 |
|
|
my $ctx = shift; |
623 |
root |
1.10 |
my $lvl = $_[0] =~ /^(?:0|off|none)$/ ? 0 : (_lvl_lst $_[0])[-1]; |
624 |
|
|
|
625 |
root |
1.8 |
$ctx->[1] = ((1 << $lvl) - 1) << 1; |
626 |
|
|
AnyEvent::Log::_reassess; |
627 |
|
|
} |
628 |
|
|
|
629 |
|
|
sub enable { |
630 |
|
|
my $ctx = shift; |
631 |
|
|
$ctx->[1] |= 1 << $_ |
632 |
|
|
for &_lvl_lst; |
633 |
|
|
AnyEvent::Log::_reassess; |
634 |
|
|
} |
635 |
|
|
|
636 |
|
|
sub disable { |
637 |
|
|
my $ctx = shift; |
638 |
|
|
$ctx->[1] &= ~(1 << $_) |
639 |
|
|
for &_lvl_lst; |
640 |
|
|
AnyEvent::Log::_reassess; |
641 |
|
|
} |
642 |
|
|
|
643 |
root |
1.9 |
=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 |
|
|
|
655 |
root |
1.8 |
=item $ctx->attach ($ctx2[, $ctx3...]) |
656 |
|
|
|
657 |
|
|
Attaches the given contexts as parents to this context. It is not an error |
658 |
|
|
to add a context twice (the second add will be ignored). |
659 |
|
|
|
660 |
|
|
A context can be specified either as package name or as a context object. |
661 |
|
|
|
662 |
|
|
=item $ctx->detach ($ctx2[, $ctx3...]) |
663 |
|
|
|
664 |
|
|
Removes the given parents from this context - it's not an error to attempt |
665 |
|
|
to remove a context that hasn't been added. |
666 |
|
|
|
667 |
|
|
A context can be specified either as package name or as a context object. |
668 |
root |
1.5 |
|
669 |
root |
1.11 |
=item $ctx->parents ($ctx2[, $ctx3...]) |
670 |
|
|
|
671 |
|
|
Replaces all parents attached to this context by the ones given. |
672 |
|
|
|
673 |
root |
1.2 |
=cut |
674 |
|
|
|
675 |
root |
1.8 |
sub attach { |
676 |
|
|
my $ctx = shift; |
677 |
|
|
|
678 |
|
|
$ctx->[2]{$_+0} = $_ |
679 |
|
|
for map { AnyEvent::Log::ctx $_ } @_; |
680 |
|
|
} |
681 |
|
|
|
682 |
|
|
sub detach { |
683 |
|
|
my $ctx = shift; |
684 |
|
|
|
685 |
|
|
delete $ctx->[2]{$_+0} |
686 |
|
|
for map { AnyEvent::Log::ctx $_ } @_; |
687 |
|
|
} |
688 |
|
|
|
689 |
root |
1.11 |
sub parents { |
690 |
|
|
undef $_[0][2]; |
691 |
|
|
&attach; |
692 |
|
|
} |
693 |
|
|
|
694 |
root |
1.9 |
=back |
695 |
|
|
|
696 |
|
|
=head3 MESSAGE LOGGING |
697 |
|
|
|
698 |
|
|
The following methods configure how the logging context actually does |
699 |
root |
1.10 |
the logging (which consists of formatting the message and printing it or |
700 |
root |
1.9 |
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 |
704 |
|
|
|
705 |
root |
1.8 |
=item $ctx->log_cb ($cb->($str)) |
706 |
root |
1.5 |
|
707 |
root |
1.8 |
Replaces the logging callback on the context (C<undef> disables the |
708 |
|
|
logging callback). |
709 |
root |
1.5 |
|
710 |
root |
1.8 |
The logging callback is responsible for handling formatted log messages |
711 |
|
|
(see C<fmt_cb> below) - normally simple text strings that end with a |
712 |
|
|
newline (and are possibly multiline themselves). |
713 |
|
|
|
714 |
|
|
It also has to return true iff it has consumed the log message, and false |
715 |
|
|
if it hasn't. Consuming a message means that it will not be sent to any |
716 |
|
|
parent context. When in doubt, return C<0> from your logging callback. |
717 |
|
|
|
718 |
|
|
Example: a very simple logging callback, simply dump the message to STDOUT |
719 |
|
|
and do not consume it. |
720 |
|
|
|
721 |
|
|
$ctx->log_cb (sub { print STDERR shift; 0 }); |
722 |
|
|
|
723 |
root |
1.10 |
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 |
|
|
|
734 |
root |
1.8 |
=item $ctx->fmt_cb ($fmt_cb->($timestamp, $ctx, $level, $message)) |
735 |
|
|
|
736 |
root |
1.10 |
Replaces the formatting callback on the context (C<undef> restores the |
737 |
root |
1.8 |
default formatter). |
738 |
|
|
|
739 |
|
|
The callback is passed the (possibly fractional) timestamp, the original |
740 |
|
|
logging context, the (numeric) logging level and the raw message string and needs to |
741 |
|
|
return a formatted log message. In most cases this will be a string, but |
742 |
|
|
it could just as well be an array reference that just stores the values. |
743 |
|
|
|
744 |
|
|
Example: format just the raw message, with numeric log level in angle |
745 |
|
|
brackets. |
746 |
|
|
|
747 |
|
|
$ctx->fmt_cb (sub { |
748 |
|
|
my ($time, $ctx, $lvl, $msg) = @_; |
749 |
|
|
|
750 |
|
|
"<$lvl>$msg\n" |
751 |
|
|
}); |
752 |
|
|
|
753 |
|
|
Example: return an array reference with just the log values, and use |
754 |
|
|
C<PApp::SQL::sql_exec> to store the emssage in a database. |
755 |
|
|
|
756 |
|
|
$ctx->fmt_cb (sub { \@_ }); |
757 |
|
|
$ctx->log_cb (sub { |
758 |
|
|
my ($msg) = @_; |
759 |
|
|
|
760 |
|
|
sql_exec "insert into log (when, subsys, prio, msg) values (?, ?, ?, ?)", |
761 |
|
|
$msg->[0] + 0, |
762 |
|
|
"$msg->[1]", |
763 |
|
|
$msg->[2] + 0, |
764 |
|
|
"$msg->[3]"; |
765 |
|
|
|
766 |
|
|
0 |
767 |
|
|
}); |
768 |
|
|
|
769 |
|
|
=cut |
770 |
|
|
|
771 |
|
|
sub log_cb { |
772 |
|
|
my ($ctx, $cb) = @_; |
773 |
root |
1.6 |
|
774 |
root |
1.10 |
$ctx->[3] = $cb; |
775 |
root |
1.6 |
} |
776 |
root |
1.5 |
|
777 |
root |
1.8 |
sub fmt_cb { |
778 |
|
|
my ($ctx, $cb) = @_; |
779 |
root |
1.6 |
|
780 |
root |
1.8 |
$ctx->[4] = $cb; |
781 |
root |
1.5 |
} |
782 |
|
|
|
783 |
root |
1.8 |
=item $ctx->log ($level, $msg[, @params]) |
784 |
|
|
|
785 |
|
|
Same as C<AnyEvent::Log::log>, but uses the given context as log context. |
786 |
|
|
|
787 |
|
|
=item $logger = $ctx->logger ($level[, \$enabled]) |
788 |
|
|
|
789 |
|
|
Same as C<AnyEvent::Log::logger>, but uses the given context as log |
790 |
|
|
context. |
791 |
|
|
|
792 |
|
|
=cut |
793 |
|
|
|
794 |
|
|
*log = \&AnyEvent::Log::_log; |
795 |
|
|
*logger = \&AnyEvent::Log::_logger; |
796 |
|
|
|
797 |
root |
1.1 |
1; |
798 |
|
|
|
799 |
|
|
=back |
800 |
|
|
|
801 |
root |
1.12 |
=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 |
|
|
=back |
860 |
|
|
|
861 |
root |
1.1 |
=head1 AUTHOR |
862 |
|
|
|
863 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
864 |
|
|
http://home.schmorp.de/ |
865 |
|
|
|
866 |
|
|
=cut |