Mon Mar 16 19:30:00 2015

mstpan 16 - Logging

Because the evidence that you're an idiot has to be retained somehow.

warn()

No, really, look, if it's a small piece of code like a script, then

warn "Some information for STDERR\n";

is perfectly fine. Overcomplicating things bad.

Log::Dispatch

Log::Dispatch is a nice set of composable modules to bolt together pretty much whatever logging setup you want from code.

It'll talk to files (rotated or otherwise), syslog, and there's assorted modules on CPAN letting it to talk to pretty much anything else you might like (Log::Dispatch::DBI being an interesting example).

Log::Log4perl

Heavily inspired by log4j, Log::Log4perl is a little heavy, a little baroque ... and exceedingly capable, with a config file system that can set up pretty much anything you want if you stare at the documentation for long enough.

Maximum overkill in a good way.

Log::Any

If you're trying to write logging-system-agnostic code, this is a good option - a very thin wrapper object that the application then registers a dispatcher with.

Said dispatcher can use Log::Dispatch - or Log::Log4perl - but can also be one of the built in ones, which handle stderr, stdout and basic file logging.

Log::Contextual

Designed to avoid needing to pass a logging object around at all, Log::Contextual also takes code blocks rather than plain strings, so expensive debugging data collection will only get executed if the relevant log level is active.

For larger applications where some debug logging operations are expensive, this tends to be a really nice way to avoid playing pass-the-logger (or stashing it in a top-level 'my' definition or other similar ick).

Message::Passing

Not specifically a logging system, Message::Passing is designed to be a set of building blocks for messaging in general - but streaming debug or audit logs to a central server of some sort is definitely one of the things it excels at.

If what you're doing is not quite or not just logging, Message::Passing is well worth a look.

tl;dr

Log::Dispatch for snap-together-in-code, Log::Log4perl for snap-together-from-config, Message::Passing if you're doing something crazy, and look at Log::Any and Log::Contextual so you'll remember they exist if you run into their chosen use cases later.

-- mst, out.