File Coverage

blib/lib/Plack/Middleware/PyeLogger.pm
Criterion Covered Total %
statement 18 30 60.0
branch 0 6 0.0
condition 0 2 0.0
subroutine 6 9 66.6
pod 2 2 100.0
total 26 49 53.0


line stmt bran cond sub pod time code
1             package Plack::Middleware::PyeLogger;
2              
3             # ABSTRACT: Use Pye as a Plack logger
4              
5 1     1   21117 use parent qw/Plack::Middleware/;
  1         389  
  1         7  
6 1     1   14883 use strict;
  1         2  
  1         27  
7 1     1   4 use warnings;
  1         5  
  1         19  
8              
9 1     1   4 use Carp;
  1         1  
  1         62  
10 1     1   3 use Plack::Util::Accessor qw/logger backend opts/;
  1         2  
  1         3  
11 1     1   497 use Pye;
  1         3992  
  1         192  
12              
13             our $VERSION = "2.000001";
14             $VERSION = eval $VERSION;
15              
16             =head1 NAME
17              
18             Plack::Middleware::PyeLogger - Use Pye as a Plack logger
19              
20             =head1 SYNOPSIS
21              
22             builder {
23             enable 'PyeLogger', backend => 'MongoDB', opts => \%opts;
24             $app;
25             };
26              
27             =head1 DESCRIPTION
28              
29             This L middleware sets L as a logger for your Plack applications (C).
30             It differs from "normal" Plack loggers in that the C subroutine takes a hash-ref of
31             a different format:
32              
33             =over
34              
35             =item * B - the text of the message (this is standard to all loggers)
36              
37             =item * B - the ID of the session (this is required for this logger)
38              
39             =item * B - an optional hash-ref of data to attach to the message
40              
41             =back
42              
43             Also, the C key is ignored, as C has no log levels.
44              
45             =head1 METHODS
46              
47             This module implements the following methods, as required by L/L.
48              
49             =head2 prepare_app()
50              
51             Generates an instance of L.
52              
53             =cut
54              
55             sub prepare_app {
56 0     0 1   my $self = shift;
57              
58 0 0         $self->backend('MongoDB')
59             unless $self->backend;
60              
61 0 0         $self->opts({})
62             unless $self->opts;
63              
64 0           $self->logger(Pye->new($self->backend, %{$self->opts}));
  0            
65             }
66              
67             =head2 call( \%env )
68              
69             Creates the C subroutine for your app.
70              
71             =cut
72              
73             sub call {
74 0     0 1   my($self, $env) = @_;
75              
76             $env->{'psgix.logger'} = sub {
77 0     0     my $args = shift;
78              
79             croak "You must provide the session_id to the logger"
80 0 0         unless $args->{session_id};
81              
82 0   0       $args->{message} ||= '';
83              
84 0           $self->logger->log($args->{session_id}, $args->{message}, $args->{data});
85 0           };
86              
87 0           $self->app->($env);
88             }
89              
90             =head1 CONFIGURATION
91              
92             You need to pass the C backend to use (e.g. C for L, which is the
93             default for backwards compatibility reasons), and optionally a hash-ref of options. These can
94             be anything the respective backend constructor accepts. For example:
95              
96             builder {
97             enable 'PyeLogger',
98             backend => 'MongoDB',
99             opts => { host => 'mongodb://logserver:27017', log_coll => 'logsssss!!!!!!' };
100             $app;
101             };
102              
103             =head1 BUGS AND LIMITATIONS
104              
105             Please report any bugs or feature requests to
106             C, or through the web interface at
107             L.
108              
109             =head1 SUPPORT
110              
111             You can find documentation for this module with the perldoc command.
112              
113             perldoc Plack::Middleware::PyeLogger
114              
115             You can also look for information at:
116              
117             =over 4
118            
119             =item * RT: CPAN's request tracker
120            
121             L
122            
123             =item * AnnoCPAN: Annotated CPAN documentation
124            
125             L
126            
127             =item * CPAN Ratings
128            
129             L
130            
131             =item * Search CPAN
132            
133             L
134            
135             =back
136              
137             =head1 SEE ALSO
138              
139             L, L, L.
140              
141             =head1 AUTHOR
142              
143             Ido Perlmuter
144              
145             =head1 LICENSE AND COPYRIGHT
146              
147             Copyright (c) 2013, Ido Perlmuter C<< ido@ido50.net >>.
148              
149             This module is free software; you can redistribute it and/or
150             modify it under the same terms as Perl itself, either version
151             5.8.1 or any later version. See L
152             and L.
153              
154             The full text of the license can be found in the
155             LICENSE file included with this module.
156              
157             =head1 DISCLAIMER OF WARRANTY
158              
159             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
160             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
161             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
162             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
163             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
164             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
165             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
166             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
167             NECESSARY SERVICING, REPAIR, OR CORRECTION.
168              
169             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
170             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
171             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
172             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
173             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
174             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
175             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
176             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
177             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
178             SUCH DAMAGES.
179              
180             =cut
181              
182             1;
183             __END__