Logging For Ever!

Logging is cool! When weirdness happens it's one of the first places we look.

The amount of data written to the logs when an application is 'debug-mode' can be overwhelming. Turning on low-level-logging on a system under heavy load can cause it to slow down to a crawl or even crash.

Log::Log4perl is a pure Perl port of the widely popular Apache/Jakarta log4j library [3] (so I'm told anyway, as I would rather drink bath water than code in Java) for Java, a project made public in 1999, which has been actively supported and enhanced by a team around head honcho Ceki Gülcü during the years.

LOG LEVELS

No great surprises here. Standard logging levels.

DEBUG

INFO

WARN

ERROR

FATAL

Why is log4perl “The Sh$%t”?

You can point the log messages wherever you want. STDOUT, file, Your friends at parties. You can send to more than one place.

It's easy to change the log level without having to do any ninja find-and-replace operations throughout your whole code base.

The get_logger() method means you don't have to pass the logger object around as a parameter to exterior methods.

You can localise logging level changes with Categories.

Logger Categories

These are the labels for logger objects.

It's not compulsory to use the perl-ish class syntax but makes perfect sense as it keeps your labels in line with various parts of the code.

log4perl is clever with inheritance between categories. If logging level set for 'Groceries::Food' then the 'Groceries::Food::Consume' class gets this debug level unless explicitly stated elsewhere.

Appenders

These dictate where the log output gets directed.

Makes use of all the appenders designed in the Log::Dispatch namespace.

There can be one or many of these appenders (you can send to a file as well as STDERR, even to yourself by email).

Expensive Log Statements

What if we don't want to execute the meat and potatoes of the log statement unless necessary? (It may be resolving the meaning of life which can take more than a trivial amount of microseconds)

if ( $logger->is_debug ) { #meaning of life algorithm}

Wish list: Wouldn't it be cool if there was a way to do this in perl i.e. not execure the contents of the parameter passed to log method if not necessary ... Meaning we wouldn't have to do the above check.

Sources and Links

log4perl on perl.com (Open Office Impress is shite and wouldn't let me paste the URL or anything else in here today as it was clearly having a bad hair day).

log4perl on sourceforge (link missing as per above)

log4perl on CPAN

Log:Dispatch on CPAN

http://log4perl.sourceforge.net

http://log4perl.sourceforge.net/releases/Log-Log4perl/docs/html/Log/Log4perl.html

http://search.cpan.org/author/DROLSKY/Log-Dispatch-2.01/Dispatch.pm

2006-10-04T10:36:50