File: Dispatch.pm

package info (click to toggle)
libcatalyst-plugin-log-dispatch-perl 0.121-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 88 kB
  • sloc: perl: 268; makefile: 2
file content (388 lines) | stat: -rw-r--r-- 10,924 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package Catalyst::Plugin::Log::Dispatch;

use warnings;
use strict;

our $VERSION = '0.121';

#use base 'Catalyst::Base';
use vars qw/$HasTimePiece $HasTimeHiRes/;
use UNIVERSAL::require;

BEGIN {
    Log::Dispatch::Config->use or warn "$@\nIt moves without using Log::Dispatch::Config.\n";
    $HasTimeHiRes = 1 if( Time::HiRes->use(qw/tv_interval/) );
    $HasTimePiece = 1 if( Time::Piece->use );
};
$Catalyst::Plugin::Log::Dispatch::CallerDepth = 0;

use IO::Handle;


# Module implementation here

sub setup {
    if( $Catalyst::VERSION >= 5.8 ) {
        MRO::Compat->use or die "can not use MRO::Compat : $@\n";
    }
    else {
        NEXT->use or die "can not use NEXT : $@\n";
    }
    my $c = shift;
    my $old_log = undef;
    if ( $c->log and ref( $c->log ) eq 'Catalyst::Log' ) {
        $old_log = $c->log;
    }
    $c->log( Catalyst::Plugin::Log::Dispatch::Backend->new );
    
    #Make it an array with one element if its a hashref
    if (ref ( $c->config->{'Log::Dispatch'} ) eq 'HASH') {
        $c->config->{'Log::Dispatch'} = [ $c->config->{'Log::Dispatch'} ];
    }
    
    unless ( ref( $c->config->{'Log::Dispatch'} ) eq 'ARRAY' ) {
        push(
            @{ $c->config->{'Log::Dispatch'} },
            {   class     => 'STDOUT',
                name      => 'default',
                min_level => 'debug',
                format    => '[%p] %m%n'
            }
        );

    }
    foreach my $tlogc ( @{ $c->config->{'Log::Dispatch'} } ) {
        my %logc = %{$tlogc};
        if ( $logc{'class'} eq 'STDOUT' or $logc{'class'} eq 'STDERR' ) {
            my $io = IO::Handle->new;
            $io->fdopen( fileno( $logc{'class'} ), 'w' );
            $logc{'class'}  = 'Handle';
            $logc{'handle'} = $io;
        }
        my $class = sprintf( "Log::Dispatch::%s", $logc{'class'} );
        delete $logc{'class'};
        $logc{'callbacks'} = [$logc{'callbacks'}] if(ref($logc{'callbacks'}) eq 'CODE');
        
        if(exists $logc{'format'} and defined $Log::Dispatch::Config::CallerDepth ) {
            my $callbacks = Log::Dispatch::Config->format_to_cb($logc{'format'},0);
            if(defined $callbacks) {
                $logc{'callbacks'} = [] unless($logc{'callbacks'});
                push(@{$logc{'callbacks'}}, $callbacks);
            }
        }
        if( exists $logc{'format_o'} and length( $logc{'format_o'} ) ) {
            my $callbacks = Catalyst::Plugin::Log::Dispatch->_format_to_cb_o($logc{'format_o'},0);
            if(defined $callbacks) {
                $logc{'callbacks'} = [] unless($logc{'callbacks'});
                push(@{$logc{'callbacks'}}, $callbacks);
            }
        }
        elsif(!$logc{'callbacks'}) {
            $logc{'callbacks'} = sub { my %p = @_; return "$p{message}\n"; };
        }
        $class->use or die "$@";
        my $logb = $class->new(%logc);
        $logb->{rtf} = $logc{real_time_flush} || 0;
        $c->log->add( $logb );
    }
    
    if ($old_log && defined __log_dispatch_get_body( $old_log ) ) {
        my @old_logs;
        foreach my $line ( split /\n/, __log_dispatch_get_body( $old_log ) ) {
            if ( $line =~ /^\[(\w+)] (.+)$/ ) {
                push( @old_logs, { level => $1, msg => [$2] } );
            }
            elsif( $line =~ /^\[(\w{3} \w{3}[ ]{1,2}\d{1,2}[ ]{1,2}\d{1,2}:\d{2}:\d{2} \d{4})\] \[catalyst\] \[(\w+)\] (.+)$/ ) {
                push( @old_logs, { level => $2, msg => [$3] } );
            }
            else {
                push( @{ $old_logs[-1]->{'msg'} }, $line );
            }
        }
        foreach my $line (@old_logs) {
            my $level = $line->{'level'};
            $c->log->$level( join( "\n", @{ $line->{'msg'} } ) );
        }
    }
    if( $Catalyst::VERSION >= 5.8 ) {
        return $c->maybe::next::method( @_ );
    }
    else {
        $c->NEXT::setup(@_);
    }
}


