|
NAMECGI::Application::Plugin::LogDispatch - Add Log::Dispatch support to CGI::ApplicationSYNOPSISpackage 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'); } DESCRIPTIONCGI::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 "Log Entries" report is added to the popup window, containing all of the entries that were logged during the execution of the runmode. METHODSlogThis 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 "log_config" has not been called before the first call to "log", 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'); log_configThis 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 "log_config" is in the "cgiapp_init" 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:
DEFAULT OPTIONSThe 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 STDERR 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 foo@bar.com and bar@baz.org. $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(foo@bar.com bar@baz.org ) ], subject => 'Oh No!!!!!!!!!!', min_level => 'emerg' } ], APPEND_NEWLINE => 1, ); EXAMPLEIn 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(foo@bar.com bar@baz.org ) ], 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... } SINGLETON SUPPORTThis module can be used as a singleton object. This means that when the object is created, it will remain accessable 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 DIE handler, or WARN 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. NOTE: Singleton support requires the Class::ISA module which is not installed automatically by this module. SINGLETON EXAMPLEpackage 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"); } BUGSPlease report any bugs or feature requests to "bug-cgi-application-plugin-logdispatch@rt.cpan.org", 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.SEE ALSOCGI::Application, Log::Dispatch, Log::Dispatch::Screen, Sub::WrapPackages, perl(1)AUTHORCees Hek <ceeshek@gmail.com>LICENSECopyright (C) 2004 Cees Hek <ceeshek@gmail.com>This library is free software. You can modify and or distribute it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |