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__
« blib/lib/Kelp/Routes/Location.pm ↑ Summary bin/kelp-generator »