… | |
… | |
56 | The design goal behind this module was to keep it simple (and small), |
56 | The design goal behind this module was to keep it simple (and small), |
57 | but make it powerful enough to be potentially useful for any module, and |
57 | but make it powerful enough to be potentially useful for any module, and |
58 | extensive enough for the most common tasks, such as logging to multiple |
58 | extensive enough for the most common tasks, such as logging to multiple |
59 | targets, or being able to log into a database. |
59 | targets, or being able to log into a database. |
60 | |
60 | |
61 | The amount of documentation might indicate otherwise, but the module is |
61 | The amount of documentation might indicate otherwise, but the runtime part |
62 | still just below 300 lines of code. |
62 | of the module is still just below 300 lines of code. |
63 | |
63 | |
64 | =head1 LOGGING LEVELS |
64 | =head1 LOGGING LEVELS |
65 | |
65 | |
66 | Logging levels in this module range from C<1> (highest priority) to C<9> |
66 | Logging 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 |
67 | (lowest priority). Note that the lowest numerical value is the highest |
… | |
… | |
249 | # now get raw message, unless we have it already |
249 | # now get raw message, unless we have it already |
250 | unless ($now) { |
250 | unless ($now) { |
251 | $format = $format->() if ref $format; |
251 | $format = $format->() if ref $format; |
252 | $format = sprintf $format, @args if @args; |
252 | $format = sprintf $format, @args if @args; |
253 | $format =~ s/\n$//; |
253 | $format =~ s/\n$//; |
254 | $now = AE::now; |
254 | $now = now; |
255 | }; |
255 | }; |
256 | |
256 | |
257 | # format msg |
257 | # format msg |
258 | my $str = $ctx->[4] |
258 | my $str = $ctx->[4] |
259 | ? $ctx->[4]($now, $_[0], $level, $format) |
259 | ? $ctx->[4]($now, $_[0], $level, $format) |
… | |
… | |
1012 | Configures the context to log to a file with the given path. Works like |
1012 | Configures the context to log to a file with the given path. Works like |
1013 | C<log_to_path>. |
1013 | C<log_to_path>. |
1014 | |
1014 | |
1015 | =item C<syslog> or C<syslog=>I<expr> |
1015 | =item C<syslog> or C<syslog=>I<expr> |
1016 | |
1016 | |
1017 | Configured the context to log to syslog. If I<expr> is given, then it is |
1017 | Configures the context to log to syslog. If I<expr> is given, then it is |
1018 | evaluated in the L<Sys::Syslog> package, so you could use: |
1018 | evaluated in the L<Sys::Syslog> package, so you could use: |
1019 | |
1019 | |
1020 | log=syslog=LOG_LOCAL0 |
1020 | log=syslog=LOG_LOCAL0 |
1021 | |
1021 | |
1022 | =item C<nolog> |
1022 | =item C<nolog> |
… | |
… | |
1064 | filter=warn |
1064 | filter=warn |
1065 | |
1065 | |
1066 | # or, more verbose |
1066 | # or, more verbose |
1067 | filter=only,level,warn |
1067 | filter=only,level,warn |
1068 | |
1068 | |
1069 | =item C<1>..C<9>, a logging level name (C<error>, C<debug> etc.) |
1069 | =item C<1>..C<9> or a logging level name (C<error>, C<debug> etc.) |
1070 | |
1070 | |
1071 | A numeric loglevel or the name of a loglevel will be interpreted according |
1071 | A numeric loglevel or the name of a loglevel will be interpreted according |
1072 | to the most recent C<only>, C<except> or C<level> directive. By default, |
1072 | to the most recent C<only>, C<except> or C<level> directive. By default, |
1073 | specifying a logging level enables that and any higher priority messages. |
1073 | specifying a logging level enables that and any higher priority messages. |
1074 | |
1074 | |
1075 | =item C<+>I<context> |
1075 | =item C<+>I<context> |
1076 | |
1076 | |
1077 | Adds/attaches the named context as slave to the context. |
1077 | Attaches the named context as slave to the context. |
1078 | |
1078 | |
1079 | =item C<+> |
1079 | =item C<+> |
1080 | |
1080 | |
1081 | A line C<+> clears the slave list form the context. Anonymous (C<%name>) |
1081 | A line C<+> detaches all contexts, i.e. clears the slave list from the |
1082 | contexts have no slaves by default, but package contexts have the parent |
1082 | context. Anonymous (C<%name>) contexts have no attached slaves by default, |
1083 | context as slave by default. |
1083 | but package contexts have the parent context as slave by default. |
1084 | |
1084 | |
1085 | Example: log messages from My::Module to a file, do not send them to the |
1085 | Example: log messages from My::Module to a file, do not send them to the |
1086 | default log collector. |
1086 | default log collector. |
1087 | |
1087 | |
1088 | My::Module=+,file=/tmp/mymodulelog |
1088 | My::Module=+,file=/tmp/mymodulelog |
1089 | |
1089 | |
1090 | =back |
1090 | =back |
1091 | |
1091 | |
|
|
1092 | Any character can be escaped by prefixing it with a C<\> (backslash), as |
|
|
1093 | usual, so to log to a file containing a comma, colon, backslash and some |
|
|
1094 | spaces in the filename, you would do this: |
|
|
1095 | |
|
|
1096 | PERL_ANYEVENT_LOG='log=file=/some\ \:file\ with\,\ \\-escapes' |
|
|
1097 | |
|
|
1098 | Since whitespace (which includes newlines) is allowed, it is fine to |
|
|
1099 | specify multiple lines in C<PERL_ANYEVENT_LOG>, e.g.: |
|
|
1100 | |
|
|
1101 | PERL_ANYEVENT_LOG=" |
|
|
1102 | filter=warn |
|
|
1103 | AnyEvent::Debug=+%trace |
|
|
1104 | %trace=only,trace,+log |
|
|
1105 | " myprog |
|
|
1106 | |
|
|
1107 | Also, in the unlikely case when you want to concatenate specifications, |
|
|
1108 | use whitespace as separator, as C<::> will be interpreted as part of a |
|
|
1109 | module name, an empty spec with two separators: |
|
|
1110 | |
|
|
1111 | PERL_ANYEVENT_LOG="$PERL_ANYEVENT_LOG MyMod=debug" |
|
|
1112 | |
1092 | =cut |
1113 | =cut |
1093 | |
1114 | |
1094 | for (my $spec = $ENV{PERL_ANYEVENT_LOG}) { |
1115 | for (my $spec = $ENV{PERL_ANYEVENT_LOG}) { |
1095 | my %anon; |
1116 | my %anon; |
1096 | |
1117 | |
1097 | my $pkg = sub { |
1118 | my $pkg = sub { |
1098 | $_[0] eq "log" ? $LOG |
1119 | $_[0] eq "log" ? $LOG |
1099 | : $_[0] eq "filter" ? $FILTER |
1120 | : $_[0] eq "filter" ? $FILTER |
1100 | : $_[0] eq "collect" ? $COLLECT |
1121 | : $_[0] eq "collect" ? $COLLECT |
1101 | : $_[0] =~ /^%(.+)$/ && $anon{$1} ||= ctx undef |
1122 | : $_[0] =~ /^%(.+)$/ ? ($anon{$1} ||= ctx undef) |
1102 | : $_[0] =~ /^(.*?)(?:::)?$/ && ctx "$1" # egad :/ |
1123 | : $_[0] =~ /^(.*?)(?:::)?$/ ? ctx "$1" # egad :/ |
|
|
1124 | : die # never reached? |
1103 | }; |
1125 | }; |
1104 | |
1126 | |
|
|
1127 | /\G[[:space:]]+/gc; # skip initial whitespace |
|
|
1128 | |
1105 | while (/\G((?:[^:=]+|::|\\.)+)=/gc) { |
1129 | while (/\G((?:[^:=[:space:]]+|::|\\.)+)=/gc) { |
1106 | my $ctx = $pkg->($1); |
1130 | my $ctx = $pkg->($1); |
1107 | my $level = "level"; |
1131 | my $level = "level"; |
1108 | |
1132 | |
1109 | while (/\G((?:[^,:[:space:]]+|::|\\.)+)/gc) { |
1133 | while (/\G((?:[^,:[:space:]]+|::|\\.)+)/gc) { |
1110 | for ("$1") { |
1134 | for ("$1") { |
… | |
… | |
1127 | } |
1151 | } |
1128 | |
1152 | |
1129 | /\G,/gc or last; |
1153 | /\G,/gc or last; |
1130 | } |
1154 | } |
1131 | |
1155 | |
1132 | /\G[:[:space:]]/gc or last; |
1156 | /\G[:[:space:]]+/gc or last; |
1133 | } |
1157 | } |
|
|
1158 | |
|
|
1159 | /\G[[:space:]]+/gc; # skip trailing whitespace |
1134 | |
1160 | |
1135 | if (/\G(.+)/g) { |
1161 | if (/\G(.+)/g) { |
1136 | die "PERL_ANYEVENT_LOG ($spec): parse error at '$1'\n"; |
1162 | die "PERL_ANYEVENT_LOG ($spec): parse error at '$1'\n"; |
1137 | } |
1163 | } |
1138 | } |
1164 | } |
1139 | |
1165 | |
1140 | 1; |
1166 | 1; |
1141 | |
1167 | |
1142 | =head1 EXAMPLES |
1168 | =head1 EXAMPLES |
1143 | |
1169 | |
1144 | This section shows some common configurations. |
1170 | This section shows some common configurations, both as code, and as |
|
|
1171 | C<PERL_ANYEVENT_LOG> string. |
1145 | |
1172 | |
1146 | =over 4 |
1173 | =over 4 |
1147 | |
1174 | |
1148 | =item Setting the global logging level. |
1175 | =item Setting the global logging level. |
1149 | |
1176 | |
1150 | Either put PERL_ANYEVENT_VERBOSE=<number> into your environment before |
1177 | Either put C<PERL_ANYEVENT_VERBOSE=><number> into your environment before |
1151 | running your program, or modify the log level of the root context: |
1178 | running your program, use C<PERL_ANYEVENT_LOG> or modify the log level of |
|
|
1179 | the root context at runtime: |
1152 | |
1180 | |
1153 | PERL_ANYEVENT_VERBOSE=5 ./myprog |
1181 | PERL_ANYEVENT_VERBOSE=5 ./myprog |
1154 | |
1182 | |
|
|
1183 | PERL_ANYEVENT_LOG=log=warn |
|
|
1184 | |
1155 | $AnyEvent::Log::FILTER->level ("warn"); |
1185 | $AnyEvent::Log::FILTER->level ("warn"); |
1156 | |
1186 | |
1157 | =item Append all messages to a file instead of sending them to STDERR. |
1187 | =item Append all messages to a file instead of sending them to STDERR. |
1158 | |
1188 | |
1159 | This is affected by the global logging level. |
1189 | This is affected by the global logging level. |
1160 | |
1190 | |
1161 | $AnyEvent::Log::LOG->log_to_file ($path); (sub { |
1191 | $AnyEvent::Log::LOG->log_to_file ($path); |
|
|
1192 | |
|
|
1193 | PERL_ANYEVENT_LOG=log=file=/some/path |
1162 | |
1194 | |
1163 | =item Write all messages with priority C<error> and higher to a file. |
1195 | =item Write all messages with priority C<error> and higher to a file. |
1164 | |
1196 | |
1165 | This writes them only when the global logging level allows it, because |
1197 | This writes them only when the global logging level allows it, because |
1166 | it is attached to the default context which is invoked I<after> global |
1198 | it is attached to the default context which is invoked I<after> global |
1167 | filtering. |
1199 | filtering. |
1168 | |
1200 | |
1169 | $AnyEvent::Log::FILTER->attach |
1201 | $AnyEvent::Log::FILTER->attach |
1170 | new AnyEvent::Log::Ctx log_to_file => $path); |
1202 | new AnyEvent::Log::Ctx log_to_file => $path); |
1171 | |
1203 | |
|
|
1204 | PERL_ANYEVENT_LOG=filter=+%filelogger:%filelogger=file=/some/path |
|
|
1205 | |
1172 | This writes them regardless of the global logging level, because it is |
1206 | This writes them regardless of the global logging level, because it is |
1173 | attached to the toplevel context, which receives all messages I<before> |
1207 | attached to the toplevel context, which receives all messages I<before> |
1174 | the global filtering. |
1208 | the global filtering. |
1175 | |
1209 | |
1176 | $AnyEvent::Log::COLLECT->attach ( |
1210 | $AnyEvent::Log::COLLECT->attach ( |
1177 | new AnyEvent::Log::Ctx log_to_file => $path); |
1211 | new AnyEvent::Log::Ctx log_to_file => $path); |
1178 | |
1212 | |
|
|
1213 | PERL_ANYEVENT_LOG=%filelogger=file=/some/path:collect=+%filelogger |
|
|
1214 | |
1179 | In both cases, messages are still written to STDERR. |
1215 | In both cases, messages are still written to STDERR. |
1180 | |
1216 | |
1181 | =item Write trace messages (only) from L<AnyEvent::Debug> to the default logging target(s). |
1217 | =item Write trace messages (only) from L<AnyEvent::Debug> to the default logging target(s). |
1182 | |
1218 | |
1183 | Attach the C<$AnyEvent::Log::LOG> context to the C<AnyEvent::Debug> |
1219 | Attach the C<$AnyEvent::Log::LOG> context to the C<AnyEvent::Debug> |
1184 | context - this simply circumvents the global filtering for trace messages. |
1220 | context - this simply circumvents the global filtering for trace messages. |
1185 | |
1221 | |
1186 | my $debug = AnyEvent::Debug->AnyEvent::Log::ctx; |
1222 | my $debug = AnyEvent::Debug->AnyEvent::Log::ctx; |
1187 | $debug->attach ($AnyEvent::Log::LOG); |
1223 | $debug->attach ($AnyEvent::Log::LOG); |
|
|
1224 | |
|
|
1225 | PERL_ANYEVENT_LOG=AnyEvent::Debug=+log |
1188 | |
1226 | |
1189 | This of course works for any package, not just L<AnyEvent::Debug>, but |
1227 | This of course works for any package, not just L<AnyEvent::Debug>, but |
1190 | assumes the log level for AnyEvent::Debug hasn't been changed from the |
1228 | assumes the log level for AnyEvent::Debug hasn't been changed from the |
1191 | default. |
1229 | default. |
1192 | |
1230 | |
… | |
… | |
1196 | |
1234 | |
1197 | Marc Lehmann <schmorp@schmorp.de> |
1235 | Marc Lehmann <schmorp@schmorp.de> |
1198 | http://home.schmorp.de/ |
1236 | http://home.schmorp.de/ |
1199 | |
1237 | |
1200 | =cut |
1238 | =cut |
|
|
1239 | |