… | |
… | |
49 | will be logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number |
49 | will be logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number |
50 | before starting your program, or change the logging level at runtime with |
50 | before starting your program, or change the logging level at runtime with |
51 | something like: |
51 | something 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 | |
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 module is also usable before AnyEvent itself is initialised, in which |
|
|
62 | case some of the functionality might be reduced. |
|
|
63 | |
61 | The amount of documentation might indicate otherwise, but the module is |
64 | The amount of documentation might indicate otherwise, but the runtime part |
62 | still just below 300 lines of code. |
65 | of the module is still just below 300 lines of code. |
63 | |
66 | |
64 | =head1 LOGGING LEVELS |
67 | =head1 LOGGING LEVELS |
65 | |
68 | |
66 | Logging levels in this module range from C<1> (highest priority) to C<9> |
69 | 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 |
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'); |
… | |
… | |
967 | filter=debug:log=+%warnings:%warnings=warn,syslog=LOG_LOCAL0 |
970 | filter=debug:log=+%warnings:%warnings=warn,syslog=LOG_LOCAL0 |
968 | |
971 | |
969 | # log trace messages (only) from AnyEvent::Debug to file |
972 | # log trace messages (only) from AnyEvent::Debug to file |
970 | AnyEvent::Debug=+%trace:%trace=only,trace,file=/tmp/tracelog |
973 | AnyEvent::Debug=+%trace:%trace=only,trace,file=/tmp/tracelog |
971 | |
974 | |
|
|
975 | A context name in the log specification can be any of the following: |
|
|
976 | |
|
|
977 | =over 4 |
|
|
978 | |
|
|
979 | =item C<collect>, C<filter>, C<log> |
|
|
980 | |
|
|
981 | Correspond to the three predefined C<$AnyEvent::Log::COLLECT>, |
|
|
982 | C<AnyEvent::Log::FILTER> and C<$AnyEvent::Log::LOG> contexts. |
|
|
983 | |
|
|
984 | =item C<%name> |
|
|
985 | |
|
|
986 | Context names starting with a C<%> are anonymous contexts created when the |
|
|
987 | name is first mentioned. The difference to package contexts is that by |
|
|
988 | default they have no attached slaves. |
|
|
989 | |
|
|
990 | =item a perl package name |
|
|
991 | |
|
|
992 | Any other string references the logging context associated with the given |
|
|
993 | Perl C<package>. In the unlikely case where you want to specify a package |
|
|
994 | context that matches on of the other context name forms, you can add a |
|
|
995 | C<::> to the package name to force interpretation as a package. |
|
|
996 | |
|
|
997 | =back |
|
|
998 | |
|
|
999 | The configuration specifications can be any number of the following: |
|
|
1000 | |
|
|
1001 | =over 4 |
|
|
1002 | |
|
|
1003 | =item C<stderr> |
|
|
1004 | |
|
|
1005 | Configures the context to use Perl's C<warn> function (which typically |
|
|
1006 | logs to C<STDERR>). Works like C<log_to_warn>. |
|
|
1007 | |
|
|
1008 | =item C<file=>I<path> |
|
|
1009 | |
|
|
1010 | Configures the context to log to a file with the given path. Works like |
|
|
1011 | C<log_to_file>. |
|
|
1012 | |
|
|
1013 | =item C<path=>I<path> |
|
|
1014 | |
|
|
1015 | Configures the context to log to a file with the given path. Works like |
|
|
1016 | C<log_to_path>. |
|
|
1017 | |
|
|
1018 | =item C<syslog> or C<syslog=>I<expr> |
|
|
1019 | |
|
|
1020 | Configures the context to log to syslog. If I<expr> is given, then it is |
|
|
1021 | evaluated in the L<Sys::Syslog> package, so you could use: |
|
|
1022 | |
|
|
1023 | log=syslog=LOG_LOCAL0 |
|
|
1024 | |
|
|
1025 | =item C<nolog> |
|
|
1026 | |
|
|
1027 | Configures the context to not log anything by itself, which is the |
|
|
1028 | default. Same as C<< $ctx->log_cb (undef) >>. |
|
|
1029 | |
|
|
1030 | =item C<0> or C<off> |
|
|
1031 | |
|
|
1032 | Sets the logging level of the context ot C<0>, i.e. all messages will be |
|
|
1033 | filtered out. |
|
|
1034 | |
|
|
1035 | =item C<all> |
|
|
1036 | |
|
|
1037 | Enables all logging levels, i.e. filtering will effectively be switched |
|
|
1038 | off (the default). |
|
|
1039 | |
|
|
1040 | =item C<only> |
|
|
1041 | |
|
|
1042 | Disables all logging levels, and changes the interpretation of following |
|
|
1043 | level specifications to enable the specified level only. |
|
|
1044 | |
|
|
1045 | Example: only enable debug messages for a context. |
|
|
1046 | |
|
|
1047 | context=only,debug |
|
|
1048 | |
|
|
1049 | =item C<except> |
|
|
1050 | |
|
|
1051 | Enables all logging levels, and changes the interpretation of following |
|
|
1052 | level specifications to disable that level. Rarely used. |
|
|
1053 | |
|
|
1054 | Example: enable all logging levels except fatal and trace (this is rather |
|
|
1055 | nonsensical). |
|
|
1056 | |
|
|
1057 | filter=exept,fatal,trace |
|
|
1058 | |
|
|
1059 | =item C<level> |
|
|
1060 | |
|
|
1061 | Enables all logging levels, and changes the interpretation of following |
|
|
1062 | level specifications to be "that level or any higher priority |
|
|
1063 | message". This is the default. |
|
|
1064 | |
|
|
1065 | Example: log anything at or above warn level. |
|
|
1066 | |
|
|
1067 | filter=warn |
|
|
1068 | |
|
|
1069 | # or, more verbose |
|
|
1070 | filter=only,level,warn |
|
|
1071 | |
|
|
1072 | =item C<1>..C<9> or a logging level name (C<error>, C<debug> etc.) |
|
|
1073 | |
|
|
1074 | A numeric loglevel or the name of a loglevel will be interpreted according |
|
|
1075 | to the most recent C<only>, C<except> or C<level> directive. By default, |
|
|
1076 | specifying a logging level enables that and any higher priority messages. |
|
|
1077 | |
|
|
1078 | =item C<+>I<context> |
|
|
1079 | |
|
|
1080 | Attaches the named context as slave to the context. |
|
|
1081 | |
|
|
1082 | =item C<+> |
|
|
1083 | |
|
|
1084 | A line C<+> detaches all contexts, i.e. clears the slave list from the |
|
|
1085 | context. Anonymous (C<%name>) contexts have no attached slaves by default, |
|
|
1086 | but package contexts have the parent context as slave by default. |
|
|
1087 | |
|
|
1088 | Example: log messages from My::Module to a file, do not send them to the |
|
|
1089 | default log collector. |
|
|
1090 | |
|
|
1091 | My::Module=+,file=/tmp/mymodulelog |
|
|
1092 | |
|
|
1093 | =back |
|
|
1094 | |
|
|
1095 | Any character can be escaped by prefixing it with a C<\> (backslash), as |
|
|
1096 | usual, so to log to a file containing a comma, colon, backslash and some |
|
|
1097 | spaces in the filename, you would do this: |
|
|
1098 | |
|
|
1099 | PERL_ANYEVENT_LOG='log=file=/some\ \:file\ with\,\ \\-escapes' |
|
|
1100 | |
972 | Since whitespace (which includes newlines) is allowed, it is fine to |
1101 | Since whitespace (which includes newlines) is allowed, it is fine to |
973 | specify multiple lines in C<PERL_ANYEVENT_LOG>, e.g.: |
1102 | specify multiple lines in C<PERL_ANYEVENT_LOG>, e.g.: |
974 | |
1103 | |
975 | PERL_ANYEVENT_LOG=" |
1104 | PERL_ANYEVENT_LOG=" |
976 | filter=warn |
1105 | filter=warn |
977 | AnyEvent::Debug=+%trace |
1106 | AnyEvent::Debug=+%trace |
978 | %trace=only,trace,+log |
1107 | %trace=only,trace,+log |
979 | " myprog |
1108 | " myprog |
980 | |
1109 | |
981 | A context name in the log specification can be any of the following: |
1110 | Also, in the unlikely case when you want to concatenate specifications, |
|
|
1111 | use whitespace as separator, as C<::> will be interpreted as part of a |
|
|
1112 | module name, an empty spec with two separators: |
982 | |
1113 | |
983 | =over 4 |
1114 | PERL_ANYEVENT_LOG="$PERL_ANYEVENT_LOG MyMod=debug" |
984 | |
|
|
985 | =item C<collect>, C<filter>, C<log> |
|
|
986 | |
|
|
987 | Correspond to the three predefined C<$AnyEvent::Log::COLLECT>, |
|
|
988 | C<AnyEvent::Log::FILTER> and C<$AnyEvent::Log::LOG> contexts. |
|
|
989 | |
|
|
990 | =item C<%name> |
|
|
991 | |
|
|
992 | Context names starting with a C<%> are anonymous contexts created when the |
|
|
993 | name is first mentioned. The difference to package contexts is that by |
|
|
994 | default they have no attached slaves. |
|
|
995 | |
|
|
996 | =item a perl package name |
|
|
997 | |
|
|
998 | Any other string references the logging context associated with the given |
|
|
999 | Perl C<package>. In the unlikely case where you want to specify a package |
|
|
1000 | context that matches on of the other context name forms, you can add a |
|
|
1001 | C<::> to the package name to force interpretation as a package. |
|
|
1002 | |
|
|
1003 | =back |
|
|
1004 | |
|
|
1005 | The configuration specifications can be any number of the following: |
|
|
1006 | |
|
|
1007 | =over 4 |
|
|
1008 | |
|
|
1009 | =item C<stderr> |
|
|
1010 | |
|
|
1011 | Configures the context to use Perl's C<warn> function (which typically |
|
|
1012 | logs to C<STDERR>). Works like C<log_to_warn>. |
|
|
1013 | |
|
|
1014 | =item C<file=>I<path> |
|
|
1015 | |
|
|
1016 | Configures the context to log to a file with the given path. Works like |
|
|
1017 | C<log_to_file>. |
|
|
1018 | |
|
|
1019 | =item C<path=>I<path> |
|
|
1020 | |
|
|
1021 | Configures the context to log to a file with the given path. Works like |
|
|
1022 | C<log_to_path>. |
|
|
1023 | |
|
|
1024 | =item C<syslog> or C<syslog=>I<expr> |
|
|
1025 | |
|
|
1026 | Configured the context to log to syslog. If I<expr> is given, then it is |
|
|
1027 | evaluated in the L<Sys::Syslog> package, so you could use: |
|
|
1028 | |
|
|
1029 | log=syslog=LOG_LOCAL0 |
|
|
1030 | |
|
|
1031 | =item C<nolog> |
|
|
1032 | |
|
|
1033 | Configures the context to not log anything by itself, which is the |
|
|
1034 | default. Same as C<< $ctx->log_cb (undef) >>. |
|
|
1035 | |
|
|
1036 | =item C<0> or C<off> |
|
|
1037 | |
|
|
1038 | Sets the logging level of the context ot C<0>, i.e. all messages will be |
|
|
1039 | filtered out. |
|
|
1040 | |
|
|
1041 | =item C<all> |
|
|
1042 | |
|
|
1043 | Enables all logging levels, i.e. filtering will effectively be switched |
|
|
1044 | off (the default). |
|
|
1045 | |
|
|
1046 | =item C<only> |
|
|
1047 | |
|
|
1048 | Disables all logging levels, and changes the interpretation of following |
|
|
1049 | level specifications to enable the specified level only. |
|
|
1050 | |
|
|
1051 | Example: only enable debug messages for a context. |
|
|
1052 | |
|
|
1053 | context=only,debug |
|
|
1054 | |
|
|
1055 | =item C<except> |
|
|
1056 | |
|
|
1057 | Enables all logging levels, and changes the interpretation of following |
|
|
1058 | level specifications to disable that level. Rarely used. |
|
|
1059 | |
|
|
1060 | Example: enable all logging levels except fatal and trace (this is rather |
|
|
1061 | nonsensical). |
|
|
1062 | |
|
|
1063 | filter=exept,fatal,trace |
|
|
1064 | |
|
|
1065 | =item C<level> |
|
|
1066 | |
|
|
1067 | Enables all logging levels, and changes the interpretation of following |
|
|
1068 | level specifications to be "that level or any higher priority |
|
|
1069 | message". This is the default. |
|
|
1070 | |
|
|
1071 | Example: log anything at or above warn level. |
|
|
1072 | |
|
|
1073 | filter=warn |
|
|
1074 | |
|
|
1075 | # or, more verbose |
|
|
1076 | filter=only,level,warn |
|
|
1077 | |
|
|
1078 | =item C<1>..C<9>, a logging level name (C<error>, C<debug> etc.) |
|
|
1079 | |
|
|
1080 | A numeric loglevel or the name of a loglevel will be interpreted according |
|
|
1081 | to the most recent C<only>, C<except> or C<level> directive. By default, |
|
|
1082 | specifying a logging level enables that and any higher priority messages. |
|
|
1083 | |
|
|
1084 | =item C<+>I<context> |
|
|
1085 | |
|
|
1086 | Adds/attaches the named context as slave to the context. |
|
|
1087 | |
|
|
1088 | =item C<+> |
|
|
1089 | |
|
|
1090 | A line C<+> clears the slave list form the context. Anonymous (C<%name>) |
|
|
1091 | contexts have no slaves by default, but package contexts have the parent |
|
|
1092 | context as slave by default. |
|
|
1093 | |
|
|
1094 | Example: log messages from My::Module to a file, do not send them to the |
|
|
1095 | default log collector. |
|
|
1096 | |
|
|
1097 | My::Module=+,file=/tmp/mymodulelog |
|
|
1098 | |
|
|
1099 | =back |
|
|
1100 | |
1115 | |
1101 | =cut |
1116 | =cut |
1102 | |
1117 | |
1103 | for (my $spec = $ENV{PERL_ANYEVENT_LOG}) { |
1118 | for (my $spec = $ENV{PERL_ANYEVENT_LOG}) { |
1104 | my %anon; |
1119 | my %anon; |