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

余生颓废 提交于 2019-12-23 02:49:06

问题


I need to collect significant-event messages to a separate log file. (The regular log is rather bloated, and meant for maintenance, while the user is only interested in very few events.)

The events are not level related, although all ERROR level and up events are considered "significant". Many such events are of INFO or WARN levels. Therefore, level thresholds or matching don't seem to be the answer.

Also, events are not limited to a given branch of the hierarchy. These can emanate from all over the application, so "categories" don't seem to fit either, or do they?

Edit: Currently, I have an INFO level general, maintenance logger and a WARN level screen logger

Is there a way to achieve such a logger with Log::Log4perl?

Regards

Meir


回答1:


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



回答2:


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").



来源:https://stackoverflow.com/questions/36932639/directing-significant-event-messages-to-a-separate-loglog4perl-logger

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