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