File Coverage

lib/Leyland/Logger.pm
Criterion Covered Total %
statement 12 17 70.5
branch 0 2 0.0
condition n/a
subroutine 4 5 80.0
pod n/a
total 16 24 66.6


line stmt bran cond sub pod time code
1             package Leyland::Logger;
2              
3             # ABSTARCT: Logging facilities for Leyland applications
4              
5 1     1   6 use Moo;
  1         3  
  1         7  
6 1     1   338 use namespace::clean;
  1         2  
  1         10  
7              
8             =head1 NAME
9              
10             Leyland::Logger - Wrapper around Plack's logging middlewares
11              
12             =head1 SYNOPSIS
13              
14             # in your app.psgi file
15             builder {
16             enable 'SomeLoggingMiddleware';
17             MyApp->to_app;
18             };
19              
20             # in your controllers
21             $c->log->debug("Some debug message");
22              
23             =head1 DESCRIPTION
24              
25             This package provides a simple wrapper around the L logging middleware
26             used by your application. An object of this class is provided to the L
27             object. Read L to learn more.
28              
29             =head1 ATTRIBUTES
30              
31             =head2 logger
32              
33             An anonymous logging subroutine. This will be the C subroutine
34             automatically created by your selected logging middleware. If you haven't selected
35             one, however, this class will create a default one that simply prints messages
36             to standard output or standard error (as appropriate).
37              
38             =cut
39              
40             has 'logger' => (
41             is => 'ro',
42             isa => sub { die "logger must be a code reference" unless ref $_[0] && ref $_[0] eq 'CODE' },
43             default => sub {
44             sub {
45             my $args = shift;
46              
47             if ($args->{level} eq 'emergency' || $args->{level} eq 'error') {
48             binmode STDERR, ":encoding(utf8)";
49             print STDERR '| [', $args->{level}, '] ', $args->{message}, "\n";
50             } else {
51             binmode STDOUT, ":encoding(utf8)";
52             print STDOUT '| [', $args->{level}, '] ', $args->{message}, "\n";
53             }
54             }
55             }
56             );
57              
58             =head1 METHODS
59              
60             This class provides methods for the following log levels:
61              
62             =over
63              
64             =item * B
65              
66             =item * B
67              
68             =item * B (with an B alias)
69              
70             =item * B
71              
72             =item * B (with a B alias)
73              
74             =item * B (with an B alias)
75              
76             =item * B (with a B and B aliases)
77              
78             =item * B
79              
80             =item * B
81              
82             =back
83              
84             All methods take the same parameters: a required C<$message> string,
85             and an optional C<\%data> hash-ref. This is meant to be used by a
86             logger such as L, so take a look at it to learn more.
87              
88             =cut
89              
90 1     1   434 no strict 'refs';
  1         2  
  1         230  
91             foreach (
92             ['trace'],
93             ['debug'],
94             ['info', 'inform'],
95             ['notice'],
96             ['warning', 'warn'],
97             ['error', 'err'],
98             ['critical', 'crit', 'fatal'],
99             ['alert'],
100             ['emergency']
101             ) {
102             my $level = $_->[0];
103              
104             *{$level} = sub {
105 0     0     my $self = shift;
106              
107 0           my $message = {
108             level => $level,
109             message => $_[0],
110             };
111 0 0         if ($_[1]) {
112 0           $message->{data} = $_[1];
113             }
114              
115 0           $self->logger->($message);
116             };
117             }
118 1     1   5 use strict 'refs';
  1         2  
  1         48  
119              
120             =head1 AUTHOR
121              
122             Ido Perlmuter, C<< >>
123              
124             =head1 BUGS
125              
126             Please report any bugs or feature requests to C, or through
127             the web interface at L. I will be notified, and then you'll
128             automatically be notified of progress on your bug as I make changes.
129              
130             =head1 SUPPORT
131              
132             You can find documentation for this module with the perldoc command.
133              
134             perldoc Leyland::Logger
135              
136             You can also look for information at:
137              
138             =over 4
139              
140             =item * RT: CPAN's request tracker
141              
142             L
143              
144             =item * AnnoCPAN: Annotated CPAN documentation
145              
146             L
147              
148             =item * CPAN Ratings
149              
150             L
151              
152             =item * Search CPAN
153              
154             L
155              
156             =back
157              
158             =head1 LICENSE AND COPYRIGHT
159              
160             Copyright 2010-2014 Ido Perlmuter.
161              
162             This program is free software; you can redistribute it and/or modify it
163             under the terms of either: the GNU General Public License as published
164             by the Free Software Foundation; or the Artistic License.
165              
166             See http://dev.perl.org/licenses/ for more information.
167              
168             =cut
169              
170             1;