blib/lib/Kelp/Module/Logger.pm

filter: Statementstmt 100% 43 / 43 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 33.3% 1 / 3 totaltotal 93.4% 57 / 61 CC 5 SCAR 5.9
CC
5
Cov
96%
load_configuration
3.0
message
3.0
BEGIN
1.0
CRAP 1.8
« blib/lib/Kelp/Routes/Location.pm ↑ Summary bin/kelp-generator »
1 package Kelp::Module::Logger;
2  
3 2 ▶use Kelp::Base 'Kelp::Module';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Log::Dispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 attr logger => undef;
11 2 attr date_format => '%Y-%m-%d %T';
12 2 attr log_format => '%s - %s - %s';
13  
14 sub _logger
15 2 ▶{
Subroutine _logger: called CC=1 SCAR=0.0
16 2 my ($self, %args) = @_;
17  
18 2 return Log::Dispatch->new(%args);
19 }
20  
21 sub load_configuration
22 2 ▶{
Subroutine load_configuration: called CC=3 SCAR=11.1
Pod undocumented
23 2 my ($self, %args) = @_;
24  
25 2 for my $field (qw(date_format log_format)) {
26 $self->$field(delete $args{$field})
27 4 ▶ if $args{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $args{$field} 0 4 4
28 }
29  
30 2 return %args;
31 }
32  
33 sub build
34 2 ▶{
Subroutine build: called CC=1 SCAR=0.0
Pod documented
35 2 my ($self, %args) = @_;
36  
37 # load module config
38 2 %args = $self->load_configuration(%args);
39  
40 # load logger with the rest of the config
41 2 $self->logger($self->_logger(%args));
42  
43 # Build the registration hash
44 my %LEVELS = map {
45 2 my $level = $_;
46 4 ▶ $level => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 4 shift;
48 4 $self->message($level, @_);
49 6 };
50 } qw(debug info error);
51  
52 # Register a few levels
53 2 $self->register(%LEVELS);
54  
55 # Also register the message method as 'logger'
56 $self->register(
57 2 ▶ logger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 2 shift;
59 2 $self->message(@_);
60 }
61 2 );
62 }
63  
64 sub message
65 6 ▶{
Subroutine message: called CC=3 SCAR=11.0
Pod undocumented
66 6 my ($self, $level, @messages) = @_;
67 6 my $date = localtime->strftime($self->date_format);
68  
69 6 local $Data::Dumper::Sortkeys = 1;
70 6 for my $message (@messages) {
71 6 ▶ $message = sprintf $self->log_format,
Branch: 1 branch
BranchTrueFalseTotal
ref $message ? : 0 6 6
72 $date,
73 $level,
74 (ref $message ? Dumper($message) : $message),
75 ;
76  
77 6 $self->logger->log(level => $level, message => $message);
78 }
79 }
80  
81 2 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  
« blib/lib/Kelp/Routes/Location.pm ↑ Summary bin/kelp-generator »