… | |
… | |
32 | |
32 | |
33 | # send all critical and higher priority messages to syslog, |
33 | # send all critical and higher priority messages to syslog, |
34 | # regardless of (most) other settings |
34 | # regardless of (most) other settings |
35 | $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx |
35 | $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx |
36 | level => "critical", |
36 | level => "critical", |
37 | log_to_syslog => 0, |
37 | log_to_syslog => "user", |
38 | ); |
38 | ); |
39 | |
39 | |
40 | =head1 DESCRIPTION |
40 | =head1 DESCRIPTION |
41 | |
41 | |
42 | This module implements a relatively simple "logging framework". It doesn't |
42 | This module implements a relatively simple "logging framework". It doesn't |
… | |
… | |
111 | |
111 | |
112 | use Carp (); |
112 | use Carp (); |
113 | use POSIX (); |
113 | use POSIX (); |
114 | |
114 | |
115 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
115 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
116 | use AnyEvent::Util (); |
116 | #use AnyEvent::Util (); need to load this in a delayed fashion, as it uses AE::log |
117 | |
117 | |
118 | our $VERSION = $AnyEvent::VERSION; |
118 | our $VERSION = $AnyEvent::VERSION; |
119 | |
119 | |
120 | our ($COLLECT, $FILTER, $LOG); |
120 | our ($COLLECT, $FILTER, $LOG); |
121 | |
121 | |
… | |
… | |
206 | info => 7, |
206 | info => 7, |
207 | debug => 8, |
207 | debug => 8, |
208 | trace => 9, |
208 | trace => 9, |
209 | ); |
209 | ); |
210 | |
210 | |
211 | sub now () { time } |
211 | our $TIME_EXACT; |
|
|
212 | |
|
|
213 | sub exact_time($) { |
|
|
214 | $TIME_EXACT = shift; |
|
|
215 | *_ts = $AnyEvent::MODEL |
|
|
216 | ? $TIME_EXACT ? \&AE::now : \&AE::time |
|
|
217 | : sub () { $TIME_EXACT ? do { require Time::HiRes; Time::HiRes::time () } : time }; |
|
|
218 | } |
|
|
219 | |
|
|
220 | BEGIN { |
|
|
221 | exact_time 0; |
|
|
222 | } |
212 | |
223 | |
213 | AnyEvent::post_detect { |
224 | AnyEvent::post_detect { |
214 | *now = \&AE::now; |
225 | exact_time $TIME_EXACT; |
215 | }; |
226 | }; |
216 | |
227 | |
217 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
228 | our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); |
218 | |
229 | |
219 | # time, ctx, level, msg |
230 | # time, ctx, level, msg |
… | |
… | |
252 | # now get raw message, unless we have it already |
263 | # now get raw message, unless we have it already |
253 | unless ($now) { |
264 | unless ($now) { |
254 | $format = $format->() if ref $format; |
265 | $format = $format->() if ref $format; |
255 | $format = sprintf $format, @args if @args; |
266 | $format = sprintf $format, @args if @args; |
256 | $format =~ s/\n$//; |
267 | $format =~ s/\n$//; |
257 | $now = now; |
268 | $now = _ts; |
258 | }; |
269 | }; |
259 | |
270 | |
260 | # format msg |
271 | # format msg |
261 | my $str = $ctx->[4] |
272 | my $str = $ctx->[4] |
262 | ? $ctx->[4]($now, $_[0], $level, $format) |
273 | ? $ctx->[4]($now, $_[0], $level, $format) |
… | |
… | |
357 | |
368 | |
358 | $LOGGER{$logger+0} = $logger; |
369 | $LOGGER{$logger+0} = $logger; |
359 | |
370 | |
360 | _reassess $logger+0; |
371 | _reassess $logger+0; |
361 | |
372 | |
|
|
373 | require AnyEvent::Util; |
362 | my $guard = AnyEvent::Util::guard { |
374 | my $guard = AnyEvent::Util::guard (sub { |
363 | # "clean up" |
375 | # "clean up" |
364 | delete $LOGGER{$logger+0}; |
376 | delete $LOGGER{$logger+0}; |
365 | }; |
377 | }); |
366 | |
378 | |
367 | sub { |
379 | sub { |
368 | $guard if 0; # keep guard alive, but don't cause runtime overhead |
380 | $guard if 0; # keep guard alive, but don't cause runtime overhead |
369 | |
381 | |
370 | _log $ctx, $level, @_ |
382 | _log $ctx, $level, @_ |
… | |
… | |
375 | sub logger($;$) { |
387 | sub logger($;$) { |
376 | _logger |
388 | _logger |
377 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
389 | $CTX{ (caller)[0] } ||= _pkg_ctx +(caller)[0], |
378 | @_ |
390 | @_ |
379 | } |
391 | } |
|
|
392 | |
|
|
393 | =item AnyEvent::Log::exact_time $on |
|
|
394 | |
|
|
395 | By default, C<AnyEvent::Log> will use C<AE::now>, i.e. the cached |
|
|
396 | eventloop time, for the log timestamps. After calling this function with a |
|
|
397 | true value it will instead resort to C<AE::time>, i.e. fetch the current |
|
|
398 | time on each log message. This only makes a difference for event loops |
|
|
399 | that actually cache the time (such as L<EV> or L<AnyEvent::Loop>). |
|
|
400 | |
|
|
401 | This setting can be changed at any time by calling this function. |
|
|
402 | |
|
|
403 | Since C<AnyEvent::Log> has to work even before the L<AnyEvent> has been |
|
|
404 | initialised, this switch will also decide whether to use C<CORE::time> or |
|
|
405 | C<Time::HiRes::time> when logging a message before L<AnyEvent> becomes |
|
|
406 | available. |
380 | |
407 | |
381 | =back |
408 | =back |
382 | |
409 | |
383 | =head1 LOGGING CONTEXTS |
410 | =head1 LOGGING CONTEXTS |
384 | |
411 | |
… | |
… | |
837 | |
864 | |
838 | Needless(?) to say, if you do not want to be bitten by some evil person |
865 | Needless(?) to say, if you do not want to be bitten by some evil person |
839 | calling C<chdir>, the path should be absolute. Doesn't help with |
866 | calling C<chdir>, the path should be absolute. Doesn't help with |
840 | C<chroot>, but hey... |
867 | C<chroot>, but hey... |
841 | |
868 | |
842 | =item $ctx->log_to_syslog ([$log_flags]) |
869 | =item $ctx->log_to_syslog ([$facility]) |
843 | |
870 | |
844 | Logs all messages via L<Sys::Syslog>, mapping C<trace> to C<debug> and all |
871 | Logs all messages via L<Sys::Syslog>, mapping C<trace> to C<debug> and |
845 | the others in the obvious way. If specified, then the C<$log_flags> are |
872 | all the others in the obvious way. If specified, then the C<$facility> is |
846 | simply or'ed onto the priority argument and can contain any C<LOG_xxx> |
873 | used as the facility (C<user>, C<auth>, C<local0> and so on). The default |
847 | flags valid for Sys::Syslog::syslog, except for the priority levels. |
874 | facility is C<user>. |
848 | |
875 | |
849 | Note that this function also sets a C<fmt_cb> - the logging part requires |
876 | Note that this function also sets a C<fmt_cb> - the logging part requires |
850 | an array reference with [$level, $str] as input. |
877 | an array reference with [$level, $str] as input. |
851 | |
878 | |
852 | =cut |
879 | =cut |
… | |
… | |
895 | 0 |
922 | 0 |
896 | }); |
923 | }); |
897 | } |
924 | } |
898 | |
925 | |
899 | sub log_to_syslog { |
926 | sub log_to_syslog { |
900 | my ($ctx, $flags) = @_; |
927 | my ($ctx, $facility) = @_; |
901 | |
928 | |
902 | require Sys::Syslog; |
929 | require Sys::Syslog; |
903 | |
930 | |
904 | $ctx->fmt_cb (sub { |
931 | $ctx->fmt_cb (sub { |
905 | my $str = $_[3]; |
932 | my $str = $_[3]; |
906 | $str =~ s/\n(?=.)/\n+ /g; |
933 | $str =~ s/\n(?=.)/\n+ /g; |
907 | |
934 | |
908 | [$_[2], "($_[1][0]) $str"] |
935 | [$_[2], "($_[1][0]) $str"] |
909 | }); |
936 | }); |
910 | |
937 | |
|
|
938 | $facility ||= "user"; |
|
|
939 | |
911 | $ctx->log_cb (sub { |
940 | $ctx->log_cb (sub { |
912 | my $lvl = $_[0][0] < 9 ? $_[0][0] : 8; |
941 | my $lvl = $_[0][0] < 9 ? $_[0][0] : 8; |
913 | |
942 | |
914 | Sys::Syslog::syslog ($flags | ($lvl - 1), $_) |
943 | Sys::Syslog::syslog ("$facility|" . ($lvl - 1), $_) |
915 | for split /\n/, $_[0][1]; |
944 | for split /\n/, $_[0][1]; |
916 | |
945 | |
917 | 0 |
946 | 0 |
918 | }); |
947 | }); |
919 | } |
948 | } |
… | |
… | |
1136 | while (/\G((?:[^,:[:space:]]+|::|\\.)+)/gc) { |
1165 | while (/\G((?:[^,:[:space:]]+|::|\\.)+)/gc) { |
1137 | for ("$1") { |
1166 | for ("$1") { |
1138 | if ($_ eq "stderr" ) { $ctx->log_to_warn; |
1167 | if ($_ eq "stderr" ) { $ctx->log_to_warn; |
1139 | } elsif (/^file=(.+)/ ) { $ctx->log_to_file ("$1"); |
1168 | } elsif (/^file=(.+)/ ) { $ctx->log_to_file ("$1"); |
1140 | } elsif (/^path=(.+)/ ) { $ctx->log_to_path ("$1"); |
1169 | } elsif (/^path=(.+)/ ) { $ctx->log_to_path ("$1"); |
1141 | } elsif (/syslog(?:=(.*))?/ ) { require Sys::Syslog; $ctx->log_to_syslog (eval "package Sys::Syslog; $1"); |
1170 | } elsif (/syslog(?:=(.*))?/ ) { require Sys::Syslog; $ctx->log_to_syslog ($1); |
1142 | } elsif ($_ eq "nolog" ) { $ctx->log_cb (undef); |
1171 | } elsif ($_ eq "nolog" ) { $ctx->log_cb (undef); |
1143 | } elsif (/^\+(.+)$/ ) { $ctx->attach ($pkg->("$1")); |
1172 | } elsif (/^\+(.+)$/ ) { $ctx->attach ($pkg->("$1")); |
1144 | } elsif ($_ eq "+" ) { $ctx->slaves; |
1173 | } elsif ($_ eq "+" ) { $ctx->slaves; |
1145 | } elsif ($_ eq "off" or $_ eq "0") { $ctx->level (0); |
1174 | } elsif ($_ eq "off" or $_ eq "0") { $ctx->level (0); |
1146 | } elsif ($_ eq "all" ) { $ctx->level ("all"); |
1175 | } elsif ($_ eq "all" ) { $ctx->level ("all"); |