1

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

MeirG
  • 333
  • 2
  • 14
  • How do you decide that it is "_significant_"? What does "_events_" mean -- as in event-driven programming, or in the generic sense, just messages (prints etc)? – zdim Apr 29 '16 at 08:00
  • I mean "events" in a very generic sense, just occurrence of something that would be important for the user to know about, e.g., an addition of a new customer. As for how I decide, it depends on the possibilities, something akin to a new level, say "NOTE" and `$logger->note( ... );` – MeirG Apr 29 '16 at 08:09
  • Given that the openness is on the decision side (in the code), perhaps using [custom filters](http://search.cpan.org/~mschilli/Log-Log4perl-1.47/lib/Log/Log4perl.pm#Custom_Filters) may help? There is a separate class, [Log::Log4Perl::Filter](http://search.cpan.org/~mschilli/Log-Log4perl-1.47/lib/Log/Log4perl/Filter.pm), for that. – zdim Apr 29 '16 at 09:01

2 Answers2

3

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

simbabque
  • 53,749
  • 8
  • 73
  • 136
2

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
Denis Ibaev
  • 2,470
  • 23
  • 29
  • I chose this answer over equally performing _"additional category"_ below, only since I prefer a single logger writing to two files. The little annoying thing with it is that the string that the filter is checking for appears in all entries. – MeirG May 03 '16 at 13:57