Skip to content

Instantly share code, notes, and snippets.

@ktat
Created May 15, 2012 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktat/2699821 to your computer and use it in GitHub Desktop.
Save ktat/2699821 to your computer and use it in GitHub Desktop.
print IP address to error_log for psgi application
package Hoge::PSGIErrors;
use strict;
use IO::Handle;
sub new {
my $class = shift;
my $env = shift;
my %self;
$self{io} = IO::Handle->new;
$self{io}->fdopen(fileno(STDERR), 'w');
$self{env} = $env;
my $o = bless \%self, $class;
# ...
$SIG{__WARN__} = sub { $o->print(@_) };
return $o;
}
sub print {
my $self = shift;
my @msg = @_;
$self->{io}->print(map {s/^/$self->{env}->{REMOTE_ADDR} /mg; $_} @msg);
}
1;
# app.psgi
use Hoge::PSGIErrors;
builder {
# ...
enable sub {
my $app = shift;
sub {
my $env = shift;
$env->{'psgi.errors'} = Hoge::PSGIErrors->new($env);
$app->($env);
}
};
# ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment