Perlish – Logging

Posted by Joe Topjian on March 28, 2011 under Development | Be the First to Comment

Introduction

Logging is an essential tool and feature for System Administrators. With Perl, logging can be as simple as using print, die, or warn, or more advanced such as logging to syslog, a private log file, or even a database. This article quickly covers the more advanced logging.

Table of Contents

Sys::Syslog

Sys::Syslog is a core Perl module. It provides a standard and simple interface to logging to your (*nix-base) server’s syslog.

Here’s a simple example:

#!/usr/bin/env perl
use strict;
use warnings;
use Sys::Syslog;

my $ident = $0; 
openlog($ident, 'pid', 'user');
syslog('info', 'Testing syslog logging');
closelog();

After running this script, you can find the following in /var/log/messages (or other appropriate log file):

Mar 25 13:36:28 server syslog.pl[20075]: Testing syslog logging

The documentation for Sys::Syslog is very well written and I highly encourage reading it — especially “The Rules of Sys::Syslog”.

Log::Log4perl

Log::Log4perl is an extremely robust logging module for Perl. Its use can range from simple logging features to full-fledged application framework logging (including to remote databases). I’ve never been on a project where Log::Log4perl was needed, but wanted to include it in this article as it seems to be the de-facto logging module for Perl.

Simple Use

This example shows how to print logging information to the screen for everything from the INFO level and up:

#!/usr/bin/env perl
use strict;
use warnings;
use Log::Log4perl qw/:easy/;

Log::Log4perl->easy_init($INFO);
my $logger = get_logger();
$logger->info("Testing Log4perl logging");

When this script is run, the following is printed on the screen:

2011/03/25 13:45:52 Testing Log4perl logging

Logging to a Logfile

This next script writes all logs to a logfile rather than on the screen. As you can see, there’s a bit more involved than the previous example — namely the need to begin to configure Log::Log4perl:

#!/usr/bin/env perl
use strict;
use warnings;
use Log::Log4perl;

my $log_conf = q/  
    log4perl.category = INFO, Logfile
     
    log4perl.appender.Logfile = Log::Log4perl::Appender::File 
    log4perl.appender.Logfile.filename = log4perl.log
    log4perl.appender.Logfile.mode = write 
    log4perl.appender.Logfile.layout = Log::Log4perl::Layout::SimpleLayout     
/;

Log::Log4perl::init( \$log_conf );
my $logger = Log::Log4perl::get_logger();

$logger->error("Testing Log4perl logging");

Once this script is run, you should have a log file called log4perl.log in the same directory. The contents will read:

ERROR - Testing Log4perl logging

Logging to Syslog

Log::Log4perl is also able to log to syslog, although it requires an extra module: Log::Dispatch::Syslog. Unless you are doing other logging with Log::Log4perl, I would recommend using the standard Sys::Syslog module:

#!/usr/bin/env perl
use strict;
use warnings;
use Log::Log4perl;

my $log_conf = q/  
    log4perl.category = INFO, SYSLOG
     
    log4perl.appender.SYSLOG = Log::Dispatch::Syslog
    log4perl.appender.SYSLOG.min_level = warn 
    log4perl.appender.SYSLOG.ident = log4perl.pl
    log4perl.appender.SYSLOG.facility = user
    log4perl.appender.SYSLOG.layout = Log::Log4perl::Layout::SimpleLayout
/;

Log::Log4perl::init( \$log_conf );
my $logger = Log::Log4perl::get_logger();

$logger->warn("Testing Log4perl logging");

Once this script is run, you can find the following message in /var/log/messages:

Mar 25 14:00:02 server log4perl.pl: WARN - Testing Log4perl logging

Log::Log4perl Resources

I found the following links to be great resources for Log::Log4perl:

Conclusion

This article covered two Perl modules that deal with advanced Perl logging, Sys::Syslog and Log::Log4perl.

Add A Comment