… | |
… | |
9 | use AnyEvent; |
9 | use AnyEvent; |
10 | |
10 | |
11 | AE::log trace => "going to call function abc"; |
11 | AE::log trace => "going to call function abc"; |
12 | AE::log debug => "the function returned 3"; |
12 | AE::log debug => "the function returned 3"; |
13 | AE::log info => "file soandso successfully deleted"; |
13 | AE::log info => "file soandso successfully deleted"; |
14 | AE::log note => "wanted to create config, but config was alraedy created"; |
14 | AE::log note => "wanted to create config, but config was already created"; |
15 | AE::log warn => "couldn't delete the file"; |
15 | AE::log warn => "couldn't delete the file"; |
16 | AE::log error => "failed to retrieve data"; |
16 | AE::log error => "failed to retrieve data"; |
17 | AE::log crit => "the battery temperature is too hot"; |
17 | AE::log crit => "the battery temperature is too hot"; |
18 | AE::log alert => "the battery died"; |
18 | AE::log alert => "the battery died"; |
19 | AE::log fatal => "no config found, cannot continue"; # never returns |
19 | AE::log fatal => "no config found, cannot continue"; # never returns |
… | |
… | |
61 | attempt to be "the" logging solution or even "a" logging solution for |
61 | attempt to be "the" logging solution or even "a" logging solution for |
62 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
62 | AnyEvent - AnyEvent simply creates logging messages internally, and this |
63 | module more or less exposes the mechanism, with some extra spiff to allow |
63 | module more or less exposes the mechanism, with some extra spiff to allow |
64 | using it from other modules as well. |
64 | using it from other modules as well. |
65 | |
65 | |
66 | Remember that the default verbosity level is C<0> (C<off>), so nothing |
66 | Remember that the default verbosity level is C<3> (C<critical>), so little |
67 | will be logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number |
67 | will be logged, unless you set C<PERL_ANYEVENT_VERBOSE> to a higher number |
68 | before starting your program, or change the logging level at runtime with |
68 | before starting your program, or change the logging level at runtime with |
69 | something like: |
69 | something like: |
70 | |
70 | |
71 | use AnyEvent::Log; |
71 | use AnyEvent::Log; |
… | |
… | |
106 | is the "official" name, the second one the "syslog" name (if it differs) |
106 | is the "official" name, the second one the "syslog" name (if it differs) |
107 | and the third one the "perl" name, suggesting (only!) that you log C<die> |
107 | and the third one the "perl" name, suggesting (only!) that you log C<die> |
108 | messages at C<error> priority. The NOTE column tries to provide some |
108 | messages at C<error> priority. The NOTE column tries to provide some |
109 | rationale on how to chose a logging level. |
109 | rationale on how to chose a logging level. |
110 | |
110 | |
111 | As a rough guideline, levels 1..3 are primarily meant for users of |
111 | As a rough guideline, levels 1..3 are primarily meant for users of the |
112 | the program (admins, staff), and are the only logged to STDERR by |
112 | program (admins, staff), and are the only ones logged to STDERR by |
113 | default. Levels 4..6 are meant for users and developers alike, while |
113 | default. Levels 4..6 are meant for users and developers alike, while |
114 | levels 7..9 are usually meant for developers. |
114 | levels 7..9 are usually meant for developers. |
115 | |
115 | |
116 | You can normally only log a single message at highest priority level |
116 | You can normally only log a message once at highest priority level (C<1>, |
117 | (C<1>, C<fatal>), because logging a fatal message will also quit the |
117 | C<fatal>), because logging a fatal message will also quit the program - so |
118 | program - so use it sparingly :) |
118 | use it sparingly :) |
119 | |
119 | |
120 | Some methods also offer some extra levels, such as C<0>, C<off>, C<none> |
120 | Some methods also offer some extra levels, such as C<0>, C<off>, C<none> |
121 | or C<all> - these are only valid in the methods they are documented for. |
121 | or C<all> - these are only valid for the methods that documented them. |
122 | |
122 | |
123 | =head1 LOGGING FUNCTIONS |
123 | =head1 LOGGING FUNCTIONS |
124 | |
124 | |
125 | These functions allow you to log messages. They always use the caller's |
125 | The following functions allow you to log messages. They always use the |
126 | package as a "logging context". Also, the main logging function C<log> is |
126 | caller's package as a "logging context". Also, the main logging function, |
127 | callable as C<AnyEvent::log> or C<AE::log> when the C<AnyEvent> module is |
127 | C<log>, is aliased to C<AnyEvent::log> and C<AE::log> when the C<AnyEvent> |
128 | loaded. |
128 | module is loaded. |
129 | |
129 | |
130 | =over 4 |
130 | =over 4 |
131 | |
131 | |
132 | =cut |
132 | =cut |
133 | |
133 | |
… | |
… | |
191 | |
191 | |
192 | Last not least, C<$msg> might be a code reference, in which case it is |
192 | Last not least, C<$msg> might be a code reference, in which case it is |
193 | supposed to return the message. It will be called only then the message |
193 | supposed to return the message. It will be called only then the message |
194 | actually gets logged, which is useful if it is costly to create the |
194 | actually gets logged, which is useful if it is costly to create the |
195 | message in the first place. |
195 | message in the first place. |
|
|
196 | |
|
|
197 | This function takes care of saving and restoring C<$!> and C<$@>, so you |
|
|
198 | don't have to. |
196 | |
199 | |
197 | Whether the given message will be logged depends on the maximum log level |
200 | Whether the given message will be logged depends on the maximum log level |
198 | and the caller's package. The return value can be used to ensure that |
201 | and the caller's package. The return value can be used to ensure that |
199 | messages or not "lost" - for example, when L<AnyEvent::Debug> detects a |
202 | messages or not "lost" - for example, when L<AnyEvent::Debug> detects a |
200 | runtime error it tries to log it at C<die> level, but if that message is |
203 | runtime error it tries to log it at C<die> level, but if that message is |
… | |
… | |
301 | |
304 | |
302 | # log if log cb |
305 | # log if log cb |
303 | if ($ctx->[3]) { |
306 | if ($ctx->[3]) { |
304 | # logging target found |
307 | # logging target found |
305 | |
308 | |
|
|
309 | local ($!, $@); |
|
|
310 | |
306 | # now get raw message, unless we have it already |
311 | # now get raw message, unless we have it already |
307 | unless ($now) { |
312 | unless ($now) { |
308 | $format = $format->() if ref $format; |
313 | $format = $format->() if ref $format; |
309 | $format = sprintf $format, @args if @args; |
314 | $format = sprintf $format, @args if @args; |
310 | $format =~ s/\n$//; |
315 | $format =~ s/\n$//; |
… | |
… | |
894 | logging context, the (numeric) logging level and the raw message string |
899 | logging context, the (numeric) logging level and the raw message string |
895 | and needs to return a formatted log message. In most cases this will be a |
900 | and needs to return a formatted log message. In most cases this will be a |
896 | string, but it could just as well be an array reference that just stores |
901 | string, but it could just as well be an array reference that just stores |
897 | the values. |
902 | the values. |
898 | |
903 | |
899 | If, for some reason, you want to use C<caller> to find out more baout the |
904 | If, for some reason, you want to use C<caller> to find out more about the |
900 | logger then you should walk up the call stack until you are no longer |
905 | logger then you should walk up the call stack until you are no longer |
901 | inside the C<AnyEvent::Log> package. |
906 | inside the C<AnyEvent::Log> package. |
902 | |
907 | |
903 | Example: format just the raw message, with numeric log level in angle |
908 | Example: format just the raw message, with numeric log level in angle |
904 | brackets. |
909 | brackets. |
… | |
… | |
1192 | |
1197 | |
1193 | Attaches the named context as slave to the context. |
1198 | Attaches the named context as slave to the context. |
1194 | |
1199 | |
1195 | =item C<+> |
1200 | =item C<+> |
1196 | |
1201 | |
1197 | A line C<+> detaches all contexts, i.e. clears the slave list from the |
1202 | A lone C<+> detaches all contexts, i.e. clears the slave list from the |
1198 | context. Anonymous (C<%name>) contexts have no attached slaves by default, |
1203 | context. Anonymous (C<%name>) contexts have no attached slaves by default, |
1199 | but package contexts have the parent context as slave by default. |
1204 | but package contexts have the parent context as slave by default. |
1200 | |
1205 | |
1201 | Example: log messages from My::Module to a file, do not send them to the |
1206 | Example: log messages from My::Module to a file, do not send them to the |
1202 | default log collector. |
1207 | default log collector. |