Plack::Middleware::LogStderr - Everything printed to STDERR sent to psgix.logger or other logger


version 1.000


Using a logger you have already configured (using Log::Dispatch as an example):

  use Log::Dispatch;
  my $logger = Log::Dispatch->new;
  $logger->add( Log::Dispatch::File->new(...) );

  builder {
      enable 'LogDispatch', logger => $logger;
      enable 'LogStderr';

Using an explicitly defined logger:

  builder {
      enable 'LogStderr', logger => sub {
          my $args = shift;

Other Options:

  enable 'LogStderr',
      log_level => 'warn',
      no_tie => 1,
      callback => sub {
          my $msg = shift;
          return "STDERR:$msg\n";


This middleware intercepts all output to STDERR and redirects it to a defined logger.

Examples where STDERR output would not typically be sent to a logger:

  print STDERR "foo";
  system('perl -e " print STDERR \'bar\'"');

This middleware uses two techniques to catch messages sent to STDERR and direct them to a logger.

The first ties the STDERR filehandle and directs all print messages to a logger. This method only works if the code printing to STDERR is aware of the Perl tied filehandle.

The second technique uses Capture::Tiny to capture everything else written to STDERR (for example any programs run using system). This method groups all STDERR output into one message. The drawback here is log messages may not be interleaved temporally with messages generated from the tied method or other calls to the logger.



A code reference for logging messages, that conforms to the psgix.logger specification. If not provided, psgix.logger is used, or the application will generate an error at runtime if there is no such logger configured.

log_level, log_level_capture

By default the log level used is 'error' use log_level to set it to another value.

Use log_level_capture if you want the default log level for captured output to be different from log_level

Make sure the log level used is valid for your logger!

callback, capture_callback, tie_callback

Callbacks that take a string and return a string.

callback is applied to all messages.
capture_callback is applied to all messages logged via the capture method.
tie_callback is applied to messages logged via the tied STDERR filehandle.


Do not tie the perl file handle STDERR to a logger. When set, all output to STDERR will be caught and logged in one message.

The benefit of this is all output sent to STDERR is in order. The drawback is all STDERR output created during a request is grouped together as one message and logged together after the request has finished processesing.



Karen Etheridge


The source code repository for Plack-Middleware-LogStderr can be found at


Alex Malek


This software is copyright (c) 2018 by Alex Malek.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.