blib/lib/Kelp/Module/Logger/Log4perl.pm

filter: Statementstmt 100% 21 / 21 Branchbran 50.0% 1 / 2 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 93.7% 30 / 32 CC 4 SCAR 7.0
CC
4
Cov
96%
message
3.0
_logger
2.0
BEGIN
1.0
CRAP 2.0
↑ Summary Build »
1 package Kelp::Module::Logger::Log4perl;
2 1 our $VERSION = "0.001";
3 1 $VERSION = eval $VERSION;
4  
5 1 ▶use Kelp::Base 'Kelp::Module::Logger';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::Log4perl qw( :levels );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 attr 'category' => '';
10  
11 sub _logger {
12 4 ▶ my ( $self, %args ) = @_;
Subroutine _logger: called CC=2 SCAR=6.9
13
14 4 Log::Log4perl->init($args{conf});
15  
16 4 ▶ return Log::Log4perl->get_logger($args{category} || '');
Condition100%2/2
$args{'category'} || ''
l!l
13
MC/DC100%1/1
$args{'category'} || ''
$args{'category'}
Truth table100%2/2
A
A= $args{'category'}
Aresult
11
00
17 }
18  
19 # Override <message> because Log4perl has own configurable layouts.
20 sub message {
21 16 ▶ my ($self, $level, @messages) = @_;
Subroutine message: called CC=3 SCAR=11.1
Pod undocumented
22  
23 16 my %LEVELS_map = (
24 'trace' => $TRACE,
25 'debug' => $DEBUG,
26 'info' => $INFO,
27 'warn' => $WARN,
28 'error' => $ERROR,
29 'fatal' => $FATAL,
30 'always' => $OFF,
31 );
32
33 16 for (@messages) {
34 16 ▶ my $message = ref($_) ? Dumper($_) : $_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 16 16
35 16 $self->{logger}->log( $LEVELS_map{$level}, $message );
36 }
37 }
38  
39 1 1;
40  
41 __END__
42  
43 =pod
44  
45 =head1 NAME
46  
47 Kelp::Module::Logger::Log4perl - Log4perl for Kelp applications
48  
49 =head1 DESCRIPTION
50  
51 This module provides log interface for Kelp web application. It uses
52 L<Log::Log4perl> instead of L<Log::Dispacher>.
53  
54 =head1 SYNOPSIS
55  
56  
57 # conf/config.pl
58 {
59 'modules' => ['Logger::Log4perl'],
60 'modules_init' => {
61 'Logger::Log4perl' => {
62 'category' => '',
63 'conf' => {
64 'log4perl.rootLogger' => 'TRACE, CommonLog',
65 'log4perl.appender.CommonLog' => 'Log::Log4perl::Appender::Screen',
66 'log4perl.appender.CommonLog.layout' => 'Log::Log4perl::Layout::PatternLayout::Multiline',
67 'log4perl.appender.CommonLog.layout.ConversionPattern' => '%d{yyyy-MM-dd HH:mm:ss} - %p - %m%n',
68 'log4perl.appender.CommonLog.utf8' => '1',
69 }
70 }
71 }
72 }
73  
74 # lib/MyApp.pm
75 sub run {
76 my $self = shift;
77 my $app = $self->SUPER::run(@_);
78 ...;
79 $app->info( 'Kelp is ready to rock!' );
80 $app->logger( 'trace', $some_ref_to_dump );
81  
82 return $app;
83 }
84  
85 Although module provides alternarive ways of initialization like L<Log::Log4perl/Alternative-initialization>:
86  
87 # conf/config.pl
88  
89 {
90 'modules_init' => {
91 'Logger::Log4perl' => {
92 'category' => '',
93 'conf' => 'conf/logger.conf',
94 }
95 }
96 };
97  
98 or even scalar ref:
99  
100 {
101 'modules' => [ 'Logger::Log4perl' ],
102 'modules_init' => {
103 'Logger::Log4perl' => {
104 'category' => '',
105 'conf' => \<<CONF
106 log4perl.rootLogger = DEBUG, CommonLog
107 log4perl.appender.CommonLog = Log::Log4perl::Appender::Screen
108 log4perl.appender.CommonLog.layout = Log::Log4perl::Layout::PatternLayout::Multiline
109 log4perl.appender.CommonLog.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} - %p - %m%n
110 log4perl.appender.CommonLog.utf8 = 1
111 CONF
112 }
113 }
114 };
115  
116 =head1 CONFIGURATION
117  
118 Configuration accepts href with two keys:
119  
120 =head2 category
121  
122 The L<Log::Log4perl> category to send logs to. Defaults to '' which sends to root logger.
123  
124 =head2 conf
125  
126 Configuration for <Log::Log4perl::init> method.
127  
128 =head1 REGISTERED METHODS
129  
130 Log methods can take array of scalars or reference as arguments. In case of
131 reference L<Data::Dumper> is used to deserialize information.
132  
133 =head2 debug/info/error
134  
135 Write log message to $DEBUG/$INFO/$ERROR level.
136  
137 # inside controller
138 $c->debug( $debug_data );
139 $c->info( "my info message." );
140 $c->error( @error_messages );
141  
142 =head2 logger
143  
144 Write message to one of the following default L<Log::Log4perl> log levels:
145 C<trace>, C<debug>, C<info>, C<warn>, C<error>, C<fatal>, C<always>.
146  
147 # inside controller
148 $c->logger( 'always', @messages );
149  
150 =head1 AUTHOR
151  
152 Konstantin Yakunin
153  
154 =head1 LICENSE
155  
156 This program is free software; you can redistribute it and/or modify
157 it under the same terms as Perl itself, either Perl version 5.8.2 or,
158 at your option, any later version of Perl 5 you may have available.
159  
160 =head1 BUGS
161  
162 None reported... yet.
163  
164 =head1 SEE ALSO
165  
166 L<Log::Log4perl>
167  
168 L<Kelp::Module::Logger>
169  
170 =cut
171  
↑ Summary Build »