CGI-Application-Plugin-LogDispatch-1.02/0000755000175000017500000000000010763000312016342 5ustar ceesceesCGI-Application-Plugin-LogDispatch-1.02/META.yml0000644000175000017500000000075610763000312017623 0ustar ceescees--- name: CGI-Application-Plugin-LogDispatch version: 1.02 author: - 'Cees Hek ' abstract: Plugin that adds Log::Dispatch support to CGI::Application license: perl requires: CGI::Application: 0 Log::Dispatch: 0.21 Scalar::Util: 0 UNIVERSAL::require: 0 recommends: Class::ISA: 0 Sub::WrapPackages: 0 provides: CGI::Application::Plugin::LogDispatch: file: lib/CGI/Application/Plugin/LogDispatch.pm version: 1.02 generated_by: Module::Build version 0.26 CGI-Application-Plugin-LogDispatch-1.02/Changes0000644000175000017500000000162610763000312017642 0ustar ceesceesRevision history for Perl extension CGI::Application::Plugin::LogDispatch. 1.02 Tue Mar 4 00:44:23 EST 2008 - Fix problem in test suite with newer versions of Test::More 1.01 Tue Mar 2 00:38:22 EST 2008 - fixed typo bug in log_config which should have caught you if you called log_config after the log object was already created. (reported by victor -at- taquiones.net) - CAP::DevPopup support added by Graham TerMarsch (cpan -at- howlingfrog.com) 1.00 Sat Oct 29 11:55:42 EDT 2005 - Simplify the pod tests according to Test::Pod docs - Add pod coverage tests - Rename some internal subs with a leading underscore - Change contact address 0.03 Tue Jan 18 23:58:30 EST 2005 - added singleton support 0.02 Thu Dec 16 11:14:56 EST 2004 - forgot to include CGI::Application as a dependancy 0.01 Tue Nov 2 21:31:17 EST 2004 - original version CGI-Application-Plugin-LogDispatch-1.02/Build.PL0000644000175000017500000000107010763000312017634 0ustar ceesceesuse Module::Build; Module::Build->new( module_name => 'CGI::Application::Plugin::LogDispatch', license => 'perl', requires => { 'CGI::Application' => 0, 'Log::Dispatch' => '0.21', 'UNIVERSAL::require' => 0, 'Scalar::Util' => 0, }, recommends => { 'Sub::WrapPackages' => 0, 'Class::ISA' => 0, }, create_makefile_pl => 'traditional', dist_author => 'Cees Hek ', dist_abstract => 'Plugin that adds Log::Dispatch support to CGI::Application', )->create_build_script; CGI-Application-Plugin-LogDispatch-1.02/MANIFEST0000644000175000017500000000054610763000312017500 0ustar ceesceesBuild.PL Changes lib/CGI/Application/Plugin/LogDispatch.pm Makefile.PL MANIFEST META.yml README t/01_basic.t t/02_newline.t t/03_options.t t/04_multiple.t t/05_logmethods.t t/06_singleton.t t/pod.t t/pod_coverage.t t/DummyIOHandle.pm t/TestAppBasic.pm t/TestAppLogMethods.pm t/TestAppMultiple.pm t/TestAppNewline.pm t/TestAppOptions.pm t/TestAppSingleton.pm CGI-Application-Plugin-LogDispatch-1.02/t/0000755000175000017500000000000010763000312016605 5ustar ceesceesCGI-Application-Plugin-LogDispatch-1.02/t/DummyIOHandle.pm0000644000175000017500000000070210763000312021601 0ustar ceesceespackage DummyIOHandle; # Very simple IO module that handles standard print statements, and # stores any printed statements in $self. # This is used as a Handle that Log::Dispatch::Handle can use, giving # us an in-memory buffer for collecting and testing log messages sub new { my $class = shift; my $string = ''; my $self = \$string; bless $self, $class; return $self; } sub print { my $self= shift; $$self .= join('', @_); } 1; CGI-Application-Plugin-LogDispatch-1.02/t/06_singleton.t0000644000175000017500000000234310763000312021303 0ustar ceesceesuse Test::More tests => 8; BEGIN { use_ok('CGI::Application::Plugin::LogDispatch') }; use lib './t'; use strict; eval { require Class::ISA; Class::ISA->import; }; if ($@) { plan skip_all => "Class::ISA required for Singleton support"; exit; } $ENV{CGI_APP_RETURN_ONLY} = 1; use TestAppSingleton; my $t1_obj = TestAppSingleton->new(); my $t1_output = $t1_obj->run(); my $logoutput = ${$TestAppSingleton::HANDLE}; unlike($logoutput, qr/log singleton debug/, 'no debug message'); like($logoutput, qr/log singleton info/, 'logged info message'); my $t2_obj = TestAppSingleton::Sub->new(); my $t2_output = $t2_obj->run(); $logoutput = ${$TestAppSingleton::Sub::HANDLE}; like($logoutput, qr/log singleton infoEXTRA/, 'logged info messageEXTRA'); unlike($logoutput, qr/info[^E]/, 'old info message not there'); my $t3_obj = TestAppSingleton::Sub2->new(); my $t3_output = $t3_obj->run(); $logoutput = ${$TestAppSingleton::HANDLE}; like($logoutput, qr/log singleton info/, 'old info message is there'); like($logoutput, qr/log subsingleton info/, 'logged info message in subclass'); TestAppSingleton->log->info('class info message'); $logoutput = ${$TestAppSingleton::HANDLE}; like($logoutput, qr/class info message/, 'class method'); CGI-Application-Plugin-LogDispatch-1.02/t/TestAppOptions.pm0000644000175000017500000000213210763000312022075 0ustar ceesceespackage TestAppOptions; use strict; use CGI::Application; use CGI::Application::Plugin::LogDispatch; use DummyIOHandle; @TestAppOptions::ISA = qw(CGI::Application); sub cgiapp_init { my $self = shift; $self->{__LOG_MESSAGES}->{HANDLE} = new DummyIOHandle; $self->log_config( LOG_DISPATCH_OPTIONS => { callbacks => sub { my %h = @_; chomp $h{message}; return $h{message}.'EXTRA'; }, }, LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::Handle', name => 'handle', min_level => 'debug', handle => $self->{__LOG_MESSAGES}->{HANDLE}, }, ], ); } sub setup { my $self = shift; $self->start_mode('test_mode'); $self->run_modes(test_mode => 'test_mode' ); } sub test_mode { my $self = shift; $self->log->debug("log debug"); $self->log->info('log info'); return "test_mode return value"; } 1; CGI-Application-Plugin-LogDispatch-1.02/t/03_options.t0000644000175000017500000000054510763000312020773 0ustar ceesceesuse Test::More tests => 2; use lib './t'; use strict; $ENV{CGI_APP_RETURN_ONLY} = 1; use TestAppOptions; my $t1_obj = TestAppOptions->new(); my $t1_output = $t1_obj->run(); my $logoutput = ${$t1_obj->{__LOG_MESSAGES}->{HANDLE}}; like($logoutput, qr/log debugEXTRA/, 'logged debug message'); like($logoutput, qr/log infoEXTRA/, 'logged info message'); CGI-Application-Plugin-LogDispatch-1.02/t/01_basic.t0000644000175000017500000000062110763000312020352 0ustar ceesceesuse Test::More tests => 3; BEGIN { use_ok('CGI::Application::Plugin::LogDispatch') }; use lib './t'; use strict; $ENV{CGI_APP_RETURN_ONLY} = 1; use TestAppBasic; my $t1_obj = TestAppBasic->new(); my $t1_output = $t1_obj->run(); my $logoutput = ${$t1_obj->{__LOG_MESSAGES}->{HANDLE}}; unlike($logoutput, qr/log debug/, 'no debug message'); like($logoutput, qr/log info/, 'logged info message'); CGI-Application-Plugin-LogDispatch-1.02/t/02_newline.t0000644000175000017500000000135610763000312020741 0ustar ceesceesuse Test::More tests => 7; use lib './t'; use strict; $ENV{CGI_APP_RETURN_ONLY} = 1; use TestAppNewline; my $t1_obj = TestAppNewline->new(); my $t1_output = $t1_obj->run(); my $logoutput = ${$t1_obj->{__LOG_MESSAGES}->{HANDLE}}; my $logoutput_append = ${$t1_obj->{__LOG_MESSAGES}->{HANDLE_APPEND}}; like($logoutput, qr/log debug1/, 'logged debug message'); like($logoutput, qr/log info1/, 'logged info message'); like($logoutput, qr#log debug2$/#, 'newline manually added'); unlike($logoutput, qr#log debug1$/#, 'no automatic newline added'); unlike($logoutput_append, qr/log debug1/, 'no debug message'); like($logoutput_append, qr/log info1/, 'logged info message'); like($logoutput_append, qr#log info1$/#, 'newline automatically added'); CGI-Application-Plugin-LogDispatch-1.02/t/TestAppNewline.pm0000644000175000017500000000314410763000312022047 0ustar ceesceespackage TestAppNewline; use strict; use CGI::Application; use CGI::Application::Plugin::LogDispatch; use DummyIOHandle; @TestAppNewline::ISA = qw(CGI::Application); sub cgiapp_init { my $self = shift; $self->{__LOG_MESSAGES}->{HANDLE} = new DummyIOHandle; $self->{__LOG_MESSAGES}->{HANDLE_APPEND} = new DummyIOHandle; $self->log_config( # LOG_DISPATCH_OPTIONS => { # callbacks => sub { my %h = @_; chomp $h{message}; return $h{message}.$/; }, # }, LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::Handle', name => 'handle', min_level => 'debug', handle => $self->{__LOG_MESSAGES}->{HANDLE}, }, { module => 'Log::Dispatch::Handle', append_newline => 1, name => 'handle_append', min_level => 'info', handle => $self->{__LOG_MESSAGES}->{HANDLE_APPEND}, }, ], ); } sub setup { my $self = shift; $self->start_mode('test_mode'); $self->run_modes(test_mode => 'test_mode' ); } sub test_mode { my $self = shift; $self->log->debug("log debug1"); $self->log->debug("log debug2$/"); $self->log->info('log info1'); $self->log->info('log info2'); return "test_mode return value"; } 1; CGI-Application-Plugin-LogDispatch-1.02/t/TestAppBasic.pm0000644000175000017500000000166310763000312021473 0ustar ceesceespackage TestAppBasic; use strict; use CGI::Application; use CGI::Application::Plugin::LogDispatch; use DummyIOHandle; @TestAppBasic::ISA = qw(CGI::Application); sub cgiapp_init { my $self = shift; $self->{__LOG_MESSAGES}->{HANDLE} = new DummyIOHandle; $self->log_config( LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::Handle', name => 'handle', min_level => 'info', handle => $self->{__LOG_MESSAGES}->{HANDLE}, }, ], ); } sub setup { my $self = shift; $self->start_mode('test_mode'); $self->run_modes(test_mode => 'test_mode' ); } sub test_mode { my $self = shift; $self->log->debug("log debug"); $self->log->info('log info'); return "test_mode return value"; } 1; CGI-Application-Plugin-LogDispatch-1.02/t/05_logmethods.t0000644000175000017500000000165410763000312021451 0ustar ceesceesuse Test::More; use lib './t'; use strict; eval { require Sub::WrapPackages; }; if ($@) { plan skip_all => "Sub::WrapPackages required to test method execution logging"; exit; } plan tests => 5; $ENV{CGI_APP_RETURN_ONLY} = 1; use TestAppLogMethods; my $t1_obj = TestAppLogMethods->new(); my $t1_output = $t1_obj->run(); my $logoutput = ${$t1_obj->{__LOG_MESSAGES}->{HANDLE}}; like($logoutput, qr/log debug/, 'logged debug message'); like($logoutput, qr/calling TestAppLogMethods::test_mode\(TestAppLogMethods=HASH/, "logged call to 'test_mode'"); like($logoutput, qr/returning from TestAppLogMethods::test_mode \(test_mode return value\)/, "logged return from 'test_mode'"); like($logoutput, qr/calling TestAppLogMethods::other_method\(param1, param2\)/, "logged call to 'other_method'"); like($logoutput, qr/returning from TestAppLogMethods::other_method \(other_method return value\)/, "logged return from 'other_method'"); CGI-Application-Plugin-LogDispatch-1.02/t/TestAppSingleton.pm0000644000175000017500000000357010763000312022413 0ustar ceesceespackage TestAppSingleton; use strict; use vars qw($HANDLE); use DummyIOHandle; BEGIN { $HANDLE = new DummyIOHandle; }; use CGI::Application; use CGI::Application::Plugin::LogDispatch ( LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::Handle', name => 'handle', min_level => 'info', handle => $HANDLE, }, ], ); @TestAppSingleton::ISA = qw(CGI::Application); sub setup { my $self = shift; $self->start_mode('test_mode'); $self->run_modes(test_mode => 'test_mode' ); } sub test_mode { my $self = shift; $self->log->debug("log singleton debug"); $self->log->info('log singleton info'); return "test_mode return value"; } package TestAppSingleton::Sub; use strict; use vars qw($HANDLE); use DummyIOHandle; BEGIN { $HANDLE = new DummyIOHandle; }; use CGI::Application; use CGI::Application::Plugin::LogDispatch ( LOG_DISPATCH_OPTIONS => { callbacks => sub { my %h = @_; chomp $h{message}; return $h{message}.'EXTRA'; }, }, LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::Handle', name => 'handle', min_level => 'info', handle => $HANDLE, }, ], ); @TestAppSingleton::Sub::ISA = qw(TestAppSingleton); package TestAppSingleton::Sub2; use strict; use vars qw($HANDLE); use CGI::Application; @TestAppSingleton::Sub2::ISA = qw(TestAppSingleton); sub test_mode { my $self = shift; $self->log->info('log subsingleton info'); return "test_mode return value"; } 1; CGI-Application-Plugin-LogDispatch-1.02/t/TestAppMultiple.pm0000644000175000017500000000246710763000312022250 0ustar ceesceespackage TestAppMultiple; use strict; use CGI::Application; use CGI::Application::Plugin::LogDispatch; use DummyIOHandle; @TestAppMultiple::ISA = qw(CGI::Application); sub cgiapp_init { my $self = shift; $self->{__LOG_MESSAGES}->{HANDLE} = new DummyIOHandle; $self->{__LOG_MESSAGES}->{HANDLE2} = new DummyIOHandle; $self->log_config( LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::Handle', name => 'handle', min_level => 'debug', handle => $self->{__LOG_MESSAGES}->{HANDLE}, }, { module => 'Log::Dispatch::Handle', name => 'handle2', min_level => 'info', handle => $self->{__LOG_MESSAGES}->{HANDLE2}, }, ], ); } sub setup { my $self = shift; $self->start_mode('test_mode'); $self->run_modes(test_mode => 'test_mode' ); } sub test_mode { my $self = shift; $self->log->debug('log debug'); $self->log->info('log info'); return "test_mode return value"; } 1; CGI-Application-Plugin-LogDispatch-1.02/t/04_multiple.t0000644000175000017500000000100710763000312021126 0ustar ceesceesuse Test::More tests => 4; use lib './t'; use strict; $ENV{CGI_APP_RETURN_ONLY} = 1; use TestAppMultiple; my $t1_obj = TestAppMultiple->new(); my $t1_output = $t1_obj->run(); my $logoutput = ${$t1_obj->{__LOG_MESSAGES}->{HANDLE}}; my $logoutput2 = ${$t1_obj->{__LOG_MESSAGES}->{HANDLE2}}; like($logoutput, qr/log debug/, 'logged debug message'); like($logoutput, qr/log info/, 'logged info message'); unlike($logoutput2, qr/log debug/, 'no debug message'); like($logoutput2, qr/log info/, 'logged info message'); CGI-Application-Plugin-LogDispatch-1.02/t/TestAppLogMethods.pm0000644000175000017500000000216010763000312022510 0ustar ceesceespackage TestAppLogMethods; use strict; use CGI::Application; use CGI::Application::Plugin::LogDispatch; use DummyIOHandle; @TestAppLogMethods::ISA = qw(CGI::Application); sub cgiapp_init { my $self = shift; $self->{__LOG_MESSAGES}->{HANDLE} = new DummyIOHandle; $self->log_config( LOG_DISPATCH_MODULES => [ { module => 'Log::Dispatch::Handle', name => 'handle', min_level => 'debug', handle => $self->{__LOG_MESSAGES}->{HANDLE}, append_newline => 1, }, ], LOG_METHOD_EXECUTION => [__PACKAGE__], ); } sub setup { my $self = shift; $self->start_mode('test_mode'); $self->run_modes(test_mode => 'test_mode' ); } sub test_mode { my $self = shift; $self->log->debug("log debug"); my $value = other_method('param1', 'param2'); return "test_mode return value"; } sub other_method { return "other_method return value"; } 1; CGI-Application-Plugin-LogDispatch-1.02/t/pod.t0000644000175000017500000000020110763000312017545 0ustar ceesceesuse Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); CGI-Application-Plugin-LogDispatch-1.02/t/pod_coverage.t0000644000175000017500000000024110763000312021424 0ustar ceesceesuse Test::More; eval "use Test::Pod::Coverage 1.00"; plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; all_pod_coverage_ok(); CGI-Application-Plugin-LogDispatch-1.02/README0000644000175000017500000000134610763000312017226 0ustar ceesceesCGI::Application::Plugin::LogDispatch Plugin that adds Log::Dispatch support to CGI::Application INSTALLATION To install this module, run the following commands: perl Build.PL ./Build ./Build test ./Build install Alternatively, if you do not have Module::Build but you do have 'make', you can used the included Makefile.PL and run the following commands: perl Makefile.PL make make test make install DEPENDENCIES CGI::Application Log::Dispatch UNIVERSAL::require Scalar::Util RECOMMENDS Sub::WrapPackages Class::ISA COPYRIGHT AND LICENCE Copyright (C) 2005 Cees Hek, All Rights Reserved. This library is free software. You can modify and or distribute it under the same terms as Perl itself. CGI-Application-Plugin-LogDispatch-1.02/lib/0000755000175000017500000000000010763000312017110 5ustar ceesceesCGI-Application-Plugin-LogDispatch-1.02/lib/CGI/0000755000175000017500000000000010763000312017512 5ustar ceesceesCGI-Application-Plugin-LogDispatch-1.02/lib/CGI/Application/0000755000175000017500000000000010763000312021755 5ustar ceesceesCGI-Application-Plugin-LogDispatch-1.02/lib/CGI/Application/Plugin/0000755000175000017500000000000010763000312023213 5ustar ceesceesCGI-Application-Plugin-LogDispatch-1.02/lib/CGI/Application/Plugin/LogDispatch.pm0000644000175000017500000004553010763000312025761 0ustar ceesceespackage CGI::Application::Plugin::LogDispatch; use strict; use vars qw($VERSION @EXPORT); use Log::Dispatch; use Log::Dispatch::Screen; use Scalar::Util (); use CGI::Application (); use File::Spec (); require UNIVERSAL::require; $VERSION = '1.02'; @EXPORT = qw( log log_config ); sub import { my $pkg = shift; my $callpkg = caller; no strict 'refs'; foreach my $sym (@EXPORT) { *{"${callpkg}::$sym"} = \&{$sym}; } $callpkg->log_config(@_) if @_; } sub log { my $self = shift; my ($log, $options, $frompkg) = _get_object_or_options($self); if (!$log) { # define the config hash if it doesn't exist to save some checks later $options = {} unless $options; # create Log::Dispatch object if ($options->{LOG_DISPATCH_OPTIONS}) { # use the parameters the user supplied $log = Log::Dispatch->new( %{ $options->{LOG_DISPATCH_OPTIONS} } ); } else { $log = Log::Dispatch->new( ); } if ($options->{LOG_DISPATCH_MODULES}) { foreach my $logger (@{ $options->{LOG_DISPATCH_MODULES} }) { if (!$logger->{module}) { # no logger module provided # not fatal... just skip this logger warn "No 'module' name provided -- skipping this logger"; } elsif (!$logger->{module}->require) { # Couldn't load the logger module # not fatal... just skip this logger warn $UNIVERSAL::require::ERROR; } else { my $module = delete $logger->{module}; # setup a callback to append a newline if requested if ($logger->{append_newline} || $options->{APPEND_NEWLINE}) { delete $logger->{append_newline} if exists $logger->{append_newline}; $logger->{callbacks} = [ $logger->{callbacks} ] if $logger->{callbacks} && ref $logger->{callbacks} ne 'ARRAY'; push @{ $logger->{callbacks} }, \&_append_newline; } # add the logger to the dispatcher $log->add( $module->new( %$logger ) ); } } } else { # create a simple STDERR logger my %options = ( name => 'screen', stderr => 1, min_level => 'debug', ); $options{callbacks} = \&_append_newline if $options->{APPEND_NEWLINE}; $log->add( Log::Dispatch::Screen->new( %options ) ); } _set_object($frompkg||$self, $log); # CAP::DevPopup support if (UNIVERSAL::can($self, 'devpopup')) { # Register our report with DevPopup $self->add_callback( 'devpopup_report', \&_devpopup_report ); # Create logger to capture all log entries my %options = ( 'name' => 'DevPopup', 'min_level' => 'debug', 'filename' => File::Spec->devnull(), 'callbacks' => sub { my %args = @_; push( @{$self->{LOG_DISPATCH_DEVPOPUP_HISTORY}}, [$args{level}, $args{message}] ); }, ); $log->add( Log::Dispatch::File->new(%options) ); } } return $log; } sub log_config { my $self = shift; my $class = ref $self ? ref $self : $self; my $log_config; if (ref $self) { die "Calling log_config after the log object has already been created" if @_ && defined $self->{__LOG_OBJECT}; $log_config = $self->{__LOG_CONFIG} ||= {}; } else { no strict 'refs'; die "Calling log_config after the log object has already been created" if @_ && defined ${$class.'::__LOG_OBJECT'}; ${$class.'::__LOG_CONFIG'} ||= {}; $log_config = ${$class.'::__LOG_CONFIG'}; } if (@_) { my $props; if (ref($_[0]) eq 'HASH') { my $rthash = %{$_[0]}; $props = CGI::Application->_cap_hash($_[0]); } else { $props = CGI::Application->_cap_hash({ @_ }); } my %options; # Check for LOG_OPTIONS if ($props->{LOG_DISPATCH_OPTIONS}) { die "log_config error: parameter LOG_DISPATCH_OPTIONS is not a hash reference" if ref $props->{LOG_DISPATCH_OPTIONS} ne 'HASH'; $log_config->{LOG_DISPATCH_OPTIONS} = delete $props->{LOG_DISPATCH_OPTIONS}; } # Check for LOG_DISPATCH_MODULES if ($props->{LOG_DISPATCH_MODULES}) { die "log_config error: parameter LOG_DISPATCH_MODULES is not an array reference" if ref $props->{LOG_DISPATCH_MODULES} ne 'ARRAY'; $log_config->{LOG_DISPATCH_MODULES} = delete $props->{LOG_DISPATCH_MODULES}; } # Check for APPEND_NEWLINE if ($props->{APPEND_NEWLINE}) { $log_config->{APPEND_NEWLINE} = 1; delete $props->{APPEND_NEWLINE}; } # Check for LOG_METHOD_EXECUTION if ($props->{LOG_METHOD_EXECUTION}) { die "log_config error: parameter LOG_METHOD_EXECUTION is not an array reference" if ref $props->{LOG_METHOD_EXECUTION} ne 'ARRAY'; _log_subroutine_calls($self->log, @{$props->{LOG_METHOD_EXECUTION}}); delete $props->{LOG_METHOD_EXECUTION}; } # If there are still entries left in $props then they are invalid die "Invalid option(s) (".join(', ', keys %$props).") passed to log_config" if %$props; } $log_config; } sub _log_subroutine_calls { my $log = shift; eval { Sub::WrapPackages->require; Sub::WrapPackages->import( packages => [@_], pre => sub { $log->debug("calling $_[0](".join(', ', @_[1..$#_]).")"); }, post => sub { no warnings qw(uninitialized); $log->debug("returning from $_[0] (".join(', ', @_[1..$#_]).")"); } ); 1; } or do { $log->error("Failed to load and configure Sub::WrapPackages: $@"); }; } sub _append_newline { my %hash = @_; chomp $hash{message}; return $hash{message}.$/; } ## ## Private methods ## sub _set_object { my $self = shift; my $log = shift; my $class = ref $self ? ref $self : $self; if (ref $self) { $self->{__LOG_OBJECT} = $log; } else { no strict 'refs'; ${$class.'::__LOG_OBJECT'} = $log; } } sub _get_object_or_options { my $self = shift; my $class = ref $self ? ref $self : $self; # Handle the simple case by looking in the object first if (ref $self) { return ($self->{__LOG_OBJECT}, undef) if $self->{__LOG_OBJECT}; return (undef, $self->{__LOG_CONFIG}) if $self->{__LOG_CONFIG}; } # See if we can find them in the class hierarchy # We look at each of the modules in the @ISA tree, and # their parents as well until we find either a log # object or a set of configuration parameters require Class::ISA; foreach my $super ($class, Class::ISA::super_path($class)) { no strict 'refs'; return (${$super.'::__LOG_OBJECT'}, undef) if ${$super.'::__LOG_OBJECT'}; return (undef, ${$super.'::__LOG_CONFIG'}, $super) if ${$super.'::__LOG_CONFIG'}; } return; } sub _devpopup_report { my $self = shift; my $r=0; my $history = join $/, map { $r=1-$r; qq($_->[0]$_->[1]) } @{$self->{LOG_DISPATCH_DEVPOPUP_HISTORY}}; $self->devpopup->add_report( title => 'Log Entries', summary => 'All entries logged via Log::Dispatch', report => qq(
$history
LevelMessage
), ); } 1; __END__ =head1 NAME CGI::Application::Plugin::LogDispatch - Add Log::Dispatch support to CGI::Application =head1 SYNOPSIS 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'); } =head1 DESCRIPTION CGI::Application::Plugin::LogDispatch adds logging support to your L modules by providing a L dispatcher object that is accessible from anywhere in the application. If you have L 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. =head1 METHODS =head2 log This method will return the current L dispatcher object. The L 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 has not been called before the first call to 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'); =head2 log_config 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 L 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 is in the C stage of L. 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: =over 4 =item LOG_DISPATCH_OPTIONS This allows you to customize how the L object is created by providing a hash of options that will be passed to the L constructor. Please see the documentation for L for the exact syntax of the parameters. Surprisingly enough you will usually not need to use this option, instead look at the LOG_DISPATCH_MODULES option. LOG_DISPATCH_OPTIONS => { callbacks => sub { my %h = @_; return time().': '.$h{message}; }, } =item LOG_DISPATCH_MODULES 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 APPEND_NEWLINE 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(foo@bar.com bar@baz.org ) ], subject => 'Oh No!!!!!!!!!!', min_level => 'emerg' } ] =item APPEND_NEWLINE 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 =item LOG_METHOD_EXECUTION (EXPERIMENTAL) This option will allow you to log the execution path of your program. Set LOG_METHOD_EXECUTION 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)], WARNING: 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 L module. =back =head2 DEFAULT OPTIONS 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 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, ); =head1 EXAMPLE 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(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... } =head1 SINGLETON SUPPORT This 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. =head1 SINGLETON EXAMPLE 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"); } =head1 BUGS Please report any bugs or feature requests to C, or through the web interface at L. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. =head1 SEE ALSO L, L, L, L, perl(1) =head1 AUTHOR Cees Hek =head1 LICENSE Copyright (C) 2004 Cees Hek This library is free software. You can modify and or distribute it under the same terms as Perl itself. =cut CGI-Application-Plugin-LogDispatch-1.02/Makefile.PL0000644000175000017500000000111210763000312020307 0ustar ceescees# Note: this file was auto-generated by Module::Build::Compat version 0.03 use ExtUtils::MakeMaker; WriteMakefile ( 'PL_FILES' => {}, 'INSTALLDIRS' => 'site', 'NAME' => 'CGI::Application::Plugin::LogDispatch', 'VERSION_FROM' => 'lib/CGI/Application/Plugin/LogDispatch.pm', 'PREREQ_PM' => { 'Scalar::Util' => 0, 'CGI::Application' => 0, 'Log::Dispatch' => '0.21', 'UNIVERSAL::require' => 0 } ) ;