问题
I have a logger with two appenders (one Screen and other File). I want to have a Screen appender with a variable log level that can be changed and File appender that will log everything no matter what. So for instance, you can disable any output to screen (Screen appender) but get full logging to the level of TRACE in your logfile (File appender). I succeeded in getting changing Screen appender but I can't set File appender for the same logger to the level of TRACE. I tried using different threshold settings but with no success.
# 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() );
回答1:
From the log4perl FAQ:
I want to log ERROR and WARN messages to different files! How can I do that?
Let's assume you wanted to have each logging statement written to a different file, based on the statement's priority. Messages with priority WARN are supposed to go to /tmp/app.warn, events prioritized as ERROR should end up in /tmp/app.error.
Now, if you define two appenders AppWarn and AppError and assign them both to the root logger, messages bubbling up from any loggers below will be logged by both appenders because of Log4perl's message propagation feature. If you limit their exposure via the appender threshold mechanism and set AppWarn's threshold to WARN and AppError's to ERROR, you'll still get ERROR messages in AppWarn, because AppWarn's WARN setting will just filter out messages with a lower priority than WARN -- ERROR is higher and will be allowed to pass through.
What we need for this is a Log4perl Custom Filter, available with Log::Log4perl 0.30.
Both appenders need to verify that the priority of the oncoming messages exactly matches the priority the appender is supposed to log messages of. To accomplish this task, let's define two custom filters, MatchError and MatchWarn, which, when attached to their appenders, will limit messages passed on to them to those matching a given priority:
log4perl.logger = WARN, AppWarn, AppError
# Filter to match level ERROR
log4perl.filter.MatchError = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchError.LevelToMatch = ERROR
log4perl.filter.MatchError.AcceptOnMatch = true
# Filter to match level WARN
log4perl.filter.MatchWarn = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchWarn.LevelToMatch = WARN
log4perl.filter.MatchWarn.AcceptOnMatch = true
# Error appender
log4perl.appender.AppError = Log::Log4perl::Appender::File
log4perl.appender.AppError.filename = /tmp/app.err
log4perl.appender.AppError.layout = SimpleLayout
log4perl.appender.AppError.Filter = MatchError
# Warning appender
log4perl.appender.AppWarn = Log::Log4perl::Appender::File
log4perl.appender.AppWarn.filename = /tmp/app.warn
log4perl.appender.AppWarn.layout = SimpleLayout
log4perl.appender.AppWarn.Filter = MatchWarn
The appenders AppWarn and AppError defined above are logging to /tmp/app.warn and /tmp/app.err respectively and have the custom filters MatchWarn and MatchError attached. This setup will direct all WARN messages, issued anywhere in the system, to /tmp/app.warn (and ERROR messages to /tmp/app.error) -- without any overlaps.
Also look at the CPAN docs for log4perl and its sub modules:
http://search.cpan.org/~mschilli/Log-Log4perl-1.46/lib/Log/Log4perl/Filter.pm
来源:https://stackoverflow.com/questions/33306035/how-to-set-two-appenders-with-different-log-levels-in-loglog4perl