Library IT News

Entering content area for Library IT News

blog posts

  • Using Log4Perl

    I've recently started using Log4Perl library more extensively, for both version 2 of the New List and to a more limited extent in the data feeds.  It's useful enought I thought I'd describe how I'm using it here.


    Here's a very quick summary how Log4Perl works.  A configuration file specifies one or more logs with options like the log's filename and the pattern for each message recorded in the log.   For example, I typically frequently record the time the message was record and the line number of the perl script that was running at the time.  There's some preset patterns you can use too.

    Then in the actual perl script or module you create a logger object, giving it the path to the configuration file.  At any point after you can call the logger with a message using a method that corresponds to one of the following levels: debug,info,warn,error, and fatal.   So, $logger->info("Starting file transfer from $src to $dest") will send that message to any log in the configuration file with a minimum threshold set to info or debug.  The minimum threshold of a logger essentially says "I don't want to record anything below this".  This is nice because I can change one word in the configuration file to toggle the behavior of all the scripts using those loggers.

    The levels are nice for testing, but in production probably the most useful aspect is the ability to have multiple listeners.  These listeners don't have to be text files, but can be the terminal or in an email message.  There's a library (Log::Dispatch) that contains several useful additional loggers.  This is one of the largest issues with our current system.  We are just using print statements that write into a log file.  If an error actually does occur, it can get buried in that log and I simply don't notice it quickly enough and end up not squashing the problem before it snowballs.

    The New Titles update process configuration file has a good example of the email logger.  It works well, although one change I might make in the future is to use a pattern that only states that an error happened, so not to risk leaking any sensitive information.   This isn't really a concern for the New Titles List, but it is for data feeds.
     (The actual email addressed has been mucked up, imagine a valid one where the incorrect one is).

    log4perl.appender.Mailer           = Log::Dispatch::Email::MailSender      = titles        = someemail@addressnull.fake
    log4perl.appender.Mailer.from      = somefake@addressnull.fake
    log4perl.appender.Mailer.subject   = Fatal error with New Titles List import
    log4perl.appender.Mailer.smtp      =
    log4perl.appender.Mailer.layout    = SimpleLayout
    log4perl.appender.Mailer.Threshold = FATAL
    log4perl.appender.Mailer.buffered  = 0

    To get the email part working, you'll need to add a few Perl modules in addition to Log-Log4Perl: Mail-Sender and Log-Dispatch.  These are both cpan modules and ActiveState modules, so they shouldn't be too hard to get.

    I've also been tempted to use the email-sms gateway for Verizon to send myself a short text message. For the New Titles I decided this would be unnecessary as the consequences of a fatal error are minimal, but it is probably something I'll add for the more sensitive data feeds.

    A longer description with more examples that I found useful is at  I will warn that on ActiveState Perl I had issues using the email setup recommended by the article.   Instead use the Mail::Sender I mention above.

additional blog information