Re: Plack so far
User:
mcholste
Date: 5/1/2010 10:13 am
Date: 5/1/2010 10:13 am
Views: 659
Rating: 0
Rating: 0
Answering my own question regarding logging in PSGI/Plack:
The Plack::Request object will provide a "logger" access method which
is a wrapper around the env variable psgi.logger code ref, so if you
do this in builder:
# myapp.psgi
builder {
enable "Log4perl", category => "myapp";
myapp->to_app;
}
Then you can access it in your app where you've inherited from Plack::Component:
#myapp.pm
use Plack::Component;
sub call {
my ($self, $env) = @_;
my $req_obj = new Plack::Request($env);
$req_obj->logger->({ level => 'debug', message => "This is a debug
message" }); # this will use Log4perl as enabled in .psgi
# create and return response
}
Ok, that's all well and good, but here's an issue. I have Moose roles
setup that require a logger. It would be great for that logger to be
the logger that gets passed around in each one of my PSGI requests so
that I don't have to worry about that part. Unfortunately, since
Plack only passes a coderef, not an object, I can't use that Log4perl
coderef in any Moose roles, which means I have to create a second
Log4perl object to give to the roles. I could of course change my
roles to require a logging coderef or something like that, but that
would eliminate a lot of functionality.
There's also the issue of a role having a required object property
when the object property is set during operation, not construction.
Specifically, the logger coderef wouldn't get set until the sub "call"
is run instead of at creation time, so "logger" could not be required,
and therefore any roles it uses could not require a logger (which is
what I want).
So, any advice on integrating a consolidated logging system in with
Plack as well as the various roles your Plack apps will use?
--Martin
The Plack::Request object will provide a "logger" access method which
is a wrapper around the env variable psgi.logger code ref, so if you
do this in builder:
# myapp.psgi
builder {
enable "Log4perl", category => "myapp";
myapp->to_app;
}
Then you can access it in your app where you've inherited from Plack::Component:
#myapp.pm
use Plack::Component;
sub call {
my ($self, $env) = @_;
my $req_obj = new Plack::Request($env);
$req_obj->logger->({ level => 'debug', message => "This is a debug
message" }); # this will use Log4perl as enabled in .psgi
# create and return response
}
Ok, that's all well and good, but here's an issue. I have Moose roles
setup that require a logger. It would be great for that logger to be
the logger that gets passed around in each one of my PSGI requests so
that I don't have to worry about that part. Unfortunately, since
Plack only passes a coderef, not an object, I can't use that Log4perl
coderef in any Moose roles, which means I have to create a second
Log4perl object to give to the roles. I could of course change my
roles to require a logging coderef or something like that, but that
would eliminate a lot of functionality.
There's also the issue of a role having a required object property
when the object property is set during operation, not construction.
Specifically, the logger coderef wouldn't get set until the sub "call"
is run instead of at creation time, so "logger" could not be required,
and therefore any roles it uses could not require a logger (which is
what I want).
So, any advice on integrating a consolidated logging system in with
Plack as well as the various roles your Plack apps will use?
--Martin