|
From: Mike S. <msc...@ao...> - 2003-10-14 06:40:06
|
Schmidtchen, Jens wrote on 10/13/2003, 11:00 PM:
> well, but specifying:
>
> log4perl.logger.app = DEBUG, A0
> log4perl.logger.app.cache = DEBUG, A1
> log4perl.logger.app.sql = DEBUG, A1
> log4perl.logger.app.executer = DEBUG, A1
> log4perl.logger.app.dbsession = DEBUG, A1
>
> does the same (logs at DEBUG level for both appenders).
It shouldn't, if you define your appender filters as we discussed.
Here's an example (replaced appenders by screen appenders for simplicity):
#!/usr/bin/perl
use Log::Log4perl qw(get_logger);
Log::Log4perl->init(\<<'EOT');
log4perl.logger.app = DEBUG, A0
log4perl.logger.app.cache = DEBUG, A1
log4perl.logger.app.sql = DEBUG, A1
log4perl.logger.app.executer = DEBUG, A1
log4perl.logger.app.dbsession = DEBUG, A1
# 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 DEBUG
log4perl.filter.MatchDebug = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchDebug.LevelToMatch = DEBUG
log4perl.filter.MatchDebug.AcceptOnMatch = true
# File-Appender
log4perl.appender.A0 = Log::Dispatch::Screen
log4perl.appender.A0.Filter = MatchDebug
log4perl.appender.A0.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.A0.layout.ConversionPattern = %d %p> %F{1}:%L %M - %m%n
# DB-Appender
log4perl.appender.A1 = Log::Log4perl::Appender::Screen
log4perl.appender.A1.Filter = MatchError
log4perl.appender.A1.layout = Log::Log4perl::Layout::SimpleLayout
EOT
my $log = get_logger("app.cache");
$log->error("error msg");
$log->debug("debug msg");
__END__
Let's check the output:
ERROR - error msg
2003/10/13 23:14:01 DEBUG> t:38 main:: - debug msg
The different layouts show that the error message is logged only by the
A0 appender, the debug message is logged only by the A1 appender.
> is it important to
> mention that "app" is the perl script (*.pl) started at command line
> and "cache", "sql", "executer" and "dbsession" are classes (*.pm)
> from different
> packages (directories) which "app" just uses? they are *NOT*
> sub-classes of
> "app" but super-classes of several other classes. each class creates
> ist own
> logger, e.g. my $logger = get_logger('app.executer').
It doesn't matter how your classes relate to each other, only how you
define your logger categories.
> my goal in general is to log INFO and ERROR messages through the db
> appender
> (to let my program leave information to its callers). furthermore i
> want to
> provide an optional second "channel" for debugging purposes (through file
> appender) - logging each and every message (including INFO and ERROR). an
> additional task of the file logger is to catch errors during
> initialization
> of db logging too.
This is different from what I interpreted your previous request to be.
So, you're saying you want to send all (system-wide) INFO+up messages to
A0 and DEBUG-only to A1 (as long as the appropriate loggers are firing,
of course)? Then you don't need to attach appenders to your sub-category
loggers, only to the top one. Also, you need two filters, one levelMatch
(to match DEBUG) and one levelRange (INFO through FATAL).
Try this:
use Log::Log4perl qw(get_logger);
Log::Log4perl->init(\<<'EOT');
log4perl.logger.app = DEBUG, A0, A1
log4perl.logger.app.cache = DEBUG
log4perl.logger.app.sql = DEBUG
log4perl.logger.app.executer = DEBUG
log4perl.logger.app.dbsession = DEBUG
# Filter to let INFO+up through
log4perl.filter.info_and_up = Log::Log4perl::Filter::LevelRange
log4perl.filter.info_and_up.LevelMin = INFO
log4perl.filter.info_and_up.LevelMax = FATAL
log4perl.filter.info_and_up.AcceptOnMatch = true
# Filter to match level DEBUG
log4perl.filter.MatchDebug = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchDebug.LevelToMatch = DEBUG
log4perl.filter.MatchDebug.AcceptOnMatch = true
# File-Appender
log4perl.appender.A0 = Log::Dispatch::Screen
log4perl.appender.A0.Filter = MatchDebug
log4perl.appender.A0.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.A0.layout.ConversionPattern = %d %p> %F{1}:%L %M - %m%n
# DB-Appender
log4perl.appender.A1 = Log::Log4perl::Appender::Screen
log4perl.appender.A1.Filter = info_and_up
log4perl.appender.A1.layout = Log::Log4perl::Layout::SimpleLayout
EOT
my $log = get_logger("app.cache");
$log->error("error msg");
$log->debug("debug msg");
> during my test i 've figured out another challenge: if the db appender is
> unable to write to db (lets say due to violated constraints) it throws
> messages to STDERR. these messages get neither handled nor logged. i
> want to
> log them through my file appender and stop execution ...
I'll hand this one over to Kevin, he's the DB appender guru :).
--
-- Mike
Mike Schilli
m...@pe...
|