sub __log_dispatch_get_body {
    my $log = shift;
    return $Catalyst::VERSION >= 5.8 ? $log->_body : $log->body;
}
use Data::Dumper;
# copy and paste from Log::Dispatch::Config
# please teach a cool method.
sub _format_to_cb_o {
    my($class, $format, $stack) = @_;
    return undef unless defined $format;
    
    # caller() called only when necessary
    my $needs_caller = $format =~ /%[FLP]/;
    if( $HasTimeHiRes ) {
        return sub {
            my %p = @_;
            $p{p} = delete $p{level};
            $p{m} = delete $p{message};
            $p{n} = "\n";
            $p{'%'} = '%';
            $p{i} = $$;
            if ($needs_caller) {
                my $depth = 0; 
                $depth++ while caller($depth) =~ /^Catalyst::Plugin::Log::Dispatch/;
                $depth += $Catalyst::Plugin::Log::Dispatch::CallerDepth;
                @p{qw(P F L)} = caller($depth);
            }
            
            my ($t,$ms) = Time::HiRes::gettimeofday();
            $ms = sprintf('%06d', $ms);
            my $log = $format;
            $log =~ s{
                         (%d(?:{(.*?)})?)|   # $1: datetime $2: datetime fmt
                         (%MS)|              # $3: milli second
                         (?:%([%pmFLPni]))   # $4: others
                 }{
                     if ($1 && $2) {
                         _strftime_o($2,$t);
                     }
                     elsif ($1) {
                         scalar localtime;
                     }
                     elsif ($3) {
                         $ms;
                     }
                     elsif ($4) {
                         $p{$4};
                     }
                 }egx;
            return $log;
        };
    }
    else {
        return sub {
            my %p = @_;
            $p{p} = delete $p{level};
            $p{m} = delete $p{message};
            $p{n} = "\n";
            $p{'%'} = '%';
            $p{i} = $$;
            if ($needs_caller) {
                my $depth = 0; 
                $depth++ while caller($depth) =~ /^Catalyst::Plugin::Log::Dispatch/;
                $depth += $Catalyst::Plugin::Log::Dispatch::CallerDepth;
                @p{qw(P F L)} = caller($depth);
            }
            
            my $log = $format;
            $log =~ s{
                         (%d(?:{(.*?)})?)|   # $1: datetime $2: datetime fmt
                         (?:%([%pmFLPn]))    # $3: others
                 }{
                     if ($1 && $2) {
                         _strftime_o($2);
                     }
                     elsif ($1) {
                         scalar localtime;
                     }
                     elsif ($3) {
                         $p{$3};
                     }
                 }egx;
            return $log;
        };
    }
}

sub _strftime_o {
    my $fmt = shift;
    my $time = shift || time;
    if ($HasTimePiece) {
        return Time::Piece->new($time)->strftime($fmt);
    } else {
        require POSIX;
        return POSIX::strftime($fmt, localtime($time));
    }
}


1;

package Catalyst::Plugin::Log::Dispatch::Backend;

use strict;

use base qw/Log::Dispatch Class::Accessor::Fast/;

