canspice.org

home about code feeds archives links

OSCON 2008: log4perl, by Mike Schilli

Why log? Logging can help you debug during development, go back in time and figure out what happened, measure performance, and to trace activity on live systems.

So why use Log4perl? It doesn’t have any CPAN dependencies. It’s easy to use, but scales with complexity. And it has some unique features.

There are four different kinds of remote controls: levels (log/suppress priority/level), loggers (locate it in the system), layouts (format it), and appenders (write it out).

In your code, you can set different levels by doing things like:

DEBUG "Starting up";
ERROR "Cannot open file $file";

The output layout can be modified:

Log::Log4perl->easy_init( {
  level => $DEBUG,
  layout => "%r %M %F-$L>%m%n",
});

If you’re doing more complex things like this, Schilli recommends using a config file: Log::Log4perl->init( “l4p.conf” );. The benefits of config files is that you can modify them independently from the program, modify it while the program is running, and it can be modified by people who don’t know (or can’t access) the code.

You can set Log4perl up so it looks for a specific configuration file. Not only that, you can set it up to reload that configuration file at any given frequency, which allows your logging to be modified while your program is running.

The available appenders are: Screen, File, Socket, DBI, Synchronized, RRDs, and a bunch more supplied by Log::Dispatch. You can set up rotating logfiles via Log::Dispatch::LogRotate.

Layouts support all kinds of different information, and if there’s a layout pattern that you want that’s not available, you can define new ones.

Log4perl isn’t “free” in the speed sense: 50k logged messages per second gives you some overhead in your program.

Wouldn’t it be nice if more CPAN modules were Log4perl-enabled?

Tags: , , ,

Leave a Reply

Name (required)
Mail (required)
Website
Comments