diff options
author | franck cuny <franck@lumberjaph.net> | 2010-04-17 13:23:08 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-04-17 13:23:08 +0200 |
commit | 2d615db13d2dbe9f134d8cc59f0b770282bf35b6 (patch) | |
tree | 9ee716dc599f12896150d1d38978f99b3724ba00 | |
parent | initial commit (diff) | |
download | dancer-logger-psgi-2d615db13d2dbe9f134d8cc59f0b770282bf35b6.tar.gz |
log to psgix.logger
-rw-r--r-- | Makefile.PL | 4 | ||||
-rw-r--r-- | lib/Dancer/Logger/PSGI.pm | 34 | ||||
-rw-r--r-- | t/01_basic.t | 50 |
3 files changed, 84 insertions, 4 deletions
diff --git a/Makefile.PL b/Makefile.PL index 57f0134..df00b28 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -3,7 +3,9 @@ name 'Dancer-Logger-PSGI'; all_from 'lib/Dancer/Logger/PSGI.pm'; readme_from 'lib/Dancer/Logger/PSGI.pm'; -# requires ''; +requires 'Plack'; +requires 'Plack::Middleware::ConsoleLogger'; +requires 'Test::TCP'; tests 't/*.t'; diff --git a/lib/Dancer/Logger/PSGI.pm b/lib/Dancer/Logger/PSGI.pm index f6e6dd4..797def4 100644 --- a/lib/Dancer/Logger/PSGI.pm +++ b/lib/Dancer/Logger/PSGI.pm @@ -4,20 +4,46 @@ use strict; use warnings; our $VERSION = '0.01'; +use Dancer::SharedData; +use base 'Dancer::Logger::Abstract'; + +sub init {} + +sub _log { + my ( $self, $level, $message ) = @_; + my $request = Dancer::SharedData->request; + if ( $request->{env}->{'psgix.logger'} ) { + $request->{env}->{'psgix.logger'} + ->( { level => $level, message => $message } ); + } +} + 1; __END__ =head1 NAME -Dancer::Logger::PSGI - +Dancer::Logger::PSGI - PSGI logger handler for Dancer =head1 SYNOPSIS - use Dancer::Logger::PSGI; +In your Dancer's configuration file: + + logger: PSGI + +In your application + + warning "this is a warning" + +In your app.psgi + + $app = builder { enable "ConsoleLogger"; $app; } + +With L<Plack::Middleware::ConsoleLogger>, all your log will be send to the javascript console of your browser. =head1 DESCRIPTION -Dancer::Logger::PSGI is +This class is an interface between your Dancer's application and B<psgix.logger>. Message will be logged in whatever logger you decided to use in your L<Plack> handler. If no logger is defined, nothing will be logged. =head1 AUTHOR @@ -25,6 +51,8 @@ franck cuny E<lt>franck@lumberjaph.netE<gt> =head1 SEE ALSO +L<Plack::Middleware::ConsoleLogger>, L<Plack::Middleware::NullLogger>, L<Plack::Middleware::Log4perl>. + =head1 LICENSE This library is free software; you can redistribute it and/or modify diff --git a/t/01_basic.t b/t/01_basic.t new file mode 100644 index 0000000..cd93d94 --- /dev/null +++ b/t/01_basic.t @@ -0,0 +1,50 @@ +use Test::More import => ['!pass']; +use strict; +use warnings; + +plan skip_all => 'LWP::UserAgent is needed to run this test' + unless Dancer::ModuleLoader->load('LWP::UserAgent'); +plan skip_all => 'Plack::Middleware::ConsoleLogger is needed to run this test' + unless Dancer::ModuleLoader->load('Plack::Middleware::ConsoleLogger'); + +use Plack::Loader; +use Plack::Builder; +use Test::TCP; + +Test::TCP::test_tcp( + client => sub { + my $port = shift; + my $ua = LWP::UserAgent->new; + + my $request = HTTP::Request->new(GET => "http://127.0.0.1:$port/"); + my $res = $ua->request($request); + ok($res->is_success, "server responded"); + like($res->content, qr/this is a warning/, "log message send"); + }, + server => sub { + my $port = shift; + + use Dancer; + use Dancer::Config 'setting'; + + setting apphandler => 'PSGI'; + setting port => $port; + setting access_log => 0; + setting logger => "PSGI"; + + get '/' => sub { + warning "this is a warning"; + return "<html><body>this is a test</body></html>"; + }; + + my $app = sub { + my $env = shift; + my $request = Dancer::Request->new($env); + Dancer->dance($request); + }; + $app = builder { enable "ConsoleLogger"; $app }; + Plack::Loader->auto(port => $port)->run($app); + }, +); + +done_testing; |