我有一个带有两个附加程序(一个屏幕和另一个文件)的记录器。我想要一个具有可以更改的可变日志级别的屏幕附加程序和无论如何都会记录所有内容的文件附加程序。因此,例如,您可以禁用任何到屏幕的输出(屏幕附加程序),但在您的日志文件(文件附加程序)中将完整的日志记录到 TRACE 级别。我成功地更改了 Screen appender,但我无法将同一记录器的 File appender 设置为 TRACE 级别。我尝试使用不同的阈值设置,但没有成功。
# Define a category logger
my $log = Log::Log4perl->get_logger("main");
# Define a layout
my $layout = Log::Log4perl::Layout::PatternLayout->new("[%d{yyyy/MM/dd HH:mm:ss,SSS}]%m%n");
# Define a file appender
my $file_appender = Log::Log4perl::Appender->new(
"Log::Log4perl::Appender::File",
name => "Logfile",
filename => "$logfile",
autoflush => 1,
umask => 022,
header_text => "INVOCATION:$0 @ARGV",
#Threshold => "TRACE", DOES NOT WORK
);
# Define a stderr appender
my $stderr_appender = Log::Log4perl::Appender->new(
"Log::Log4perl::Appender::ScreenColoredLevels",
name => "Screen",
stderr => 1,
);
# Have both appenders use the same layout (could be different)
$stderr_appender->layout($layout);
$file_appender->layout($layout);
#add both appenders to logger
$log->add_appender($stderr_appender);
$log->add_appender($file_appender);
#add a level to logger
#$log_level coming from command line or configuration
$log->level($log_level);
#$file_appender->threshold( "TRACE" ); THIS DOES NOT WORK
#Log::Log4perl->appender_thresholds_adjust(-1, ['Logfile']); NOR THIS
#check your appenders
#print Dumper( Log::Log4perl->appenders() );