CGI-Application-Plugin-LogDispatch

 view release on metacpan or  search on metacpan

lib/CGI/Application/Plugin/LogDispatch.pm  view on Meta::CPAN

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
                    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;
}

lib/CGI/Application/Plugin/LogDispatch.pm  view on Meta::CPAN

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
=over 4
 
=item LOG_DISPATCH_OPTIONS
 
This allows you to customize how the L<Log::Dispatch> object is created by providing a hash of
options that will be passed to the L<Log::Dispatch> constructor.  Please see the documentation
for L<Log::Dispatch> 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

t/TestAppNewline.pm  view on Meta::CPAN

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@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',

t/TestAppOptions.pm  view on Meta::CPAN

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@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},
                          },
              ],
    );

t/TestAppSingleton.pm  view on Meta::CPAN

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 
use strict;
use vars qw($HANDLE);
 
BEGIN { $HANDLE = new DummyIOHandle; };
              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,
                          },
              ],
);



( run in 0.268 second using v1.01-cache-2.11-cpan-05444aca049 )