1Log::Dispatchouli(3) User Contributed Perl Documentation Log::Dispatchouli(3)
2
3
4
6 Log::Dispatchouli - a simple wrapper around Log::Dispatch
7
9 version 1.102350
10
12 my $logger = Log::Dispatchouli->new({
13 ident => 'stuff-purger',
14 facility => 'daemon',
15 to_stdout => $opt->{print},
16 debug => $opt->{verbose}
17 })
18
19 $logger->log([ "There are %s items left to purge...", $stuff_left ]);
20
21 $logger->log_debug("this is extra often-ignored debugging log");
22
23 $logger->log_fatal("Now we will die!!");
24
26 Log::Dispatchouli is a thin layer above Log::Dispatch and meant to make
27 it dead simple to add logging to a program without having to think much
28 about categories, facilities, levels, or things like that. It is meant
29 to make logging just configurable enough that you can find the logs you
30 want and just easy enough that you will actually log things.
31
32 Log::Dispatchouli can log to syslog (if you specify a facility),
33 standard error or standard output, to a file, or to an array in memory.
34 That last one is mostly useful for testing.
35
36 In addition to providing as simple a way to get a handle for logging
37 operations, Log::Dispatchouli uses String::Flogger to process the
38 things to be logged, meaning you can easily log data structures.
39 Basically: strings are logged as is, arrayrefs are taken as (sprintf
40 format, args), and subroutines are called only if needed. For more
41 information read the String::Flogger docs.
42
44 new
45 my $logger = Log::Dispatchouli->new(\%arg);
46
47 This returns a new logger, a Log::Dispatchouli object.
48
49 Valid arguments are:
50
51 ident - the name of the thing logging (mandatory)
52 to_self - log to the logger object for testing; default: false
53 to_file - log to PROGRAM_NAME.YYYYMMDD in the log path; default: false
54 to_stdout - log to STDOUT; default: false
55 to_stderr - log to STDERR; default: false
56 facility - to which syslog facility to send logs; default: none
57 log_pid - if true, prefix all log entries with the pid; default: true
58 fail_fatal - a boolean; if true, failure to log is fatal; default: true
59 muted - a boolean; if true, only fatals are logged; default: false
60 debug - a boolean; if true, log_debug method is not a no-op
61 defaults to the truth of the DISPATCHOULI_DEBUG env var
62 quiet_fatal - 'stderr' or 'stdout' or an arrayref of zero, one, or both
63 fatal log messages will not be logged to these
64 (default: stderr)
65
66 The log path is either /tmp or the value of the DISPATCHOULI_PATH env
67 var.
68
69 If the DISPATCHOULI_NOSYSLOG env var is true, we don't log to syslog.
70
71 log
72 $logger->log(@messages);
73
74 $logger->log(\%arg, @messages);
75
76 This method uses String::Flogger on the input, then logs the result.
77 Each message is flogged individually, then joined with spaces.
78
79 If the first argument is a hashref, it will be used as extra arguments
80 to logging. It may include a "prefix" entry to preprocess the message
81 by prepending a string (if the prefix is a string) or calling a
82 subroutine to generate a new message (if the prefix is a coderef).
83
84 log_fatal
85 This behaves like the "log" method, but will throw the logged string as
86 an exception after logging.
87
88 This method can also be called as "fatal", to match other popular
89 logging interfaces. If you want to override this method, you must
90 override "log_fatal" and not "fatal".
91
92 log_debug
93 This behaves like the "log" method, but will only log (at the debug
94 level) if the logger object has its debug property set to true.
95
96 This method can also be called as "debug", to match other popular
97 logging interfaces. If you want to override this method, you must
98 override "log_debug" and not "debug".
99
100 set_debug
101 $logger->set_debug($bool);
102
103 This sets the logger's debug property, which affects the behavior of
104 "log_debug".
105
106 get_debug
107 This gets the logger's debug property, which affects the behavior of
108 "log_debug".
109
110 clear_debug
111 This method does nothing, and is only useful for
112 Log::Dispatchouli::Proxy objects. See Methods for Proxy Loggers,
113 below.
114
115 set_muted
116 $logger->set_muted($bool);
117
118 This sets the logger's muted property, which affects the behavior of
119 "log".
120
121 get_muted
122 This gets the logger's muted property, which affects the behavior of
123 "log".
124
125 clear_muted
126 This method does nothing, and is only useful for
127 Log::Dispatchouli::Proxy objects. See Methods for Proxy Loggers,
128 below.
129
130 get_prefix
131 my $prefix = $logger->get_prefix;
132
133 This method returns the currently-set prefix for the logger, which may
134 be a string or code reference or undef. See Logger Prefix.
135
136 set_prefix
137 $logger->set_prefix( $new_prefix );
138
139 This method changes the prefix. See Logger Prefix.
140
141 clear_prefix
142 This method clears any set logger prefix. (It can also be called as
143 "unset_prefix", but this is deprecated. See Logger Prefix.
144
145 dispatcher
146 This returns the underlying Log::Dispatch object. This is not the
147 method you're looking for. Move along.
148
150 Log messages may be prepended with information to set context. This
151 can be set at a logger level or per log item. The simplest example is:
152
153 my $logger = Log::Dispatchouli->new( ... );
154
155 $logger->set_prefix("Batch 123: ");
156
157 $logger->log("begun processing");
158
159 # ...
160
161 $logger->log("finished processing");
162
163 The above will log something like:
164
165 Batch 123: begun processing
166 Batch 123: finished processing
167
168 To pass a prefix per-message:
169
170 $logger->log({ prefix => 'Sub-Item 234: ', 'error!' })
171
172 # Logs: Batch 123: Sub-Item 234: error!
173
174 If the prefix is a string, it is prepended to each line of the message.
175 If it is a coderef, it is called and passed the message to be logged.
176 The return value is logged instead.
177
178 Proxy loggers also have their own prefix settings, which accumulate.
179 So:
180
181 my $proxy = $logger->proxy({ proxy_prefix => 'Subsystem 12: ' });
182
183 $proxy->set_prefix('Page 9: ');
184
185 $proxy->log({ prefix => 'Paragraph 6: ' }, 'Done.');
186
187 ...will log...
188
189 Batch 123: Subsystem 12: Page 9: Paragraph 6: Done.
190
192 new_tester
193 my $logger = Log::Dispatchouli->new_tester( \%arg );
194
195 This returns a new logger that logs only "to_self". It's useful in
196 testing. If no "ident" arg is provided, one will be generated.
197 "log_pid" is off by default, but can be overridden.
198
199 "\%arg" is optional.
200
201 events
202 This method returns the arrayref of events logged to an array in memory
203 (in the logger). If the logger is not logging "to_self" this raises an
204 exception.
205
206 clear_events
207 This method empties the current sequence of events logged into an array
208 in memory. If the logger is not logging "to_self" this raises an
209 exception.
210
212 proxy
213 my $proxy_logger = $logger->proxy( \%arg );
214
215 This method returns a new proxy logger -- an instance of
216 Log::Dispatchouli::Proxy -- which will log through the given logger,
217 but which may have some settings localized.
218
219 %arg is optional. It may contain the following entries:
220
221 proxy_prefix
222 This is a prefix that will be applied to anything the proxy logger
223 logs, and cannot be changed.
224
225 debug
226 This can be set to true or false to change the proxy's "am I in
227 debug mode?" setting. It can be changed or cleared later on the
228 proxy.
229
230 parent
231 logger
232 These methods return the logger itself. (They're more useful when
233 called on proxy loggers.)
234
236 To provide compatibility with some other loggers, most specifically
237 Log::Contextual, the following methods are provided. You should not
238 use these methods without a good reason, and you should never subclass
239 them. Instead, subclass the methods they call.
240
241 is_debug
242 This method calls "get_debug".
243
244 is_info
245 is_fatal
246 These methods return true.
247
248 info
249 fatal
250 debug
251 These methods redispatch to "log", "log_fatal", and "log_debug"
252 respectively.
253
255 · Log::Dispatch
256
257 · String::Flogger
258
260 Ricardo SIGNES <rjbs@cpan.org>
261
263 This software is copyright (c) 2010 by Ricardo SIGNES.
264
265 This is free software; you can redistribute it and/or modify it under
266 the same terms as the Perl 5 programming language system itself.
267
268
269
270perl v5.12.2 2010-08-23 Log::Dispatchouli(3)