PSGI logging (Perl)

1.8k views Asked by At

Despite rather scant and unclear documentation and an effective How-To for beginners, I have grown to like PSGI and am currently using it in one of my applications. What I would like to know is how do I manage logging across a multi-node application? What is considered "best practice" regarding logging in PSGI?

1

There are 1 answers

0
Mark Stosberg On

I recommend using Plack::Middleware::AccessLog for logging accessing and Plack::Middleware::LogDispatch for custom logging. They both in turn use the popular Log::Dispatch module.

The LogDispatch Middleware docs do not currently show you to how to use the logging object once it is set up. Here's an example:

my $app = sub {
    my $env = shift;
    $env->{'psgix.logger'}->({ level => "debug", message => "This is debug" });
    return [ 200, [], [] ];
};

To address the multi-node concern, you could then use Log::Dispatch::Syslog which would send logging to rsyslog which could in turn to pass the log data on to another rsyslog server. In this way, all the nodes can log to a single central logging server.

With the flexibility of Log::Dispatch, you also have the option to log both locally and remotely for redundancy if you like. The logs sent to the central server could be considered primary, and the logging done locally could be considered backup in case the central log server is down for a bit.

Using a central log server has several advantages:

  1. You can 'grep' your logs across the entire cluster in one place.
  2. Log rotation and analysis is simpler with one set of log files.
  3. You are better prepared to scale your cluster up and down, as you can shut a node off without worrying about losing the logging, or keeping the server on just so an overnight cron job can fire to process the logs.

I currently using Log::Dispach and Rsyslog together in this way to manage a multi-node cluster myself.