Add log::dispatch support to cgi::application
package My::App; use CGI::Application::Plugin::LogDispatch; sub cgiapp_init { my $self = shift; # calling log_config is optional as # some simple defaults will be used $self->log_config( LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::File', name => 'debug', filename => '/tmp/debug.log', min_level => 'debug', }, ] ); } sub myrunmode { my $self = shift; $self->log->info('Information message'); $self->log->debug('Debug message'); } - or as a class based singleton - package My::App; use CGI::Application::Plugin::LogDispatch ( LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::File', name => 'debug', filename => '/tmp/debug.log', min_level => 'debug', }, ] ); My::App->log->info('Information message'); sub myrunmode { my $self = shift; $self->log->info('This also works'); }
CGI::Application::Plugin::LogDispatch adds logging support to your CGI::Application modules by providing a Log::Dispatch dispatcher object that is accessible from anywhere in the application.
If you have CGI::Application::Plugin::DevPopup installed, a \*(L"Log Entries\*(R" report is added to the popup window, containing all of the entries that were logged during the execution of the runmode.
This method will return the current Log::Dispatch dispatcher object. The Log::Dispatch object is created on the first call to this method, and any subsequent calls will return the same object. This effectively creates a singleton log dispatcher for the duration of the request. If \*(C`log_config\*(C' has not been called before the first call to \*(C`log\*(C', then it will choose some sane defaults to create the dispatcher object (the exact default values are defined below).
# retrieve the log object my $log = $self->log; $log->warning("something's not right!"); $log->emergency("It's all gone pear shaped!");
- or -
# use the log object directly $self->log->debug(Data::Dumper::Dumper(\%hash));
- or -
# if you configured it as a singleton My::App->log->debug('This works too');
This method can be used to customize the functionality of the CGI::Application::Plugin::LogDispatch module. Calling this method does not mean that a new Log::Dispatch object will be immediately created. The log object will not be created until the first call to $self->log.
The recommended place to call \*(C`log_config\*(C' is in the \*(C`cgiapp_init\*(C' stage of CGI::Application. If this method is called after the log object has already been accessed, then it will die with an error message.
If this method is not called at all then a reasonable set of defaults will be used (the exact default values are defined below).
The following parameters are accepted:
This allows you to customize how the Log::Dispatch object is created by providing a hash of options that will be passed to the Log::Dispatch constructor. Please see the documentation for Log::Dispatch for the exact syntax of the parameters. Surprisingly enough you will usually not need to use this option, instead look at the \s-1LOG_DISPATCH_MODULES\s0 option. LOG_DISPATCH_OPTIONS => { callbacks => sub { my %h = @_; return time().': '.$h{message}; }, }
This option allows you to specify the Log::Dispatch::* modules that you wish to use to log messages. You can list multiple dispatch modules, each with their own set of options. Format the options in an array of hashes, where each hash contains the options for the Log::Dispatch:: module you are configuring and also include a 'module' parameter containing the name of the dispatch module. See below for an example. You can also add an 'append_newline' option to automatically append a newline to each log entry for this dispatch module (this option is not needed if you already specified the \s-1APPEND_NEWLINE\s0 option listed below which will add a newline for all dispatch modules). LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::File', name => 'messages', filename => '/tmp/messages.log', min_level => 'info', append_newline => 1 }, { module => 'Log::Dispatch::Email::MailSend', name => 'email', to => [ qw([email protected] [email protected] ) ], subject => 'Oh No!!!!!!!!!!', min_level => 'emerg' } ]
By default Log::Dispatch does not append a newline to the end of the log messages. By setting this option to a true value, a newline character will automatically be added to the end of the log message. APPEND_NEWLINE => 1
This option will allow you to log the execution path of your program. Set \s-1LOG_METHOD_EXECUTION\s0 to a list of all the modules you want to be logged. This will automatically send a debug message at the start and end of each method/function that is called in the modules you listed. The parameters passed, and the return value will also be logged. This can be useful by tracing the program flow in the logfile without having to resort to the debugger. LOG_METHOD_EXECUTION => [qw(_\|_PACKAGE_\|_ CGI::Application CGI)], \s-1WARNING:\s0 This hasn't been heavily tested, although it seems to work fine for me. Also, a closure is created around the log object, so some care may need to be taken when using this in a persistent environment like mod_perl. This feature depends on the Sub::WrapPackages module.
The following example shows what options are set by default (ie this is what you would get if you do not call log_config). A single Log::Dispatch::Screen module that writes error messages to \s-1STDERR\s0 with a minimum log level of debug.
$self->log_config( LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::Screen', name => 'screen', stderr => 1, min_level => 'debug', append_newline => 1 } ], );
Here is a more customized example that uses two file appenders, and an email gateway. Here all debug messages are sent to /tmp/debug.log, and all messages above are sent to /tmp/messages.log. Also, any emergency messages are emailed to [email protected] and [email protected].
$self->log_config( LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::File', name => 'debug', filename => '/tmp/debug.log', min_level => 'debug', max_level => 'debug' }, { module => 'Log::Dispatch::File', name => 'messages', filename => '/tmp/messages.log', min_level => 'info' }, { module => 'Log::Dispatch::Email::MailSend', name => 'email', to => [ qw([email protected] [email protected] ) ], subject => 'Oh No!!!!!!!!!!', min_level => 'emerg' } ], APPEND_NEWLINE => 1, );
In a CGI::Application module:
# configure the log modules once during the init stage sub cgiapp_init { my $self = shift;
# Configure the session $self->log_config( LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::File', name => 'messages', filename => '/tmp/messages.log', min_level => 'error' }, { module => 'Log::Dispatch::Email::MailSend', name => 'email', to => [ qw([email protected] [email protected] ) ], subject => 'Oh No!!!!!!!!!!', min_level => 'emerg' } ], APPEND_NEWLINE => 1, );
}
sub cgiapp_prerun { my $self = shift;
$self->log->debug("Current runmode: ".$self->get_current_runmode); }
sub my_runmode { my $self = shift; my $log = $self->log;
if ($ENV{'REMOTE_USER'}) { $log->info("user ".$ENV{'REMOTE_USER'}); }
# etc... }
This module can be used as a singleton object. This means that when the object is created, it will remain accessible for the duration of the process. This can be useful in persistent environments like mod_perl and PersistentPerl, since the object only has to be created one time, and will remain in memory across multiple requests. It can also be useful if you want to setup a \s-1DIE\s0 handler, or \s-1WARN\s0 handler, since you will not have access to the $self object.
To use this module as a singleton you need to provide all configuration parameters as options to the use statement. The use statement will accept all the same parameters that the log_config method accepts, so see the documentation above for more details.
When creating the singleton, the log object will be saved in the namespace of the module that created it. The singleton will also be inherited by any subclasses of this module.
\s-1NOTE:\s0 Singleton support requires the Class::ISA module which is not installed automatically by this module.
package My::App;
use base qw(CGI::Application); use CGI::Application::Plugin::LogDispatch( LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::File', name => 'messages', filename => '/tmp/messages.log', min_level => 'error' }, ], APPEND_NEWLINE => 1, );
}
sub cgiapp_prerun { my $self = shift;
$self->log->debug("Current runmode: ".$self->get_current_runmode); }
sub my_runmode { my $self = shift; my $log = $self->log;
if ($ENV{'REMOTE_USER'}) { $log->info("user ".$ENV{'REMOTE_USER'}); }
# etc... }
package My::App::Subclass;
use base qw(My::App);
# Setup a die handler that uses the logger $SIG{_\|_DIE_\|_} = sub { My::App::Subclass->log->emerg(@_); CORE::die(@_); };
sub my_other_runmode { my $self = shift;
$self->log->info("This will log to the logger configured in My::App"); }
Please report any bugs or feature requests to \*(C`[email protected]\*(C', or through the web interface at <http://rt.cpan.org>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
CGI::Application, Log::Dispatch, Log::Dispatch::Screen, Sub::WrapPackages, perl\|(1)
Cees Hek <[email protected]>
Copyright (C) 2004 Cees Hek <[email protected]>
This library is free software. You can modify and or distribute it under the same terms as Perl itself.