ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Log.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Log.pm (file contents):
Revision 1.28 by root, Thu Aug 25 00:08:10 2011 UTC vs.
Revision 1.36 by root, Thu Aug 25 05:39:47 2011 UTC

49will be logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number 49will be logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number
50before starting your program, or change the logging level at runtime with 50before starting your program, or change the logging level at runtime with
51something like: 51something like:
52 52
53 use AnyEvent::Log; 53 use AnyEvent::Log;
54 AnyEvent::Log::FILTER->level ("info"); 54 $AnyEvent::Log::FILTER->level ("info");
55 55
56The design goal behind this module was to keep it simple (and small), 56The design goal behind this module was to keep it simple (and small),
57but make it powerful enough to be potentially useful for any module, and 57but make it powerful enough to be potentially useful for any module, and
58extensive enough for the most common tasks, such as logging to multiple 58extensive enough for the most common tasks, such as logging to multiple
59targets, or being able to log into a database. 59targets, or being able to log into a database.
60 60
61The module is also usable before AnyEvent itself is initialised, in which
62case some of the functionality might be reduced.
63
61The amount of documentation might indicate otherwise, but the module is 64The amount of documentation might indicate otherwise, but the runtime part
62still just below 300 lines of code. 65of the module is still just below 300 lines of code.
63 66
64=head1 LOGGING LEVELS 67=head1 LOGGING LEVELS
65 68
66Logging levels in this module range from C<1> (highest priority) to C<9> 69Logging levels in this module range from C<1> (highest priority) to C<9>
67(lowest priority). Note that the lowest numerical value is the highest 70(lowest priority). Note that the lowest numerical value is the highest
249 # now get raw message, unless we have it already 252 # now get raw message, unless we have it already
250 unless ($now) { 253 unless ($now) {
251 $format = $format->() if ref $format; 254 $format = $format->() if ref $format;
252 $format = sprintf $format, @args if @args; 255 $format = sprintf $format, @args if @args;
253 $format =~ s/\n$//; 256 $format =~ s/\n$//;
254 $now = AE::now; 257 $now = now;
255 }; 258 };
256 259
257 # format msg 260 # format msg
258 my $str = $ctx->[4] 261 my $str = $ctx->[4]
259 ? $ctx->[4]($now, $_[0], $level, $format) 262 ? $ctx->[4]($now, $_[0], $level, $format)
513 } 516 }
514 517
515 @$_ = ($_->[0], (1 << 10) - 1 - 1) 518 @$_ = ($_->[0], (1 << 10) - 1 - 1)
516 for $LOG, $FILTER, $COLLECT; 519 for $LOG, $FILTER, $COLLECT;
517 520
518 $LOG->slaves; 521 #$LOG->slaves;
519 $LOG->title ('$AnyEvent::Log::LOG'); 522 $LOG->title ('$AnyEvent::Log::LOG');
520 $LOG->log_to_warn; 523 $LOG->log_to_warn;
521 524
522 $FILTER->slaves ($LOG); 525 $FILTER->slaves ($LOG);
523 $FILTER->title ('$AnyEvent::Log::FILTER'); 526 $FILTER->title ('$AnyEvent::Log::FILTER');
1012Configures the context to log to a file with the given path. Works like 1015Configures the context to log to a file with the given path. Works like
1013C<log_to_path>. 1016C<log_to_path>.
1014 1017
1015=item C<syslog> or C<syslog=>I<expr> 1018=item C<syslog> or C<syslog=>I<expr>
1016 1019
1017Configured the context to log to syslog. If I<expr> is given, then it is 1020Configures the context to log to syslog. If I<expr> is given, then it is
1018evaluated in the L<Sys::Syslog> package, so you could use: 1021evaluated in the L<Sys::Syslog> package, so you could use:
1019 1022
1020 log=syslog=LOG_LOCAL0 1023 log=syslog=LOG_LOCAL0
1021 1024
1022=item C<nolog> 1025=item C<nolog>
1064 filter=warn 1067 filter=warn
1065 1068
1066 # or, more verbose 1069 # or, more verbose
1067 filter=only,level,warn 1070 filter=only,level,warn
1068 1071
1069=item C<1>..C<9>, a logging level name (C<error>, C<debug> etc.) 1072=item C<1>..C<9> or a logging level name (C<error>, C<debug> etc.)
1070 1073
1071A numeric loglevel or the name of a loglevel will be interpreted according 1074A numeric loglevel or the name of a loglevel will be interpreted according
1072to the most recent C<only>, C<except> or C<level> directive. By default, 1075to the most recent C<only>, C<except> or C<level> directive. By default,
1073specifying a logging level enables that and any higher priority messages. 1076specifying a logging level enables that and any higher priority messages.
1074 1077
1075=item C<+>I<context> 1078=item C<+>I<context>
1076 1079
1077Adds/attaches the named context as slave to the context. 1080Attaches the named context as slave to the context.
1078 1081
1079=item C<+> 1082=item C<+>
1080 1083
1081A line C<+> clears the slave list form the context. Anonymous (C<%name>) 1084A line C<+> detaches all contexts, i.e. clears the slave list from the
1082contexts have no slaves by default, but package contexts have the parent 1085context. Anonymous (C<%name>) contexts have no attached slaves by default,
1083context as slave by default. 1086but package contexts have the parent context as slave by default.
1084 1087
1085Example: log messages from My::Module to a file, do not send them to the 1088Example: log messages from My::Module to a file, do not send them to the
1086default log collector. 1089default log collector.
1087 1090
1088 My::Module=+,file=/tmp/mymodulelog 1091 My::Module=+,file=/tmp/mymodulelog
1089 1092
1090=back 1093=back
1091 1094
1095Any character can be escaped by prefixing it with a C<\> (backslash), as
1096usual, so to log to a file containing a comma, colon, backslash and some
1097spaces in the filename, you would do this:
1098
1099 PERL_ANYEVENT_LOG='log=file=/some\ \:file\ with\,\ \\-escapes'
1100
1101Since whitespace (which includes newlines) is allowed, it is fine to
1102specify multiple lines in C<PERL_ANYEVENT_LOG>, e.g.:
1103
1104 PERL_ANYEVENT_LOG="
1105 filter=warn
1106 AnyEvent::Debug=+%trace
1107 %trace=only,trace,+log
1108 " myprog
1109
1110Also, in the unlikely case when you want to concatenate specifications,
1111use whitespace as separator, as C<::> will be interpreted as part of a
1112module name, an empty spec with two separators:
1113
1114 PERL_ANYEVENT_LOG="$PERL_ANYEVENT_LOG MyMod=debug"
1115
1092=cut 1116=cut
1093 1117
1094for (my $spec = $ENV{PERL_ANYEVENT_LOG}) { 1118for (my $spec = $ENV{PERL_ANYEVENT_LOG}) {
1095 my %anon; 1119 my %anon;
1096 1120
1097 my $pkg = sub { 1121 my $pkg = sub {
1098 $_[0] eq "log" ? $LOG 1122 $_[0] eq "log" ? $LOG
1099 : $_[0] eq "filter" ? $FILTER 1123 : $_[0] eq "filter" ? $FILTER
1100 : $_[0] eq "collect" ? $COLLECT 1124 : $_[0] eq "collect" ? $COLLECT
1101 : $_[0] =~ /^%(.+)$/ && $anon{$1} ||= ctx undef 1125 : $_[0] =~ /^%(.+)$/ ? ($anon{$1} ||= ctx undef)
1102 : $_[0] =~ /^(.*?)(?:::)?$/ && ctx "$1" # egad :/ 1126 : $_[0] =~ /^(.*?)(?:::)?$/ ? ctx "$1" # egad :/
1127 : die # never reached?
1103 }; 1128 };
1104 1129
1130 /\G[[:space:]]+/gc; # skip initial whitespace
1131
1105 while (/\G((?:[^:=]+|::|\\.)+)=/gc) { 1132 while (/\G((?:[^:=[:space:]]+|::|\\.)+)=/gc) {
1106 my $ctx = $pkg->($1); 1133 my $ctx = $pkg->($1);
1107 my $level = "level"; 1134 my $level = "level";
1108 1135
1109 while (/\G((?:[^,:[:space:]]+|::|\\.)+)/gc) { 1136 while (/\G((?:[^,:[:space:]]+|::|\\.)+)/gc) {
1110 for ("$1") { 1137 for ("$1") {
1127 } 1154 }
1128 1155
1129 /\G,/gc or last; 1156 /\G,/gc or last;
1130 } 1157 }
1131 1158
1132 /\G[:[:space:]]/gc or last; 1159 /\G[:[:space:]]+/gc or last;
1133 } 1160 }
1161
1162 /\G[[:space:]]+/gc; # skip trailing whitespace
1134 1163
1135 if (/\G(.+)/g) { 1164 if (/\G(.+)/g) {
1136 die "PERL_ANYEVENT_LOG ($spec): parse error at '$1'\n"; 1165 die "PERL_ANYEVENT_LOG ($spec): parse error at '$1'\n";
1137 } 1166 }
1138} 1167}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines