1package Plack::Middleware::LogDispatch; 2use strict; 3use parent qw(Plack::Middleware); 4use Plack::Util::Accessor qw(logger); 5use Carp (); 6 7sub prepare_app { 8 my $self = shift; 9 unless ($self->logger) { 10 Carp::croak "logger is not defined"; 11 } 12} 13 14sub call { 15 my($self, $env) = @_; 16 17 $env->{'psgix.logger'} = sub { 18 my $args = shift; 19 $args->{level} = 'critical' if $args->{level} eq 'fatal'; 20 21 if ( ref $args->{message} && ref $args->{message} ne 'CODE' ) { 22 $args->{message} .= q{}; 23 } 24 25 $self->logger->log(%$args); 26 }; 27 28 $self->app->($env); 29} 30 311; 32 33__END__ 34 35=head1 NAME 36 37Plack::Middleware::LogDispatch - Uses Log::Dispatch to configure logger 38 39=head1 SYNOPSIS 40 41 use Log::Dispatch; 42 43 my $logger = Log::Dispatch->new; 44 $logger->add( Log::Dispatch::File->new(...) ); 45 $logger->add( Log::Dispatch::DesktopNotification->new(...) ); 46 47 builder { 48 enable "LogDispatch", logger => $logger; 49 $app; 50 } 51 52 # use with Log::Dispatch::Config 53 use Log::Dispatch::Config; 54 Log::Dispatch::Config->configure('/path/to/log.conf'); 55 56 builder { 57 enable "LogDispatch", logger => Log::Dispatch::Config->instance; 58 ... 59 } 60 61=head1 DESCRIPTION 62 63LogDispatch is a L<Plack::Middleware> component that allows you to use 64L<Log::Dispatch> to configure the logging object, C<psgix.logger>. 65 66=head1 CONFIGURATION 67 68=over 4 69 70=item logger 71 72L<Log::Dispatch> object to send logs to. Required. 73 74=back 75 76=head1 AUTHOR 77 78Tatsuhiko Miyagawa 79 80=head1 SEE ALSO 81 82L<Log::Dispatch> 83 84L<Plack::Middleware::Log4perl> 85 86=cut 87 88