File Coverage

blib/lib/Kelp/Module/Logger.pm
Criterion Covered Total %
statement 39 39 100.0
branch 2 4 50.0
condition n/a
subroutine 11 11 100.0
pod 1 3 33.3
total 53 57 92.9


line stmt bran cond sub pod time code
1             package Kelp::Module::Logger;
2              
3 2     2   1525 use Kelp::Base 'Kelp::Module';
  2         6  
  2         17  
4              
5 2     2   14 use Carp;
  2         3  
  2         150  
6 2     2   1476 use Log::Dispatch;
  2         421593  
  2         86  
7 2     2   1290 use Time::Piece;
  2         263416  
  2         19  
8 2     2   324 use Data::Dumper;
  2         8  
  2         1649  
9              
10             attr logger => undef;
11             attr date_format => '%Y-%m-%d %T';
12             attr log_format => '%s - %s - %s';
13              
14             sub _logger
15             {
16 2     2   4 my ($self, %args) = @_;
17              
18 2         29 return Log::Dispatch->new(%args);
19             }
20              
21             sub load_configuration
22             {
23 2     2 0 6 my ($self, %args) = @_;
24              
25 2         6 for my $field (qw(date_format log_format)) {
26             $self->$field(delete $args{$field})
27 4 50       17 if $args{$field};
28             }
29              
30 2         9 return %args;
31             }
32              
33             sub build
34             {
35 2     2 1 6 my ($self, %args) = @_;
36              
37             # load module config
38 2         11 %args = $self->load_configuration(%args);
39              
40             # load logger with the rest of the config
41 2         9 $self->logger($self->_logger(%args));
42              
43             # Build the registration hash
44             my %LEVELS = map {
45 2         8 my $level = $_;
  6         10  
46             $level => sub {
47 4     4   220 shift;
48 4         17 $self->message($level, @_);
49 6         36 };
50             } qw(debug info error);
51              
52             # Register a few levels
53 2         25 $self->register(%LEVELS);
54              
55             # Also register the message method as 'logger'
56             $self->register(
57             logger => sub {
58 2     2   67 shift;
59 2         8 $self->message(@_);
60             }
61 2         14 );
62             }
63              
64             sub message
65             {
66 6     6 0 20 my ($self, $level, @messages) = @_;
67 6         25 my $date = localtime->strftime($self->date_format);
68              
69 6         610 local $Data::Dumper::Sortkeys = 1;
70 6         17 for my $message (@messages) {
71 6 50       26 $message = sprintf $self->log_format,
72             $date,
73             $level,
74             (ref $message ? Dumper($message) : $message),
75             ;
76              
77 6         23 $self->logger->log(level => $level, message => $message);
78             }
79             }
80              
81             1;
82              
83             __END__
84              
85             =pod
86              
87             =head1 NAME
88              
89             Kelp::Module::Logger - Logger for Kelp applications
90              
91             =head1 SYNOPSIS
92              
93             # conf/config.pl
94             {
95             modules => ['Logger'],
96             modules_init => {
97             Logger => {
98             outputs => [
99             [ 'Screen', min_level => 'debug', newline => 1 ],
100             ]
101             },
102             },
103             }
104              
105             # lib/MyApp.pm
106             sub run {
107             my $self = shift;
108             my $app = $self->SUPER::run(@_);
109             ...;
110             $app->info('Kelp is ready to rock!');
111             return $app;
112             }
113              
114              
115             =head1 DESCRIPTION
116              
117             This module provides an log interface for Kelp web application. It uses
118             L<Log::Dispatch> as underlying logging module.
119              
120             =head1 CONFIGURATION
121              
122             All module's configuration is passed to L<Log::Dispatch>, so consult its docs
123             for details. In addition, following keys can be configured which change how the
124             module behaves:
125              
126             =head2 date_format
127              
128             A string in L<strftime
129             format|https://www.unix.com/man-page/FreeBSD/3/strftime/> which will be used to
130             generate the date.
131              
132             By default, value C<'%Y-%m-%d %T'> is used.
133              
134             =head2 log_format
135              
136             A string in L<sprintf format|https://perldoc.perl.org/functions/sprintf> which
137             will be used to generate the log. Three string values will be used in this
138             string, in order: date, log level and the message itself.
139              
140             By default, value C<'%s - %s - %s'> is used.
141              
142             =head1 REGISTERED METHODS
143              
144             =head2 debug
145              
146             =head2 info
147              
148             =head2 error
149              
150             =head2 logger
151              
152             C<< $app->logger(info => 'message') >> is equivalent to C<< $app->info('message') >>.
153              
154             =head1 SEE ALSO
155              
156             L<Kelp::Module::Logger::Simple> - always dumps to standard output
157              
158             =cut
159