File: Log4perl.pm

package info (click to toggle)
libplack-perl 1.0047-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,480 kB
  • sloc: perl: 5,287; python: 6; makefile: 4
file content (106 lines) | stat: -rw-r--r-- 2,103 bytes parent folder | download | duplicates (5)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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);
    }

    # NOTICE: if category = '0' you must not change it by '' (root logger)
    $self->logger( Log::Log4perl->get_logger( defined $self->category ? $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

  my $app = sub {
      my $env =  shift;

      $env->{'psgix.logger'}({ level => 'error', message => 'Hi' });

      return [
          '200',
          [ 'Content-Type' => 'text/plain' ],
          [ "Hello World" ],
      ];
  };


  # Use your own Log4perl configuration
  use Log::Log4perl;
  Log::Log4perl::init('/path/to/log4perl.conf');

  builder {
      # tell the logger to log with 'plack' category
      enable "Log4perl", category => "plack";
      $app;
  }


  # Configure with Log4perl middleware options
  builder {
      enable "Log4perl", category => "plack", conf => '/path/to/log4perl.conf';
      $app;
  }

=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> for a
given category.

=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