1CGI::Application::PlugiUns:e:rLoCgoDnitsrpiabtucthe(d3C)PGeIr:l:ADpopcluimceanttiaotni:o:nPlugin::LogDispatch(3)
2
3
4

NAME

6       CGI::Application::Plugin::LogDispatch - Add Log::Dispatch support to
7       CGI::Application
8

SYNOPSIS

10        package My::App;
11
12        use CGI::Application::Plugin::LogDispatch;
13
14        sub cgiapp_init {
15          my $self = shift;
16
17          # calling log_config is optional as
18          # some simple defaults will be used
19          $self->log_config(
20            LOG_DISPATCH_MODULES => [
21              {    module => 'Log::Dispatch::File',
22                     name => 'debug',
23                 filename => '/tmp/debug.log',
24                min_level => 'debug',
25              },
26            ]
27          );
28        }
29
30        sub myrunmode {
31          my $self = shift;
32
33          $self->log->info('Information message');
34          $self->log->debug('Debug message');
35        }
36
37        - or as a class based singleton -
38
39        package My::App;
40
41        use CGI::Application::Plugin::LogDispatch (
42          LOG_DISPATCH_MODULES => [
43            {    module => 'Log::Dispatch::File',
44                   name => 'debug',
45               filename => '/tmp/debug.log',
46              min_level => 'debug',
47            },
48          ]
49        );
50
51        My::App->log->info('Information message');
52
53        sub myrunmode {
54          my $self = shift;
55
56          $self->log->info('This also works');
57        }
58

DESCRIPTION

60       CGI::Application::Plugin::LogDispatch adds logging support to your
61       CGI::Application modules by providing a Log::Dispatch dispatcher object
62       that is accessible from anywhere in the application.
63
64       If you have CGI::Application::Plugin::DevPopup installed, a "Log
65       Entries" report is added to the popup window, containing all of the
66       entries that were logged during the execution of the runmode.
67

METHODS

