--- AnyEvent/lib/AnyEvent/Log.pm 2011/08/21 03:20:52 1.23 +++ AnyEvent/lib/AnyEvent/Log.pm 2011/09/05 07:21:54 1.43 @@ -4,7 +4,8 @@ =head1 SYNOPSIS - # simple use +Simple uses: + use AnyEvent; AE::log debug => "hit my knee"; @@ -12,7 +13,11 @@ AE::log error => "the flag was false!"; AE::log fatal => "the bit toggled! run!"; # never returns - # "complex" use (for speed sensitive code) + # available log levels in order: + # fatal alert critical error warn note info debug trace + +"Complex" uses (for speed sensitive code): + use AnyEvent::Log; my $tracer = AnyEvent::Log::logger trace => \$my $trace; @@ -20,7 +25,7 @@ $tracer->("i am here") if $trace; $tracer->(sub { "lots of data: " . Dumper $self }) if $trace; - # configuration +Configuration (also look at the EXAMPLES section): # set logging for the current package to errors and higher only AnyEvent::Log::ctx->level ("error"); @@ -32,11 +37,9 @@ # regardless of (most) other settings $AnyEvent::Log::COLLECT->attach (new AnyEvent::Log::Ctx level => "critical", - log_to_syslog => 0, + log_to_syslog => "user", ); - # see also EXAMPLES, below - =head1 DESCRIPTION This module implements a relatively simple "logging framework". It doesn't @@ -51,15 +54,18 @@ something like: use AnyEvent::Log; - AnyEvent::Log::FILTER->level ("info"); + $AnyEvent::Log::FILTER->level ("info"); The design goal behind this module was to keep it simple (and small), but make it powerful enough to be potentially useful for any module, and extensive enough for the most common tasks, such as logging to multiple targets, or being able to log into a database. -The amount of documentation might indicate otherwise, but the module is -still just below 300 lines of code. +The module is also usable before AnyEvent itself is initialised, in which +case some of the functionality might be reduced. + +The amount of documentation might indicate otherwise, but the runtime part +of the module is still just below 300 lines of code. =head1 LOGGING LEVELS @@ -71,20 +77,26 @@ Instead of specifying levels by name you can also specify them by aliases: LVL NAME SYSLOG PERL NOTE - 1 fatal emerg exit aborts program! - 2 alert - 3 critical crit - 4 error err die - 5 warn warning - 6 note notice - 7 info - 8 debug - 9 trace + 1 fatal emerg exit system unusable, aborts program! + 2 alert failure in primary system + 3 critical crit failure in backup system + 4 error err die non-urgent program errors, a bug + 5 warn warning possible problem, not necessarily error + 6 note notice unusual conditions + 7 info normal messages, no action required + 8 debug debugging messages for development + 9 trace copious tracing output As you can see, some logging levels have multiple aliases - the first one is the "official" name, the second one the "syslog" name (if it differs) -and the third one the "perl" name, suggesting that you log C messages -at C priority. +and the third one the "perl" name, suggesting (only!) that you log C +messages at C priority. The NOTE column tries to provide some +rationale on how to chose a logging level. + +As a rough guideline, levels 1..3 are primarily meant for users of +the program (admins, staff), and are the only logged to STDERR by +default. Levels 4..6 are meant for users and developers alike, while +levels 7..9 are usually meant for developers. You can normally only log a single message at highest priority level (C<1>, C), because logging a fatal message will also quit the @@ -110,7 +122,7 @@ use POSIX (); use AnyEvent (); BEGIN { AnyEvent::common_sense } -use AnyEvent::Util (); +#use AnyEvent::Util (); need to load this in a delayed fashion, as it uses AE::log our $VERSION = $AnyEvent::VERSION; @@ -150,7 +162,7 @@ Requests logging of the given C<$msg> with the given log level, and returns true if the message was logged I. -For C log levels, the program will abort. +For loglevel C, the program will abort. If only a C<$msg> is given, it is logged as-is. With extra C<@args>, the C<$msg> is interpreted as an sprintf format string. @@ -205,10 +217,21 @@ trace => 9, ); -sub now () { time } +our $TIME_EXACT; + +sub exact_time($) { + $TIME_EXACT = shift; + *_ts = $AnyEvent::MODEL + ? $TIME_EXACT ? \&AE::now : \&AE::time + : sub () { $TIME_EXACT ? do { require Time::HiRes; Time::HiRes::time () } : time }; +} + +BEGIN { + exact_time 0; +} AnyEvent::post_detect { - *now = \&AE::now; + exact_time $TIME_EXACT; }; our @LEVEL2STR = qw(0 fatal alert crit error warn note info debug trace); @@ -228,6 +251,10 @@ join "", @res } +sub fatal_exit() { + exit 1; +} + sub _log { my ($ctx, $level, $format, @args) = @_; @@ -251,7 +278,7 @@ $format = $format->() if ref $format; $format = sprintf $format, @args if @args; $format =~ s/\n$//; - $now = AE::now; + $now = _ts; }; # format msg @@ -270,7 +297,7 @@ } while $ctx = pop @ctx; - exit 1 if $level <= 1; + fatal_exit if $level <= 1; $success } @@ -281,8 +308,6 @@ @_; } -*AnyEvent::log = *AE::log = \&log; - =item $logger = AnyEvent::Log::logger $level[, \$enabled] Creates a code reference that, when called, acts as if the @@ -356,10 +381,11 @@ _reassess $logger+0; - my $guard = AnyEvent::Util::guard { + require AnyEvent::Util unless $AnyEvent::Util::VERSION; + my $guard = AnyEvent::Util::guard (sub { # "clean up" delete $LOGGER{$logger+0}; - }; + }); sub { $guard if 0; # keep guard alive, but don't cause runtime overhead @@ -375,6 +401,21 @@ @_ } +=item AnyEvent::Log::exact_time $on + +By default, C will use C, i.e. the cached +eventloop time, for the log timestamps. After calling this function with a +true value it will instead resort to C, i.e. fetch the current +time on each log message. This only makes a difference for event loops +that actually cache the time (such as L or L). + +This setting can be changed at any time by calling this function. + +Since C has to work even before the L has been +initialised, this switch will also decide whether to use C or +C when logging a message before L becomes +available. + =back =head1 LOGGING CONTEXTS @@ -503,6 +544,9 @@ =cut +our $ORIG_VERBOSE = $AnyEvent::VERBOSE; +$AnyEvent::VERBOSE = 9; + sub reset { # hard to kill complex data structures # we "recreate" all package loggers and reset the hierarchy @@ -515,16 +559,13 @@ @$_ = ($_->[0], (1 << 10) - 1 - 1) for $LOG, $FILTER, $COLLECT; - $LOG->slaves; + #$LOG->slaves; $LOG->title ('$AnyEvent::Log::LOG'); - $LOG->log_cb (sub { - warn shift; - 0 - }); + $LOG->log_to_warn; $FILTER->slaves ($LOG); $FILTER->title ('$AnyEvent::Log::FILTER'); - $FILTER->level ($AnyEvent::VERBOSE); + $FILTER->level ($ORIG_VERBOSE); $COLLECT->slaves ($FILTER); $COLLECT->title ('$AnyEvent::Log::COLLECT'); @@ -532,6 +573,14 @@ _reassess; } +# override AE::log/logger +*AnyEvent::log = *AE::log = \&log; +*AnyEvent::logger = *AE::logger = \&logger; + +# convert AnyEvent loggers to AnyEvent::Log loggers +$_->[0] = ctx $_->[0] # convert "pkg" to "ctx" + for values %LOGGER; + # create the default logger contexts $LOG = ctx undef; $FILTER = ctx undef; @@ -791,7 +840,7 @@ string, but it could just as well be an array reference that just stores the values. -If, for some reaosn, you want to use C to find out more baout the +If, for some reason, you want to use C to find out more baout the logger then you should walk up the call stack until you are no longer inside the C package. @@ -820,6 +869,11 @@ 0 }); +=item $ctx->log_to_warn + +Sets the C to simply use C to report any messages +(usually this logs to STDERR). + =item $ctx->log_to_file ($path) Sets the C to log to a file (by appending), unbuffered. @@ -830,12 +884,16 @@ is much slower, but allows you to change/move/rename/delete the file at basically any time. -=item $ctx->log_to_syslog ([$log_flags]) - -Logs all messages via L, mapping C to C and all -the others in the obvious way. If specified, then the C<$log_flags> are -simply or'ed onto the priority argument and can contain any C -flags valid for Sys::Syslog::syslog, except for the priority levels. +Needless(?) to say, if you do not want to be bitten by some evil person +calling C, the path should be absolute. Doesn't help with +C, but hey... + +=item $ctx->log_to_syslog ([$facility]) + +Logs all messages via L, mapping C to C and +all the others in the obvious way. If specified, then the C<$facility> is +used as the facility (C, C, C and so on). The default +facility is C. Note that this function also sets a C - the logging part requires an array reference with [$level, $str] as input. @@ -854,6 +912,15 @@ $ctx->[4] = $cb; } +sub log_to_warn { + my ($ctx, $path) = @_; + + $ctx->log_cb (sub { + warn shift; + 0 + }); +} + sub log_to_file { my ($ctx, $path) = @_; @@ -866,7 +933,7 @@ }); } -sub log_to_file { +sub log_to_path { my ($ctx, $path) = @_; $ctx->log_cb (sub { @@ -879,7 +946,7 @@ } sub log_to_syslog { - my ($ctx, $flags) = @_; + my ($ctx, $facility) = @_; require Sys::Syslog; @@ -890,10 +957,12 @@ [$_[2], "($_[1][0]) $str"] }); + $facility ||= "user"; + $ctx->log_cb (sub { my $lvl = $_[0][0] < 9 ? $_[0][0] : 8; - Sys::Syslog::syslog ($flags | ($lvl - 1), $_) + Sys::Syslog::syslog ("$facility|" . ($lvl - 1), $_) for split /\n/, $_[0][1]; 0 @@ -923,30 +992,259 @@ *log = \&AnyEvent::Log::_log; *logger = \&AnyEvent::Log::_logger; -1; +=back + +=cut + +package AnyEvent::Log; + +=head1 CONFIGURATION VIA $ENV{PERL_ANYEVENT_LOG} + +Logging can also be configured by setting the environment variable +C (or C). + +The value consists of one or more logging context specifications separated +by C<:> or whitespace. Each logging specification in turn starts with a +context name, followed by C<=>, followed by zero or more comma-separated +configuration directives, here are some examples: + + # set default logging level + filter=warn + + # log to file instead of to stderr + log=file=/tmp/mylog + + # log to file in addition to stderr + log=+%file:%file=file=/tmp/mylog + + # enable debug log messages, log warnings and above to syslog + filter=debug:log=+%warnings:%warnings=warn,syslog=LOG_LOCAL0 + + # log trace messages (only) from AnyEvent::Debug to file + AnyEvent::Debug=+%trace:%trace=only,trace,file=/tmp/tracelog + +A context name in the log specification can be any of the following: + +=over 4 + +=item C, C, C + +Correspond to the three predefined C<$AnyEvent::Log::COLLECT>, +C and C<$AnyEvent::Log::LOG> contexts. + +=item C<%name> + +Context names starting with a C<%> are anonymous contexts created when the +name is first mentioned. The difference to package contexts is that by +default they have no attached slaves. + +=item a perl package name + +Any other string references the logging context associated with the given +Perl C. In the unlikely case where you want to specify a package +context that matches on of the other context name forms, you can add a +C<::> to the package name to force interpretation as a package. =back +The configuration specifications can be any number of the following: + +=over 4 + +=item C + +Configures the context to use Perl's C function (which typically +logs to C). Works like C. + +=item CI + +Configures the context to log to a file with the given path. Works like +C. + +=item CI + +Configures the context to log to a file with the given path. Works like +C. + +=item C or CI + +Configures the context to log to syslog. If I is given, then it is +evaluated in the L package, so you could use: + + log=syslog=LOG_LOCAL0 + +=item C + +Configures the context to not log anything by itself, which is the +default. Same as C<< $ctx->log_cb (undef) >>. + +=item C<0> or C + +Sets the logging level of the context ot C<0>, i.e. all messages will be +filtered out. + +=item C + +Enables all logging levels, i.e. filtering will effectively be switched +off (the default). + +=item C + +Disables all logging levels, and changes the interpretation of following +level specifications to enable the specified level only. + +Example: only enable debug messages for a context. + + context=only,debug + +=item C + +Enables all logging levels, and changes the interpretation of following +level specifications to disable that level. Rarely used. + +Example: enable all logging levels except fatal and trace (this is rather +nonsensical). + + filter=exept,fatal,trace + +=item C + +Enables all logging levels, and changes the interpretation of following +level specifications to be "that level or any higher priority +message". This is the default. + +Example: log anything at or above warn level. + + filter=warn + + # or, more verbose + filter=only,level,warn + +=item C<1>..C<9> or a logging level name (C, C etc.) + +A numeric loglevel or the name of a loglevel will be interpreted according +to the most recent C, C or C directive. By default, +specifying a logging level enables that and any higher priority messages. + +=item C<+>I + +Attaches the named context as slave to the context. + +=item C<+> + +A line C<+> detaches all contexts, i.e. clears the slave list from the +context. Anonymous (C<%name>) contexts have no attached slaves by default, +but package contexts have the parent context as slave by default. + +Example: log messages from My::Module to a file, do not send them to the +default log collector. + + My::Module=+,file=/tmp/mymodulelog + +=back + +Any character can be escaped by prefixing it with a C<\> (backslash), as +usual, so to log to a file containing a comma, colon, backslash and some +spaces in the filename, you would do this: + + PERL_ANYEVENT_LOG='log=file=/some\ \:file\ with\,\ \\-escapes' + +Since whitespace (which includes newlines) is allowed, it is fine to +specify multiple lines in C, e.g.: + + PERL_ANYEVENT_LOG=" + filter=warn + AnyEvent::Debug=+%trace + %trace=only,trace,+log + " myprog + +Also, in the unlikely case when you want to concatenate specifications, +use whitespace as separator, as C<::> will be interpreted as part of a +module name, an empty spec with two separators: + + PERL_ANYEVENT_LOG="$PERL_ANYEVENT_LOG MyMod=debug" + +=cut + +for (my $spec = $ENV{PERL_ANYEVENT_LOG}) { + my %anon; + + my $pkg = sub { + $_[0] eq "log" ? $LOG + : $_[0] eq "filter" ? $FILTER + : $_[0] eq "collect" ? $COLLECT + : $_[0] =~ /^%(.+)$/ ? ($anon{$1} ||= ctx undef) + : $_[0] =~ /^(.*?)(?:::)?$/ ? ctx "$1" # egad :/ + : die # never reached? + }; + + /\G[[:space:]]+/gc; # skip initial whitespace + + while (/\G((?:[^:=[:space:]]+|::|\\.)+)=/gc) { + my $ctx = $pkg->($1); + my $level = "level"; + + while (/\G((?:[^,:[:space:]]+|::|\\.)+)/gc) { + for ("$1") { + if ($_ eq "stderr" ) { $ctx->log_to_warn; + } elsif (/^file=(.+)/ ) { $ctx->log_to_file ("$1"); + } elsif (/^path=(.+)/ ) { $ctx->log_to_path ("$1"); + } elsif (/syslog(?:=(.*))?/ ) { require Sys::Syslog; $ctx->log_to_syslog ($1); + } elsif ($_ eq "nolog" ) { $ctx->log_cb (undef); + } elsif (/^\+(.+)$/ ) { $ctx->attach ($pkg->("$1")); + } elsif ($_ eq "+" ) { $ctx->slaves; + } elsif ($_ eq "off" or $_ eq "0") { $ctx->level (0); + } elsif ($_ eq "all" ) { $ctx->level ("all"); + } elsif ($_ eq "level" ) { $ctx->level ("all"); $level = "level"; + } elsif ($_ eq "only" ) { $ctx->level ("off"); $level = "enable"; + } elsif ($_ eq "except" ) { $ctx->level ("all"); $level = "disable"; + } elsif (/^\d$/ ) { $ctx->$level ($_); + } elsif (exists $STR2LEVEL{$_} ) { $ctx->$level ($_); + } else { die "PERL_ANYEVENT_LOG ($spec): parse error at '$_'\n"; + } + } + + /\G,/gc or last; + } + + /\G[:[:space:]]+/gc or last; + } + + /\G[[:space:]]+/gc; # skip trailing whitespace + + if (/\G(.+)/g) { + die "PERL_ANYEVENT_LOG ($spec): parse error at '$1'\n"; + } +} + +1; + =head1 EXAMPLES -This section shows some common configurations. +This section shows some common configurations, both as code, and as +C string. =over 4 =item Setting the global logging level. -Either put PERL_ANYEVENT_VERBOSE= into your environment before -running your program, or modify the log level of the root context: +Either put C into your environment before +running your program, use C or modify the log level of +the root context at runtime: PERL_ANYEVENT_VERBOSE=5 ./myprog + PERL_ANYEVENT_LOG=log=warn + $AnyEvent::Log::FILTER->level ("warn"); =item Append all messages to a file instead of sending them to STDERR. This is affected by the global logging level. - $AnyEvent::Log::LOG->log_to_file ($path); (sub { + $AnyEvent::Log::LOG->log_to_file ($path); + + PERL_ANYEVENT_LOG=log=file=/some/path =item Write all messages with priority C and higher to a file. @@ -954,9 +1252,11 @@ it is attached to the default context which is invoked I global filtering. - $AnyEvent::Log::FILTER->attach + $AnyEvent::Log::FILTER->attach ( new AnyEvent::Log::Ctx log_to_file => $path); + PERL_ANYEVENT_LOG=filter=+%filelogger:%filelogger=file=/some/path + This writes them regardless of the global logging level, because it is attached to the toplevel context, which receives all messages I the global filtering. @@ -964,6 +1264,8 @@ $AnyEvent::Log::COLLECT->attach ( new AnyEvent::Log::Ctx log_to_file => $path); + PERL_ANYEVENT_LOG=%filelogger=file=/some/path:collect=+%filelogger + In both cases, messages are still written to STDERR. =item Write trace messages (only) from L to the default logging target(s). @@ -974,6 +1276,8 @@ my $debug = AnyEvent::Debug->AnyEvent::Log::ctx; $debug->attach ($AnyEvent::Log::LOG); + PERL_ANYEVENT_LOG=AnyEvent::Debug=+log + This of course works for any package, not just L, but assumes the log level for AnyEvent::Debug hasn't been changed from the default. @@ -986,3 +1290,4 @@ http://home.schmorp.de/ =cut +