• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

lib/Log/H20-Jul-2020-3,6611,202

t/H20-Jul-2020-2,6252,135

xt/H20-Jul-2020-460392

CODE_OF_CONDUCT.mdH A D20-Jul-20203.2 KiB7656

CONTRIBUTING.mdH A D20-Jul-20203.6 KiB10570

ChangesH A D20-Jul-202023.9 KiB826480

INSTALLH A D20-Jul-20202.2 KiB7346

LICENSEH A D20-Jul-20208.8 KiB208154

MANIFESTH A D20-Jul-20201.4 KiB6867

META.jsonH A D20-Jul-202040.4 KiB1,2061,204

META.ymlH A D20-Jul-202026 KiB904903

Makefile.PLH A D20-Jul-20203.8 KiB154137

README.mdH A D20-Jul-202015.1 KiB435294

azure-pipelines.ymlH A D20-Jul-2020741 3226

cpanfileH A D20-Jul-20202.8 KiB9083

dist.iniH A D20-Jul-20201 KiB4135

perlcriticrcH A D20-Jul-20201.8 KiB6847

perltidyrcH A D20-Jul-2020327 2423

tidyall.iniH A D20-Jul-2020598 2923

weaver.iniH A D20-Jul-2020166 1811

README.md

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