Directing significant-event messages to a separate Log::Log4perl logger

爱⌒轻易说出口 提交于 2019-12-08 03:38:26

You may use a filters:

log4perl.logger = WARN, Log1, Log2
log4perl.filter.Filter1       = sub { ... }
log4perl.filter.Filter2       = sub { ... }
log4perl.appender.Log1        = Log::Log4perl::Appender::Screen
log4perl.appender.Log1.Filter = Filter1
log4perl.appender.Log2        = Log::Log4perl::Appender::File
log4perl.appender.Log2.Filter = Filter2

Or you can write own filter packages inherited from Log::Log4perl::Filter:

log4perl.logger = WARN, Log1, Log2
log4perl.filter.Filter1       = MyApp::Log::Filter1
log4perl.filter.Filter2       = MyApp::Log::Filter2
log4perl.appender.Log1        = Log::Log4perl::Appender::Screen
log4perl.appender.Log1.Filter = Filter1
log4perl.appender.Log2        = Log::Log4perl::Appender::File
log4perl.appender.Log2.Filter = Filter2

You can achieve this by adding an additional category in your Log4perl.

Categories are also called "Loggers" in Log4perl, both refer to the same thing and these terms are used interchangeably

You can grab an additional logger like this:

my $important_logger = Log::Log4perl->get_logger("Important");

The category can have its own configuration:

log4perl.logger.Important               = TRACE, ImportantApp
log4perl.additivity.Important           = 0
log4perl.appender.ImportantApp          = Log::Log4perl::Appender::File
log4perl.appender.ImportantApp.filename = important.log

ImportantApp is the name that's used for the Appender instance, and it is configured in the two bottom lines. The first line basically means:

Send everything with loglevel TRACE or higher to the Appender named ImportantApp.

Important is the name of the logger, or the Category, that we grabbed above with the get_logger("Important").

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!