Log::Any::Adapter::Log4perl - Log::Any::Adapter::Log4perl documentation


Log-Any-Adapter-Log4perl documentation Contained in the Log-Any-Adapter-Log4perl distribution.

Index


Code Index:

NAME

Top

Log::Any::Adapter::Log4perl

SYNOPSIS

Top

    use Log::Log4perl;
    Log::Log4perl::init('/etc/log4perl.conf');

    Log::Any::Adapter->set('Log::Log4perl');

DESCRIPTION

Top

This Log::Any adapter uses Log::Log4perl for logging. log4perl must be initialized before calling set. There are no parameters.

LOG LEVEL TRANSLATION

Top

Log levels are translated from Log::Any to Log4perl as follows:

    notice -> info
    warning -> warn
    critical -> fatal
    alert -> fatal
    emergency -> fatal

SEE ALSO

Top

Log::Any, Log::Any::Adapter, Log::Log4perl

AUTHOR

Top

Jonathan Swartz

COPYRIGHT & LICENSE

Top


Log-Any-Adapter-Log4perl documentation Contained in the Log-Any-Adapter-Log4perl distribution.

package Log::Any::Adapter::Log4perl;
use Log::Log4perl;
use Log::Any::Adapter::Util qw(make_method);
use strict;
use warnings;
use base qw(Log::Any::Adapter::Base);

our $VERSION = '0.06';

sub init {
    my ($self) = @_;

    $self->{logger} = Log::Log4perl->get_logger( $self->{category} );
}

foreach my $method ( Log::Any->logging_and_detection_methods() ) {
    my $log4perl_method = $method;

    # Map log levels down to log4perl levels where necessary
    #
    for ($log4perl_method) {
        s/notice/info/;
        s/warning/warn/;
        s/critical|alert|emergency/fatal/;
    }

    # Delegate to log4perl logger, increasing caller_depth so that %F, %C,
    # etc. are generated correctly
    #
    make_method(
        $method,
        sub {
            my $self = shift;
            local $Log::Log4perl::caller_depth =
              $Log::Log4perl::caller_depth + 1;
            return $self->{logger}->$log4perl_method(@_);
        }
    );
}

# Override alias and printf variants to increase depth first
#
my %aliases = Log::Any->log_level_aliases;
my @methods = (
    keys(%aliases),
    ( map { $_ . "f" } ( Log::Any->logging_methods, keys(%aliases) ) )
);
foreach my $method (@methods) {
    make_method(
        $method,
        sub {
            my $self = shift;
            local $Log::Log4perl::caller_depth =
              $Log::Log4perl::caller_depth + 2;
            my $super_method = "SUPER::$method";
            return $self->$super_method(@_);
        }
    );
}

1;

__END__