From 7ef963e74f44f3db29bb15d440025700880ea020 Mon Sep 17 00:00:00 2001 From: Patrick Donelan Date: Tue, 13 Apr 2010 17:27:18 -0400 Subject: [PATCH] Logging fallback --- README | 2 +- benchmark.pl | 12 +++++++----- eg/dev.localhost.localdomain.fcgi | 2 +- lib/WebGUI/Middleware/Session.pm | 6 ++++++ lib/WebGUI/Session/ErrorHandler.pm | 19 ++++++++++++++++++- 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/README b/README index 1854a0459..62971c023 100644 --- a/README +++ b/README @@ -2,7 +2,7 @@ This is the PSGI branch of WebGUI8 Currently, the best performance is achieved via: - plackup -E none -s Starman --workers 10 + plackup -E none -s Starman --workers 10 --disable-keepalive You can benchmark your server via: diff --git a/benchmark.pl b/benchmark.pl index e269ba619..fa71b1dc4 100755 --- a/benchmark.pl +++ b/benchmark.pl @@ -1,17 +1,19 @@ # Little script used to run benchmarks against dev.localhost.localdomain # # To profile, run "perl -d:NYTProf benchmark.pl" -use Devel::Leak::Object qw(GLOBAL_bless); -$Devel::Leak::Object::TRACKSOURCELINES = 1; use lib '/data/WebGUI/lib'; use WebGUI; use Plack::Test; +use Plack::Builder; use HTTP::Request::Common; my $wg = WebGUI->new( root => '/data/WebGUI', site => 'dev.localhost.localdomain.conf' ); -my $app = $wg->psgi_app; +my $app = builder { + enable '+WebGUI::Middleware::Session', config => $wg->config; + $wg; +}; test_psgi $app, sub { my $cb = shift; - my $res = $cb->( GET "/" ); -} for 1..100; \ No newline at end of file + $cb->( GET "/" ) for 1..1000; +}; \ No newline at end of file diff --git a/eg/dev.localhost.localdomain.fcgi b/eg/dev.localhost.localdomain.fcgi index 431274292..ca633fef5 100755 --- a/eg/dev.localhost.localdomain.fcgi +++ b/eg/dev.localhost.localdomain.fcgi @@ -1,5 +1,5 @@ #!/usr/bin/perl use Plack::Server::FCGI; -my $app = Plack::Util::load_psgi("/data/WebGUI/etc/dev.localhost.localdomain.psgi"); +my $app = Plack::Util::load_psgi("../app.psgi"); Plack::Server::FCGI->new->run($app); diff --git a/lib/WebGUI/Middleware/Session.pm b/lib/WebGUI/Middleware/Session.pm index b847adc9d..3f70cf908 100644 --- a/lib/WebGUI/Middleware/Session.pm +++ b/lib/WebGUI/Middleware/Session.pm @@ -8,6 +8,7 @@ use Plack::Middleware::StackTrace; use Plack::Middleware::Debug; use WebGUI::Middleware::HTTPExceptions; use Plack::Middleware::ErrorDocument; +use Plack::Middleware::SimpleLogger; use Plack::Util::Accessor qw( config error_docs ); @@ -33,6 +34,11 @@ sub call { my $app = $self->app; my $config = $self->config or die 'Mandatory config parameter missing'; + + # Logger fallback + if (!$env->{'psgix.logger'}) { + $app = Plack::Middleware::SimpleLogger->wrap( $app ); + } my $session = try { $env->{'webgui.session'} = WebGUI::Session->open( $config->getWebguiRoot, $config, $env ); diff --git a/lib/WebGUI/Session/ErrorHandler.pm b/lib/WebGUI/Session/ErrorHandler.pm index f14713f51..a2ad0e913 100644 --- a/lib/WebGUI/Session/ErrorHandler.pm +++ b/lib/WebGUI/Session/ErrorHandler.pm @@ -274,7 +274,24 @@ Returns a reference to the logger. sub getLogger { my $self = shift; - return $self->session->request->logger; + if ($self->session->request) { + return $self->session->request->logger; + } else { + + # Thanks to Plack, wG has been decoupled from Log4Perl + # However when called outside a web context, we currently still fall back to Log4perl + # (pending a better idea) + if (!$self->{_logger}) { + Log::Log4perl->init_once( $self->session->config->getWebguiRoot."/etc/log.conf" ); + my $logger = Log::Log4perl->get_logger($self->session->config->getFilename); + $self->{_logger} = sub { + my $args = shift; + my $level = $args->{level}; + $logger->$level($args->{message}); + }; + } + return $self->{_logger}; + } }