leponceau.org

Programming And Stuff, You Know The Thing…

Log::Log4perl Examples

Posted at — May 24, 2011

Easy

use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init({level => $ENV{DEBUG} ? $DEBUG : $WARN, layout => '%d %H %F{1}[%P] P:%p C:%c M:%M Z:%L %m%n'});
DEBUG(...); # <-- :easy
INFO(...);
WARN(...);
ERROR(...);
LOGDIE(...);
LOGCONFESS(...);
...

Example Configs

log4perl.logger     = DEBUG, FileAppender1,AppStat

#MAIN  LOG  - taegliches FILE-Rotate an
log4perl.appender.FileAppender1.Threshold           = INFO
log4perl.appender.FileAppender1   = Log::Dispatch::FileRotate
log4perl.appender.FileAppender1.filename  =
/home/interface/var/log/bestandRunner.log
log4perl.appender.FileAppender1.max                 = 5
log4perl.appender.FileAppender1.DatePattern         = yyyy-MM-dd
log4perl.appender.FileAppender1.mode                = append
log4perl.appender.FileAppender1.layout              = PatternLayout
log4perl.appender.FileAppender1.layout.ConversionPattern = [%d] [%c] %p
%L> %m%n

# Filter to match level STATUS
log4perl.filter.MatchStat   = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchStat.LevelToMatch    = STATUS
log4perl.filter.MatchStat.AcceptOnMatch   = true

# Status-LOG  Appender mit Filter
log4perl.appender.AppStat   = Log::Log4perl::Appender::File
log4perl.appender.AppStat.filename  =
/home/interface/var/log/bestandRunnerStatus.log
log4perl.appender.AppStat.layout                   = PatternLayout
log4perl.appender.AppStat.layout.ConversionPattern = %d#%m%n
log4perl.appender.AppStat.Filter                   = MatchStat 


# DEBUG|INFO|WARN|ERROR|FATAL|ALL|OFF
Log::Log4perl::Config::allow_code( 1 );
my $log_config = q(
    log4perl.logger                    = sub { loglevel() }

    log4perl.appender.Syslog           = Log::Dispatch::Syslog
    log4perl.appender.Syslog.Threshold = INFO
    log4perl.appender.Syslog.layout    = PatternLayout
    log4perl.appender.Syslog.layout.ConversionPattern = %F{1} C:%c M:%M Z:%L %m%n

    log4perl.appender.FileApp          = Log::Log4perl::Appender::File
    log4perl.appender.FileApp.filename = /path/to/log/file.log
    log4perl.appender.FileApp.layout   = PatternLayout
    log4perl.appender.FileApp.layout.ConversionPattern = %d - %P [%p] %c > %m (%M, line %L)%n
    log4perl.appender.Logfile.recreate = 1
    log4perl.appender.Logfile.recreate_check_signal = USR1
);

sub loglevel() {
    if ($debug) {
        return ("ALL, FileApp, Syslog");
    } else {
        return ("INFO, FileApp, Syslog");
    }
}

Log::Log4perl::init( \$log_config );


use constant LOG_LAYOUT => '%d %H %F{1}[%P] P:%p C:%c M:%M Z:%L %m%n';
use constant LOG_LAYOUT_CONSOLE => '%08r [%05P] l%03L %.1p %m%n';
use constant LOG_LAYOUT_CONSOLE2 => '%08r l%04L %.1p M:%M %m%n';

Carp

# replace Carp methods with Log4perl methods....
{
    use Carp;
    {
        package Carp;
        local $^W = 0;
        no warnings;
        *carp = sub {
            local $Log::Log4perl::caller_depth += 1;
            Log::Log4perl->get_logger()->warn(@_);
        };
        *cluck = sub {
            local $Log::Log4perl::caller_depth += 1;
            Log::Log4perl->get_logger()->logcluck(@_);
        };
        *croak = sub {
            local $Log::Log4perl::caller_depth += 1;
            Log::Log4perl->get_logger()->logdie(@_);
        };
        *confess = sub {
            local $Log::Log4perl::caller_depth += 1;
            Log::Log4perl->get_logger()->logconfess(@_);
        };
    }
}