/
Log4perl.pm
92 lines (60 loc) · 1.83 KB
/
Log4perl.pm
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package Plack::Middleware::Log4perl;
use strict;
use parent qw(Plack::Middleware);
use Plack::Util::Accessor qw(category logger conf);
use Carp ();
sub prepare_app {
my $self = shift;
if ($self->conf) {
require Log::Log4perl;
Log::Log4perl::init($self->conf);
}
$self->logger( Log::Log4perl->get_logger($self->category || '') );
}
sub call {
my($self, $env) = @_;
$env->{'psgix.logger'} = sub {
my $args = shift;
my $level = $args->{level};
local $Log::Log4perl::caller_depth
= $Log::Log4perl::caller_depth + 1;
$self->logger->$level($args->{message});
};
$self->app->($env);
}
1;
__END__
=head1 NAME
Plack::Middleware::Log4perl - Uses Log::Log4perl to configure logger
=head1 SYNOPSIS
use Log::Log4perl;
Log::Log4perl::init('/path/to/log4perl.conf');
builder {
enable "Log4perl", category => "plack";
$app;
}
# in log4perl.conf
log4perl.logger.plack = INFO, Logfile
log4perl.appender.Logfile = Log::Log4perl::Appender::File
log4perl.appender.Logfile.filename = /path/to/logfile.log
log4perl.appender.Logfile.layout = Log::Log4perl::Layout::SimpleLayout
# Or let middleware to configure log4perl
enable "Log4perl", category => "plack", conf => '/path/to/log.conf';
=head1 DESCRIPTION
Log4perl is a L<Plack::Middleware> component that allows you to use
L<Log::Log4perl> to configure the logging object, C<psgix.logger>.
=head1 CONFIGURATION
=over 4
=item category
The C<log4perl> category to send logs to. Defaults to C<''> which means
it send to the root logger.
=item conf
The configuration file path (or a scalar ref containing the config
string) for L<Log::Log4perl> to automatically configure.
=back
=head1 AUTHOR
Tatsuhiko Miyagawa
=head1 SEE ALSO
L<Log::Log4perl>
L<Plack::Middleware::LogDispatch>
=cut