line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Logger; |
2
|
|
|
|
|
|
|
|
3
|
177
|
|
|
177
|
|
101258
|
use Catmandu::Sane; |
|
177
|
|
|
|
|
476
|
|
|
177
|
|
|
|
|
9878
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
177
|
|
|
177
|
|
1193
|
use Moo::Role; |
|
177
|
|
|
|
|
3265
|
|
|
177
|
|
|
|
|
4037
|
|
8
|
177
|
|
|
177
|
|
155190
|
use Log::Any (); |
|
177
|
|
|
|
|
1549148
|
|
|
177
|
|
|
|
|
6188
|
|
9
|
177
|
|
|
177
|
|
2653
|
use namespace::clean; |
|
177
|
|
|
|
|
465
|
|
|
177
|
|
|
|
|
4213
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has log => (is => 'lazy', init_arg => undef); |
12
|
|
|
|
|
|
|
has log_category => (is => 'lazy'); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
{ |
15
|
|
|
|
|
|
|
my $loggers = {}; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub _build_log { |
18
|
613
|
|
|
613
|
|
7206
|
my ($self) = @_; |
19
|
613
|
|
|
|
|
10451
|
my $category = $self->log_category; |
20
|
613
|
|
66
|
|
|
5690
|
$loggers->{$category} //= Log::Any->get_logger(category => $category); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub _build_log_category { |
25
|
613
|
|
|
613
|
|
6850
|
ref $_[0]; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
1; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
__END__ |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=pod |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 NAME |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Catmandu::Logger - A role for classes that need logging capabilities |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 SYNOPSIS |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
package MyApp::View; |
41
|
|
|
|
|
|
|
use Moo; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
with 'Catmandu::Logger'; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub something { |
46
|
|
|
|
|
|
|
my ($self) = @_; |
47
|
|
|
|
|
|
|
$self->log->debug("started bar"); # logs with default class catergory "MyApp::View" |
48
|
|
|
|
|
|
|
$self->log->error("started bar"); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=head1 DESCRIPTION |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
A logging role building a very lightweight wrapper to L<Log::Any>. Connecting |
54
|
|
|
|
|
|
|
a Log::Any::Adapter should be performed prior to logging the first log message, |
55
|
|
|
|
|
|
|
otherwise nothing will happen, just like with Log::Any. |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
The logger needs to be setup before using the logger, which could happen in the main application: |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
package main; |
60
|
|
|
|
|
|
|
use Log::Any::Adapter; |
61
|
|
|
|
|
|
|
use Log::Log4perl; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Log::Any::Adapter->set('Log4perl'); |
64
|
|
|
|
|
|
|
Log::Log4perl::init('./log4perl.conf'); |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $app = MyApp::View->new; |
67
|
|
|
|
|
|
|
$app->something(); # will print debug and error messages |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
with log4perl.conf like: |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
log4perl.rootLogger=DEBUG,OUT |
72
|
|
|
|
|
|
|
log4perl.appender.OUT=Log::Log4perl::Appender::Screen |
73
|
|
|
|
|
|
|
log4perl.appender.OUT.stderr=1 |
74
|
|
|
|
|
|
|
log4perl.appender.OUT.utf8=1 |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
log4perl.appender.OUT.layout=PatternLayout |
77
|
|
|
|
|
|
|
log4perl.appender.OUT.layout.ConversionPattern=%d [%P] - %p %l time=%r : %m%n |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
See L<Log::Log4perl> for more configuration options and selecting which messages |
80
|
|
|
|
|
|
|
to log and which not. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 CATMANDU COMMAND LINE |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
When using the L<catmandu> command line, the logger can be activated using the |
85
|
|
|
|
|
|
|
-D option on all Catmandu commands: |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
$ catmandu -D convert JSON to YAML < data.json |
88
|
|
|
|
|
|
|
$ catmandu -D export MongoDB --database-name items --bag |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
The log4perl configuration for the C<catmandu> command line must be defined in a |
91
|
|
|
|
|
|
|
'catmandu.yml' configuration file: |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
$ cat catmandu.yml |
94
|
|
|
|
|
|
|
log4perl: | |
95
|
|
|
|
|
|
|
log4perl.rootLogger=DEBUG,OUT |
96
|
|
|
|
|
|
|
log4perl.appender.OUT=Log::Log4perl::Appender::Screen |
97
|
|
|
|
|
|
|
log4perl.appender.OUT.stderr=1 |
98
|
|
|
|
|
|
|
log4perl.appender.OUT.utf8=1 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
log4perl.appender.OUT.layout=PatternLayout |
101
|
|
|
|
|
|
|
log4perl.appender.OUT.layout.ConversionPattern=%d [%P] - %p %l time=%r : %m%n |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
The C<log4perl> section can point to an inline log4perl configuration or a |
104
|
|
|
|
|
|
|
filename containing the configuration. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
See L<Catmandu::Fix::log> how to include log messages in the L<Catmandu::Fix> |
107
|
|
|
|
|
|
|
language. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 ACCESSORS |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head2 log |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
The C<log> attribute holds the L<Log::Any::Adapter> object that implements all |
114
|
|
|
|
|
|
|
logging methods for the defined log levels, such as C<debug> or C<error>. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
package MyApp::View::JSON; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
extends 'MyApp::View'; |
119
|
|
|
|
|
|
|
with 'Catmandu::Logger'; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub bar { |
122
|
|
|
|
|
|
|
$self->log->info("Everything fine so far"); # logs a info message |
123
|
|
|
|
|
|
|
$self->log->debug("Something is fishy here"); # logs a debug message |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Your package automatically has a logging category of MyApp::View::JSON. Use lines like: |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
log4perl.logger.MyApp::View::JSON=DEBUG,STDOUT |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
or |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
log4perl.logger.MyApp::View=DEBUG,STDOUT |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
or |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
log4perl.logger.MyApp=DEBUG,STDOUT |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
for specialized logging for your application. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 log_category |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Default is the class name. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 SEE ALSO |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
L<Log::Any> |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=cut |