69   log
70       This method will return the current Log::Dispatch dispatcher object.
71       The Log::Dispatch object is created on the first call to this method,
72       and any subsequent calls will return the same object.  This effectively
73       creates a singleton log dispatcher for the duration of the request.  If
74       "log_config" has not been called before the first call to "log", then
75       it will choose some sane defaults to create the dispatcher object (the
76       exact default values are defined below).
77
78         # retrieve the log object
79         my $log = $self->log;
80         $log->warning("something's not right!");
81         $log->emergency("It's all gone pear shaped!");
82
83         - or -
84
85         # use the log object directly
86         $self->log->debug(Data::Dumper::Dumper(\%hash));
87
88         - or -
89
90         # if you configured it as a singleton
91         My::App->log->debug('This works too');
92
93   log_config
94       This method can be used to customize the functionality of the
95       CGI::Application::Plugin::LogDispatch module.  Calling this method does
96       not mean that a new Log::Dispatch object will be immediately created.
97       The log object will not be created until the first call to $self->log.
98
99       The recommended place to call "log_config" is in the "cgiapp_init"
100       stage of CGI::Application.  If this method is called after the log
101       object has already been accessed, then it will die with an error
102       message.
103
104       If this method is not called at all then a reasonable set of defaults
105       will be used (the exact default values are defined below).
106
107       The following parameters are accepted:
108
109       LOG_DISPATCH_OPTIONS
110           This allows you to customize how the Log::Dispatch object is
111           created by providing a hash of options that will be passed to the
112           Log::Dispatch constructor.  Please see the documentation for
113           Log::Dispatch for the exact syntax of the parameters.  Surprisingly
114           enough you will usually not need to use this option, instead look
115           at the LOG_DISPATCH_MODULES option.
116
117            LOG_DISPATCH_OPTIONS => {
118                 callbacks => sub { my %h = @_; return time().': '.$h{message}; },
119            }
120
121       LOG_DISPATCH_MODULES
122           This option allows you to specify the Log::Dispatch::* modules that
123           you wish to use to log messages.  You can list multiple dispatch
124           modules, each with their own set of options.  Format the options in
125           an array of hashes, where each hash contains the options for the
126           Log::Dispatch:: module you are configuring and also include a
127           'module' parameter containing the name of the dispatch module.  See
128           below for an example.  You can also add an 'append_newline' option
129           to automatically append a newline to each log entry for this
130           dispatch module (this option is not needed if you already specified
131           the APPEND_NEWLINE option listed below which will add a newline for
132           all dispatch modules).
133
134            LOG_DISPATCH_MODULES => [
135              {         module => 'Log::Dispatch::File',
136                          name => 'messages',
137                      filename => '/tmp/messages.log',
138                     min_level => 'info',
139                append_newline => 1
140              },
141              {         module => 'Log::Dispatch::Email::MailSend',
142                          name => 'email',
143                            to => [ qw(foo@bar.com bar@baz.org ) ],
144                        subject => 'Oh No!!!!!!!!!!',
145                     min_level => 'emerg'
146              }
147            ]
148
149       APPEND_NEWLINE
150           By default Log::Dispatch does not append a newline to the end of
151           the log messages.  By setting this option to a true value, a
152           newline character will automatically be added to the end of the log
153           message.
154
155            APPEND_NEWLINE => 1
156
157       LOG_METHOD_EXECUTION (EXPERIMENTAL)
158           This option will allow you to log the execution path of your
159           program.  Set LOG_METHOD_EXECUTION to a list of all the modules you
160           want to be logged.  This will automatically send a debug message at
161           the start and end of each method/function that is called in the
162           modules you listed.  The parameters passed, and the return value
163           will also be logged.  This can be useful by tracing the program
164           flow in the logfile without having to resort to the debugger.
165
166            LOG_METHOD_EXECUTION => [qw(__PACKAGE__ CGI::Application CGI)],
167
168           WARNING:  This hasn't been heavily tested, although it seems to
169           work fine for me.  Also, a closure is created around the log
170           object, so some care may need to be taken when using this in a
171           persistent environment like mod_perl.  This feature depends on the
172           Sub::WrapPackages module.
173
174   DEFAULT OPTIONS
175       The following example shows what options are set by default (ie this is
176       what you would get if you do not call log_config).  A single
177       Log::Dispatch::Screen module that writes error messages to STDERR with
178       a minimum log level of debug.
179
180        $self->log_config(
181          LOG_DISPATCH_MODULES => [
182            {        module => 'Log::Dispatch::Screen',
183                       name => 'screen',
184                     stderr => 1,
185                  min_level => 'debug',
186             append_newline => 1
187            }
188          ],
189        );
190
191       Here is a more customized example that uses two file appenders, and an
192       email gateway.  Here all debug messages are sent to /tmp/debug.log, and
193       all messages above are sent to /tmp/messages.log.  Also, any emergency
194       messages are emailed to foo@bar.com and bar@baz.org.
195
196        $self->log_config(
197          LOG_DISPATCH_MODULES => [
198            {    module => 'Log::Dispatch::File',
199                   name => 'debug',
200               filename => '/tmp/debug.log',
201              min_level => 'debug',
202              max_level => 'debug'
203            },
204            {    module => 'Log::Dispatch::File',
205                   name => 'messages',
206               filename => '/tmp/messages.log',
207              min_level => 'info'
208            },
209            {    module => 'Log::Dispatch::Email::MailSend',
210                   name => 'email',
211                     to => [ qw(foo@bar.com bar@baz.org ) ],
212                 subject => 'Oh No!!!!!!!!!!',
213              min_level => 'emerg'
214            }
215          ],
216          APPEND_NEWLINE => 1,
217        );
218

EXAMPLE

