Log4perl 日志文件中的双输出
我正在现有 Perl 代码库中开发一项新功能,该功能允许我们记录发送到数据库的命令。我的解决方案基于 Log4perl,这使我不必重新发明几个轮子。
不幸的是,我遇到了一个障碍:每条消息都会发送到日志文件两次。我希望它停止这样做。
我的研究(谷歌)表明,将相同的消息发送到两个不同的日志文件是一个很常见的问题,但这不是我所看到的。每条消息在单个日志文件中出现两次。
有人对我应该从哪里开始寻找纠正这种行为的方法有任何建议吗?
Edit: The config file looks like this:
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 文件,但我正在使用现有的代码。
I'm working on a new feature in an existing Perl codebase that will allow us to log the commands being sent to the database. I'm basing the solution off of Log4perl, which prevents me from having to reinvent several wheels.
Unfortunately, I'm running into a snag: every message gets sent to the log file twice. I'd like it to stop doing that.
My research (Google) has indicated that getting the same message sent to two different log files is a common enough problem, but that's not what I'm seeing. Each and every message appears twice within the single log file.
Does anybody have any tips on where I should start looking for ways to correct this behavior?
Edit: The config file looks like this:
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;
I wish this was a nice pretty .conf file, but I'm working with the existing code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
Log::Log4perl
常见问题解答中有一个问题:我不断收到重复的日志消息!怎么了?。In the
Log::Log4perl
FAQ there is a question: I keep getting duplicate log messages! What's wrong?.我对 log4* 库也有类似的问题;这表明使用了两个不同的日志记录实例,其中两个实例在内部都与单例相关联。我记得,您必须选择两个单独的日志记录字符串。
I've had similar issues with the log4* libraries; this is indicative of using two different logging instantiations where internally both are tied against a singleton. You will have to select two separate logging strings, as I recall.