1Log::Dispatch(3) User Contributed Perl Documentation Log::Dispatch(3)
2
3
4
6 Log::Dispatch - Dispatches messages to one or more outputs
7
9 version 2.71
10
12 use Log::Dispatch;
13
14 # Simple API
15 #
16 my $log = Log::Dispatch->new(
17 outputs => [
18 [ 'File', min_level => 'debug', filename => 'logfile' ],
19 [ 'Screen', min_level => 'warning' ],
20 ],
21 );
22
23 $log->info('Blah, blah');
24
25 # More verbose API
26 #
27 my $log = Log::Dispatch->new();
28 $log->add(
29 Log::Dispatch::File->new(
30 name => 'file1',
31 min_level => 'debug',
32 filename => 'logfile'
33 )
34 );
35 $log->add(
36 Log::Dispatch::Screen->new(
37 name => 'screen',
38 min_level => 'warning',
39 )
40 );
41
42 $log->log( level => 'info', message => 'Blah, blah' );
43
44 my $sub = sub { my %p = @_; return reverse $p{message}; };
45 my $reversing_dispatcher = Log::Dispatch->new( callbacks => $sub );
46
48 This module manages a set of Log::Dispatch::* output objects that can
49 be logged to via a unified interface.
50
51 The idea is that you create a Log::Dispatch object and then add various
52 logging objects to it (such as a file logger or screen logger). Then
53 you call the "log" method of the dispatch object, which passes the
54 message to each of the objects, which in turn decide whether or not to
55 accept the message and what to do with it.
56
57 This makes it possible to call single method and send a message to a
58 log file, via email, to the screen, and anywhere else, all with very
59 little code needed on your part, once the dispatching object has been
60 created.
61
63 This class provides the following methods:
64
65 Log::Dispatch->new(...)
66 This method takes the following parameters:
67
68 • outputs( [ [ class, params, ... ], [ class, params, ... ], ... ] )
69
70 This parameter is a reference to a list of lists. Each inner list
71 consists of a class name and a set of constructor params. The class
72 is automatically prefixed with 'Log::Dispatch::' unless it begins
73 with '+', in which case the string following '+' is taken to be a
74 full classname. e.g.
75
76 outputs => [ [ 'File', min_level => 'debug', filename => 'logfile' ],
77 [ '+My::Dispatch', min_level => 'info' ] ]
78
79 For each inner list, a new output object is created and added to
80 the dispatcher (via the add() method).
81
82 See "OUTPUT CLASSES" for the parameters that can be used when
83 creating an output object.
84
85 • callbacks( \& or [ \&, \&, ... ] )
86
87 This parameter may be a single subroutine reference or an array
88 reference of subroutine references. These callbacks will be called
89 in the order they are given and passed a hash containing the
90 following keys:
91
92 ( message => $log_message, level => $log_level )
93
94 In addition, any key/value pairs passed to a logging method will be
95 passed onto your callback.
96
97 The callbacks are expected to modify the message and then return a
98 single scalar containing that modified message. These callbacks
99 will be called when either the "log" or "log_to" methods are called
100 and will only be applied to a given message once. If they do not
101 return the message then you will get no output. Make sure to return
102 the message!
103
104 $dispatch->clone()
105 This returns a shallow clone of the original object. The underlying
106 output objects and callbacks are shared between the two objects.
107 However any changes made to the outputs or callbacks that the object
108 contains are not shared.
109
110 $dispatch->log( level => $, message => $ or \& )
111 Sends the message (at the appropriate level) to all the output objects
112 that the dispatcher contains (by calling the "log_to" method
113 repeatedly).
114
115 The level can be specified by name or by an integer from 0 (debug) to 7
116 (emergency).
117
118 This method also accepts a subroutine reference as the message
119 argument. This reference will be called only if there is an output that
120 will accept a message of the specified level.
121
122 $dispatch->debug (message), info (message), ...
123 You may call any valid log level (including valid abbreviations) as a
124 method with a single argument that is the message to be logged. This is
125 converted into a call to the "log" method with the appropriate level.
126
127 For example:
128
129 $log->alert('Strange data in incoming request');
130
131 translates to:
132
133 $log->log( level => 'alert', message => 'Strange data in incoming request' );
134
135 If you pass an array to these methods, it will be stringified as is:
136
137 my @array = ('Something', 'bad', 'is', 'here');
138 $log->alert(@array);
139
140 # is equivalent to
141
142 $log->alert("@array");
143
144 You can also pass a subroutine reference, just like passing one to the
145 log() method.
146
147 $dispatch->log_and_die( level => $, message => $ or \& )
148 Has the same behavior as calling log() but calls _die_with_message() at
149 the end.
150
151 You can throw exception objects by subclassing this method.
152
153 If the "carp_level" parameter is present its value will be added to the
154 current value of $Carp::CarpLevel.
155
156 $dispatch->log_and_croak( level => $, message => $ or \& )
157 A synonym for "$dispatch-"log_and_die()>.
158
159 $dispatch->log_to( name => $, level => $, message => $ )
160 Sends the message only to the named object. Note: this will not
161 properly handle a subroutine reference as the message.
162
163 $dispatch->add_callback( $code )
164 Adds a callback (like those given during construction). It is added to
165 the end of the list of callbacks. Note that this can also be called on
166 individual output objects.
167
168 $dispatch->remove_callback( $code )
169 Remove the given callback from the list of callbacks. Note that this
170 can also be called on individual output objects.
171
172 $dispatch->callbacks()
173 Returns a list of the callbacks in a given output.
174
175 $dispatch->level_is_valid( $string )
176 Returns true or false to indicate whether or not the given string is a
177 valid log level. Can be called as either a class or object method.
178
179 $dispatch->would_log( $string )
180 Given a log level, returns true or false to indicate whether or not
181 anything would be logged for that log level.
182
183 $dispatch->is_$level
184 There are methods for every log level: is_debug(), is_warning(), etc.
185
186 This returns true if the logger will log a message at the given level.
187
188 $dispatch->add( Log::Dispatch::* OBJECT )
189 Adds a new output object to the dispatcher. If an object of the same
190 name already exists, then that object is replaced, with a warning if
191 $^W is true.
192
193 $dispatch->remove($)
194 Removes the output object that matches the name given to the remove
195 method. The return value is the object being removed or undef if no
196 object matched this.
197
198 $dispatch->outputs()
199 Returns a list of output objects.
200
201 $dispatch->output( $name )
202 Returns the output object of the given name. Returns undef or an empty
203 list, depending on context, if the given output does not exist.
204
205 $dispatch->_die_with_message( message => $, carp_level => $ )
206 This method is used by "log_and_die" and will either die() or croak()
207 depending on the value of "message": if it's a reference or it ends
208 with a new line then a plain die will be used, otherwise it will croak.
209
211 An output class - e.g. Log::Dispatch::File or Log::Dispatch::Screen -
212 implements a particular way of dispatching logs. Many output classes
213 come with this distribution, and others are available separately on
214 CPAN.
215
216 The following common parameters can be used when creating an output
217 class. All are optional. Most output classes will have additional
218 parameters beyond these, see their documentation for details.
219
220 • name ($)
221
222 A name for the object (not the filename!). This is useful if you
223 want to refer to the object later, e.g. to log specifically to it
224 or remove it.
225
226 By default a unique name will be generated. You should not depend
227 on the form of generated names, as they may change.
228
229 • min_level ($)
230
231 The minimum logging level this object will accept. Required.
232
233 • max_level ($)
234
235 The maximum logging level this object will accept. By default the
236 maximum is the highest possible level (which means functionally
237 that the object has no maximum).
238
239 • callbacks( \& or [ \&, \&, ... ] )
240
241 This parameter may be a single subroutine reference or an array
242 reference of subroutine references. These callbacks will be called
243 in the order they are given and passed a hash containing the
244 following keys:
245
246 ( message => $log_message, level => $log_level )
247
248 The callbacks are expected to modify the message and then return a
249 single scalar containing that modified message. These callbacks
250 will be called when either the "log" or "log_to" methods are called
251 and will only be applied to a given message once. If they do not
252 return the message then you will get no output. Make sure to return
253 the message!
254
255 • newline (0|1)
256
257 If true, a callback will be added to the end of the callbacks list
258 that adds a newline to the end of each message. Default is false,
259 but some output classes may decide to make the default true.
260
262 The log levels that Log::Dispatch uses are taken directly from the
263 syslog man pages (except that I expanded them to full words). Valid
264 levels are:
265
266 debug
267 info
268 notice
269 warning
270 error
271 critical
272 alert
273 emergency
274
275 Alternately, the numbers 0 through 7 may be used (debug is 0 and
276 emergency is 7). The syslog standard of 'err', 'crit', and 'emerg' is
277 also acceptable. We also allow 'warn' as a synonym for 'warning'.
278
280 This module was designed to be easy to subclass. If you want to handle
281 messaging in a way not implemented in this package, you should be able
282 to add this with minimal effort. It is generally as simple as
283 subclassing Log::Dispatch::Output and overriding the "new" and
284 "log_message" methods. See the Log::Dispatch::Output docs for more
285 details.
286
287 If you would like to create your own subclass for sending email then it
288 is even simpler. Simply subclass Log::Dispatch::Email and override the
289 "send_email" method. See the Log::Dispatch::Email docs for more
290 details.
291
292 The logging levels that Log::Dispatch uses are borrowed from the
293 standard UNIX syslog levels, except that where syslog uses partial
294 words ("err") Log::Dispatch also allows the use of the full word as
295 well ("error").
296
298 Log::Dispatch::DBI
299 Written by Tatsuhiko Miyagawa. Log output to a database table.
300
301 Log::Dispatch::FileRotate
302 Written by Mark Pfeiffer. Rotates log files periodically as part of its
303 usage.
304
305 Log::Dispatch::File::Stamped
306 Written by Eric Cholet. Stamps log files with date and time
307 information.
308
309 Log::Dispatch::Jabber
310 Written by Aaron Straup Cope. Logs messages via Jabber.
311
312 Log::Dispatch::Tk
313 Written by Dominique Dumont. Logs messages to a Tk window.
314
315 Log::Dispatch::Win32EventLog
316 Written by Arthur Bergman. Logs messages to the Windows event log.
317
318 Log::Log4perl
319 An implementation of Java's log4j API in Perl. Log messages can be
320 limited by fine-grained controls, and if they end up being logged, both
321 native Log4perl and Log::Dispatch appenders can be used to perform the
322 actual logging job. Created by Mike Schilli and Kevin Goess.
323
324 Log::Dispatch::Config
325 Written by Tatsuhiko Miyagawa. Allows configuration of logging via a
326 text file similar (or so I'm told) to how it is done with log4j.
327 Simpler than Log::Log4perl.
328
329 Log::Agent
330 A very different API for doing many of the same things that
331 Log::Dispatch does. Originally written by Raphael Manfredi.
332
334 Log::Dispatch::ApacheLog, Log::Dispatch::Email,
335 Log::Dispatch::Email::MailSend, Log::Dispatch::Email::MailSender,
336 Log::Dispatch::Email::MailSendmail, Log::Dispatch::Email::MIMELite,
337 Log::Dispatch::File, Log::Dispatch::File::Locked,
338 Log::Dispatch::Handle, Log::Dispatch::Output, Log::Dispatch::Screen,
339 Log::Dispatch::Syslog
340
342 Bugs may be submitted at
343 <https://github.com/houseabsolute/Log-Dispatch/issues>.
344
346 The source code repository for Log-Dispatch can be found at
347 <https://github.com/houseabsolute/Log-Dispatch>.
348
350 If you'd like to thank me for the work I've done on this module, please
351 consider making a "donation" to me via PayPal. I spend a lot of free
352 time creating free software, and would appreciate any support you'd
353 care to offer.
354
355 Please note that I am not suggesting that you must do this in order for
356 me to continue working on this particular software. I will continue to
357 do so, inasmuch as I have in the past, for as long as it interests me.
358
359 Similarly, a donation made in this way will probably not make me work
360 on this software much more, unless I get so many donations that I can
361 consider working on free software full time (let's all have a chuckle
362 at that together).
363
364 To donate, log into PayPal and send money to autarch@urth.org, or use
365 the button at <https://houseabsolute.com/foss-donations/>.
366
368 Dave Rolsky <autarch@urth.org>
369
371 • Anirvan Chatterjee <anirvan@users.noreply.github.com>
372
373 • Carsten Grohmann <mail@carstengrohmann.de>
374
375 • Doug Bell <doug@preaction.me>
376
377 • Graham Knop <haarg@haarg.org>
378
379 • Graham Ollis <plicease@cpan.org>
380
381 • Gregory Oschwald <goschwald@maxmind.com>
382
383 • hartzell <hartzell@alerce.com>
384
385 • Joelle Maslak <jmaslak@antelope.net>
386
387 • Johann Rolschewski <jorol@cpan.org>
388
389 • Jonathan Swartz <swartz@pobox.com>
390
391 • Karen Etheridge <ether@cpan.org>
392
393 • Kerin Millar <kfm@plushkava.net>
394
395 • Kivanc Yazan <kivancyazan@gmail.com>
396
397 • Konrad Bucheli <kb@open.ch>
398
399 • Michael Schout <mschout@gkg.net>
400
401 • Olaf Alders <olaf@wundersolutions.com>
402
403 • Olivier Mengué <dolmen@cpan.org>
404
405 • Rohan Carly <se456@rohan.id.au>
406
407 • Ross Attrill <ross.attrill@gmail.com>
408
409 • Salvador Fandiño <sfandino@yahoo.com>
410
411 • Sergey Leschenko <sergle.ua@gmail.com>
412
413 • Slaven Rezic <srezic@cpan.org>
414
415 • Steve Bertrand <steveb@cpan.org>
416
417 • Whitney Jackson <whitney.jackson@baml.com>
418
420 This software is Copyright (c) 2023 by Dave Rolsky.
421
422 This is free software, licensed under:
423
424 The Artistic License 2.0 (GPL Compatible)
425
426 The full text of the license can be found in the LICENSE file included
427 with this distribution.
428
429
430
431perl v5.38.0 2023-07-20 Log::Dispatch(3)