220       In a CGI::Application module:
221
222         # configure the log modules once during the init stage
223         sub cgiapp_init {
224           my $self = shift;
225
226           # Configure the session
227           $self->log_config(
228             LOG_DISPATCH_MODULES => [
229               {    module => 'Log::Dispatch::File',
230                      name => 'messages',
231                  filename => '/tmp/messages.log',
232                 min_level => 'error'
233               },
234               {    module => 'Log::Dispatch::Email::MailSend',
235                      name => 'email',
236                        to => [ qw(foo@bar.com bar@baz.org ) ],
237                    subject => 'Oh No!!!!!!!!!!',
238                 min_level => 'emerg'
239               }
240             ],
241             APPEND_NEWLINE => 1,
242           );
243
244         }
245
246         sub cgiapp_prerun {
247           my $self = shift;
248
249           $self->log->debug("Current runmode:  ".$self->get_current_runmode);
250         }
251
252         sub my_runmode {
253           my $self = shift;
254           my $log  = $self->log;
255
256           if ($ENV{'REMOTE_USER'}) {
257             $log->info("user ".$ENV{'REMOTE_USER'});
258           }
259
260           # etc...
261         }
262

SINGLETON SUPPORT

264       This module can be used as a singleton object.  This means that when
265       the object is created, it will remain accessable for the duration of
266       the process.  This can be useful in persistent environments like
267       mod_perl and PersistentPerl, since the object only has to be created
268       one time, and will remain in memory across multiple requests.  It can
269       also be useful if you want to setup a DIE handler, or WARN handler,
270       since you will not have access to the $self object.
271
272       To use this module as a singleton you need to provide all configuration
273       parameters as options to the use statement.  The use statement will
274       accept all the same parameters that the log_config method accepts, so
275       see the documentation above for more details.
276
277       When creating the singleton, the log object will be saved in the
278       namespace of the module that created it.  The singleton will also be
279       inherited by any subclasses of this module.
280
281       NOTE:  Singleton support requires the Class::ISA module which is not
282       installed automatically by this module.
283

SINGLETON EXAMPLE

285         package My::App;
286
287         use base qw(CGI::Application);
288         use CGI::Application::Plugin::LogDispatch(
289             LOG_DISPATCH_MODULES => [
290               {    module => 'Log::Dispatch::File',
291                      name => 'messages',
292                  filename => '/tmp/messages.log',
293                 min_level => 'error'
294               },
295             ],
296             APPEND_NEWLINE => 1,
297           );
298
299         }
300
301         sub cgiapp_prerun {
302           my $self = shift;
303
304           $self->log->debug("Current runmode:  ".$self->get_current_runmode);
305         }
306
307         sub my_runmode {
308           my $self = shift;
309           my $log  = $self->log;
310
311           if ($ENV{'REMOTE_USER'}) {
312             $log->info("user ".$ENV{'REMOTE_USER'});
313           }
314
315           # etc...
316         }
317
318         package My::App::Subclass;
319
320         use base qw(My::App);
321
322         # Setup a die handler that uses the logger
323         $SIG{__DIE__} = sub { My::App::Subclass->log->emerg(@_); CORE::die(@_); };
324
325         sub my_other_runmode {
326           my $self = shift;
327
328           $self->log->info("This will log to the logger configured in My::App");
329         }
330

BUGS

332       Please report any bugs or feature requests to
333       "bug-cgi-application-plugin-logdispatch@rt.cpan.org", or through the
334       web interface at <http://rt.cpan.org>.  I will be notified, and then
335       you'll automatically be notified of progress on your bug as I make
336       changes.
337

SEE ALSO

339       CGI::Application, Log::Dispatch, Log::Dispatch::Screen,
340       Sub::WrapPackages, perl(1)
341

AUTHOR

343       Cees Hek <ceeshek@gmail.com>
344

LICENSE

346       Copyright (C) 2004 Cees Hek <ceeshek@gmail.com>
347
348       This library is free software. You can modify and or distribute it
349       under the same terms as Perl itself.
350
351
352
353perl v5.12.0                      2010-C0G4I-:3:0Application::Plugin::LogDispatch(3)
Impressum