use Time::HiRes qw/gettimeofday/;
use Data::Dump;
use Data::Dumper;

{
    foreach my $l (qw/debug info warn error fatal/) {
        my $name = $l;
        $name = 'warning'  if ( $name eq 'warn' );
        $name = 'critical' if ( $name eq 'fatal' );

        no strict 'refs';
        *{"is_${l}"} = sub {
            my $self = shift;
            return $self->level_is_valid($name);
        };

        *{"$l"} = sub {
            my $self = shift;
            my %p = (level => $name,
                     message => "@_");
            local $Log::Dispatch::Config::CallerDepth += 1;
            local $Catalyst::Plugin::Log::Dispatch::CallerDepth += 3;
            if( keys( %{ $self->{outputs} } ) ) {
                foreach (keys %{ $self->{outputs} }) {
                    my %h = %p;
                    $h{name} = $_;
                    if( $self->{outputs}->{$_}->{rtf} ) {
                        $self->{outputs}->{$_}->log(%h);
                    }
                    else {
                        $h{message} = $self->{outputs}->{$_}->_apply_callbacks(%h)
                            if($self->{outputs}->{$_}->{callbacks});
                        push(@{$self->_body}, \%h);
                    }
                }
            }
            else {
                push(@{$self->_body}, \%p);
            }
        };
    }
}

sub new {
    my $pkg  = shift;
    my $this = $pkg->SUPER::new(@_);
    $this->mk_accessors(qw/abort _body/);
    $this->_body([]);
    return $this;
}


sub dumper {
    my $self = shift;
    return $self->debug( Data::Dumper::Dumper(@_) );
}

sub _dump {
    my $self = shift;
    return $self->debug( Data::Dump::dump(@_) );
}

sub level_is_valid {
    my $self = shift;
    return 0 if ( $self->abort );
    return $self->SUPER::level_is_valid(@_);
}

sub _flush {
    my $self = shift;
    if ( $self->abort || !(scalar @{$self->_body})) {
        $self->abort(undef);
    }
    else {
        foreach my $p (@{$self->_body}) {
            local $self->{outputs}->{$p->{name}}->{callbacks} = undef;
            $self->{outputs}->{$p->{name}}->log(%{$p});
        }
    }
    $self->_body([]);
}


1;    # Magic true value required at end of module
__END__


=head1 NAME

Catalyst::Plugin::Log::Dispatch - Log module of Catalyst that uses Log::Dispatch


=head1 VERSION

This document describes Catalyst::Plugin::Log::Dispatch version 2.15


=head1 SYNOPSIS

    package MyApp;

    use Catalyst qw/Log::Dispatch/;

configuration in source code

    MyApp->config->{ Log::Dispatch } = [
        {
         class     => 'File',
         name      => 'file',
         min_level => 'debug',
         filename  => MyApp->path_to('debug.log'),
         format    => '[%p] %m %n',
        }];

in myapp.yml

    Log::Dispatch:
     - class: File
       name: file
       min_level: debug
       filename: __path_to(debug.log)__
       mode: append
       format: '[%p] %m %n'

If you use L<Catalyst::Plugin::ConfigLoader>,
please load this module after L<Catalyst::Plugin::ConfigLoader>.

=head1 DESCRIPTION

Catalyst::Plugin::Log::Dispatch is a plugin to use Log::Dispatch from Catalyst.

=head1 CONFIGURATION

It is same as the configuration of Log::Dispatch excluding "class" and "format".

=over

=item class

The class name to Log::Dispatch::* object.
Please specify the name just after "Log::Dispatch::" of the class name.

=item format

It is the same as the format option of Log::Dispatch::Config.

=back

=head1 DEPENDENCIES

L<Catalyst>, L<Log::Dispatch>, L<Log::Dispatch::Config>

=head1 AUTHOR

Shota Takayama  C<< <shot[at]bindstorm.jp> >>


=head1 LICENCE AND COPYRIGHT

Copyright (c) 2006, Shota Takayama C<< <shot[at]bindstorm.jp> >>. All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.

=cut