Created
May 15, 2012 07:49
-
-
Save ktat/2699821 to your computer and use it in GitHub Desktop.
print IP address to error_log for psgi application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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