CGI-Builder-LogDispatch

 view release on metacpan or  search on metacpan

lib/CGI/Builder/LogDispatch.pm  view on Meta::CPAN

package CGI::Builder::LogDispatch;

$VERSION = 0.1;

use strict;
use Log::Dispatch 2.0 ();
use Date::Format ();

$Carp::Internal{ 'Log::Dispatch' }++;
$Carp::Internal{+__PACKAGE__}++;

use Class::groups(
    { name    => 'logger_config',
      default => {  }
    } ,
);

use Class::props(
    { name    => 'logger',
      default => sub { shift()->logger_new( @_ ) }
    } ,
);

# Note name and min_level are required params to new(), so we hard-code them
# but allow overrides from the user configuration
sub logger_new { 
    my ($self) = @_;
    my %defaults;
    my $logger = Log::Dispatch->new();
    if ( $self->can('r') and $self->r->can('log') ) {
        require Log::Dispatch::ApacheLog;
        %defaults = (name=>'default', min_level=>'warning',apache=>$self->r);
        $logger->add(
            Log::Dispatch::ApacheLog->new( %defaults, %{$self->logger_config} )
        );
    } else {
        require Log::Dispatch::Screen;
        %defaults = ( name=>'default'
                    , min_level=>'warning'
                    , callbacks => sub { sprintf "[%s] %s\n", Date::Format::time2str('%Y-%m-%d %H:%M:%S',time), $_[1] }
                    );
        $logger->add(
            Log::Dispatch::Screen->new( %defaults, %{$self->logger_config} )
        );
    }
    return $logger;
}


1;

__END__

=head1 NAME

CGI::Builder::LogDispatch - integrated logging system for CGI::Builder

=head1 VERSION 0.01

=head1 INSTALLATION

=over

=item Prerequisites

    CGI::Builder    >= 1.12
    Log::Dispatch   >= 2.0

=back

=head1 SYNOPSIS

    # just include it in your build
    use CGI::Builder
    qw| CGI::Builder::LogDispatch
      |;
    
    # Logger can write to different "Log Levels". The default level is 
    # "warning". Messages of lesser importance will not be written. When
    # developing, you probably want this cranked to "debug":
    sub OH_init {
        my ($webapp) = @_;
        # This MUST be done before calling $webapp->logger or it will have no effect!
        $webapp->logger_config('min_level' => 'debug');
    }
    
    # Then use it to write nicely formatted messages to your web server log
    sub PH_AUTOLOAD {
        my ($webapp) = @_;
        # The default min_level
        $webapp->logger->debug("This message only gets logged if you set min_level=>debug.");
        if ( $webapp->page_error ) {
            $webapp->logger->error("Oh no! There is a problem!");
        }
    }

=head1 DESCRIPTION

The module should do what you want with no prodding. Just include it and start 
using the logger property to log things. If you are using Apache::CGI::Builder



( run in 0.783 second using v1.01-cache-2.11-cpan-39bf76dae61 )