… | |
… | |
6 | |
6 | |
7 | use AnyEvent::Log; |
7 | use AnyEvent::Log; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
|
|
11 | This module implements a relatively simple "logging framework". It doesn't |
|
|
12 | attempt to be "the" logging solution or even "a" logging solution for |
|
|
13 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
|
|
14 | module more or less exposes the mechanism, with some extra spiff to allow |
|
|
15 | using it from other modules as well. |
|
|
16 | |
|
|
17 | Remember that the default verbosity level is C<0>, so nothing |
|
|
18 | will be logged, ever, unless you set C<$Anyvent::VERBOSE> or |
|
|
19 | C<PERL_ANYEVENT_VERBOSE> to a higher number. |
|
|
20 | |
|
|
21 | Possible future extensions are to allow custom log targets (where the |
|
|
22 | level is an object), log filtering based on package, formatting, aliasing |
|
|
23 | or package groups. |
|
|
24 | |
11 | =head1 FUNCTIONS |
25 | =head1 LOG FUNCTIONS |
|
|
26 | |
|
|
27 | These functions allow you to log messages. They always use the caller's |
|
|
28 | package as a "logging module/source". Also, The main logging function is |
|
|
29 | easily available as C<AnyEvent::log> or C<AE::log> when the C<AnyEvent> |
|
|
30 | module is loaded. |
12 | |
31 | |
13 | =over 4 |
32 | =over 4 |
14 | |
33 | |
15 | =cut |
34 | =cut |
16 | |
35 | |
17 | package AnyEvent::Log; |
36 | package AnyEvent::Log; |
18 | |
37 | |
|
|
38 | use Carp (); |
19 | use POSIX (); |
39 | use POSIX (); |
20 | |
40 | |
21 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
41 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
|
|
42 | |
|
|
43 | our ($now_int, $now_str1, $now_str2); |
|
|
44 | |
|
|
45 | # Format Time, not public - yet? |
|
|
46 | sub ft($) { |
|
|
47 | my $i = int $_[0]; |
|
|
48 | my $f = sprintf "%06d", 1e6 * ($_[0] - $i); |
|
|
49 | |
|
|
50 | ($now_int, $now_str1, $now_str2) = ($i, split /\x01/, POSIX::strftime "%Y-%m-%d %H:%M:%S.\x01 %z", localtime $i) |
|
|
51 | if $now_int != $i; |
|
|
52 | |
|
|
53 | "$now_str1$f$now_str2" |
|
|
54 | } |
|
|
55 | |
|
|
56 | =item AnyEvent::Log::log $level, $msg[, @args] |
|
|
57 | |
|
|
58 | Requests logging of the given C<$msg> with the given log level (1..9). |
|
|
59 | You can also use the following strings as log level: C<fatal> (1), |
|
|
60 | C<alert> (2), C<critical> (3), C<error> (4), C<warn> (5), C<note> (6), |
|
|
61 | C<info> (7), C<debug> (8), C<trace> (9). |
|
|
62 | |
|
|
63 | For C<fatal> log levels, the program will abort. |
|
|
64 | |
|
|
65 | If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the |
|
|
66 | C<$msg> is interpreted as an sprintf format string. |
|
|
67 | |
|
|
68 | The C<$msg> should not end with C<\n>, but may if that is convenient for |
|
|
69 | you. Also, multiline messages are handled properly. |
|
|
70 | |
|
|
71 | In addition, for possible future expansion, C<$msg> must not start with an |
|
|
72 | angle bracket (C<< < >>). |
|
|
73 | |
|
|
74 | Whether the given message will be logged depends on the maximum log level |
|
|
75 | and the caller's package. |
|
|
76 | |
|
|
77 | Note that you can (and should) call this function as C<AnyEvent::log> or |
|
|
78 | C<AE::log>, without C<use>-ing this module if possible, as those functions |
|
|
79 | will laod the logging module on demand only. |
|
|
80 | |
|
|
81 | =cut |
|
|
82 | |
|
|
83 | # also allow syslog equivalent names |
|
|
84 | our %STR2LEVEL = ( |
|
|
85 | fatal => 1, emerg => 1, |
|
|
86 | alert => 2, |
|
|
87 | critical => 3, crit => 3, |
|
|
88 | error => 4, err => 4, |
|
|
89 | warn => 5, warning => 5, |
|
|
90 | note => 6, notice => 6, |
|
|
91 | info => 7, |
|
|
92 | debug => 8, |
|
|
93 | trace => 9, |
|
|
94 | ); |
|
|
95 | |
|
|
96 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
|
|
97 | |
|
|
98 | sub log($$;@) { |
|
|
99 | my ($targ, $msg, @args) = @_; |
|
|
100 | |
|
|
101 | my $level = ref $targ ? die "Can't use reference as logging level (yet)" |
|
|
102 | : $targ > 0 && $targ <= 9 ? $targ+0 |
|
|
103 | : $STR2LEVEL{$targ} || Carp::croak "$targ: not a valid logging level, caught"; |
|
|
104 | |
|
|
105 | return if $level > $AnyEvent::VERBOSE; |
|
|
106 | |
|
|
107 | my $pkg = (caller)[0]; |
|
|
108 | |
|
|
109 | $msg = sprintf $msg, @args if @args; |
|
|
110 | $msg =~ s/\n$//; |
|
|
111 | |
|
|
112 | # now we have a message, log it |
|
|
113 | #TODO: could do LOTS of stuff here, and should, at least in some later version |
|
|
114 | |
|
|
115 | $msg = sprintf "%5s (%s) %s", $LEVEL2STR[$level], $pkg, $msg; |
|
|
116 | my $pfx = ft AE::now; |
|
|
117 | |
|
|
118 | for (split /\n/, $msg) { |
|
|
119 | printf STDERR "$pfx $_\n"; |
|
|
120 | $pfx = "\t"; |
|
|
121 | } |
|
|
122 | |
|
|
123 | exit 1 if $level <= 1; |
|
|
124 | } |
|
|
125 | |
|
|
126 | *AnyEvent::log = *AE::log = \&log; |
|
|
127 | |
|
|
128 | #TODO |
|
|
129 | |
|
|
130 | =back |
|
|
131 | |
|
|
132 | =head1 CONFIGURATION FUNCTIONALITY |
|
|
133 | |
|
|
134 | None, yet, except for C<PERL_ANYEVENT_VERBOSE>, described in the L<AnyEvent> manpage. |
|
|
135 | |
|
|
136 | =over 4 |
|
|
137 | |
|
|
138 | =cut |
22 | |
139 | |
23 | 1; |
140 | 1; |
24 | |
141 | |
25 | =back |
142 | =back |
26 | |
143 | |