Log4perl日志文件中的双输出

时间:2011-03-14 17:22:32

标签: perl log4perl

我正在研究现有Perl代码库中的新功能,该功能允许我们记录发送到数据库的命令。我基于Log4perl的解决方案,这使我不必重新发明几个轮子。

不幸的是,我遇到了麻烦:每条消息都会被发送到日志文件两次。我希望它停止这样做。

我的研究(谷歌)表示,将相同的消息发送到两个不同的日志文件是一个常见的问题,但这不是我所看到的。每条消息在单个日志文件中出现两次。

有没有人知道我应该从哪里开始寻找纠正这种行为的方法?

<小时/> 编辑:配置文件如下所示:

my $log_packages = undef;

sub _get_logging_modifications {
    # Hash that is keyed by a package name
    # and the value is the level at which
    # to log that package
    return %{$log_packages} if defined $log_packages;
    $log_packages = {};

    my $log_info = $ENV{PROJECT_LOG_INFO} || '';

    for my $log_specification (split(/,/, $log_info)) {
        # Skip to the next log specification unless we have
        # a well formed log spec. i.e., Package::Name/LOGLEVEL
        next unless $log_specification =~ m!([^/]+)/([A-Z]+)!i;

        my $package   = $1;
        my $log_level = $2;

        $log_packages->{$package} = $log_level;
    }

    return %{$log_packages};
}

BEGIN {
    my $layout = Log::Log4perl::Layout::PatternLayout->new(
        '[%d] +%X{user_name}+ ||%X{request_uri}||%n  ' .
        '%C:%L - %P - %p - %n  ' .
        '%m%n'
    );

    my $web_data_path = $ENV{PROJECT_DATA_DIR}
        || File::Temp::tempdir( CLEANUP => 1 );

    my $logfile = "${web_data_path}/app.log";
    my $log = Log::Log4perl->get_logger('');

    my $app = Log::Log4perl::Appender->new(
        "Log::Dispatch::File",
        name     => 'APP',
        filename => $logfile,
    );

    $app->layout($layout);
    $log->add_appender($app);
    $log->level($WARN);

    my %levels = (
        FATAL => $FATAL,
        ERROR => $ERROR,
        WARN  => $WARN,
        INFO  => $INFO,
        DEBUG => $DEBUG,
    );

    my %mods = _get_logging_modifications();

    for my $cat (keys %mods) {
        my $level = uc($mods{$cat});
        next unless exists($levels{$level});
        my $other_log = Log::Log4perl->get_logger($cat);
        $other_log->level($levels{$level});
    }

    # NEW BLAIRHIPPO CODE STARTS HERE
    my $dbi_log = Log::Log4perl->get_logger('Project::NewLoggerThing');
    my $dbi_layout = Log::Log4perl::Layout::PatternLayout->new('%m%n');
    my $dbi_logfile = "/opt/home/blairhippo/test.log"; # FIXME: Make this configurable
    my $dbi_app = Log::Log4perl::Appender->new(
        "Log::Dispatch::File",
        name     => 'APP',
        filename => $dbi_logfile,
    );

    $dbi_app->layout($dbi_layout);
    $dbi_log->add_appender($dbi_app);
    $dbi_log->level($DEBUG); # FIXME:  Make this configurable   
}

1;

我希望这是一个漂亮的.conf文件,但我正在使用现有的代码。

3 个答案:

答案 0 :(得分:6)

Log::Log4perl常见问题解答中,有一个问题:I keep getting duplicate log messages! What's wrong?

答案 1 :(得分:3)

我在log4 *库中遇到过类似的问题;这表示使用两个不同的日志记录实例,其中内部两者都与单例绑定。我记得你必须选择两个独立的日志字符串。

答案 2 :(得分:1)

log4perl.oneMessagePerAppender = 1