summary refs log tree commit diff
path: root/lib/Plack/Middleware/Debug/Dancer/Logger.pm
blob: 03e5b03c502fa2828b0764b96c4ef63595ccd9cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package Plack::Middleware::Debug::Dancer::Logger;

# ABSTRACT: Log message from you Dancer's application

use strict;
use warnings;
use parent qw(Plack::Middleware::Debug::Base);
use Dancer::Logger;
use Class::Method::Modifiers qw(install_modifier);

# XXX Not thread/Coro/AE safe. Should use $c->env or something
my $psgi_env;
install_modifier 'Dancer::Logger', 'around', 'warning' => sub {
    my $orig = shift;
    my $self = shift;
    _add_log( 'warning', $_[0] );
    $self->$orig(@_);
};

install_modifier 'Dancer::Logger', 'around', 'error' => sub {
    my $orig = shift;
    my $self = shift;
    _add_log( 'error', $_[0] );
    $self->$orig(@_);
};

install_modifier 'Dancer::Logger', 'around', 'debug' => sub {
    my $orig = shift;
    my $self = shift;
    _add_log( 'debug', $_[0] );
    $self->$orig(@_);
};

sub _add_log {
    my ( $level, $msg ) = @_;
    push @{ $psgi_env->{'plack.middleware.dancer_log'} }, $level, $msg;
}

sub run {
    my ( $self, $env, $panel ) = @_;
    $psgi_env = $env;

    return sub {
        my $res = shift;

        $panel->title('Dancer::Logger');
        $panel->nav_subtitle('Dancer::Logger');
        my $logs = delete $env->{'plack.middleware.dancer_log'};
        $panel->content( sub { $self->render_list_pairs($logs) } );
        $psgi_env = undef;
    };
}

1;