1MooseX::Log::Log4perl(3U)ser Contributed Perl DocumentatiMoonoseX::Log::Log4perl(3)
2
3
4
6 MooseX::Log::Log4perl - A Logging Role for Moose based on Log::Log4perl
7
9 package MyApp;
10 use Moose;
11
12 with 'MooseX::Log::Log4perl';
13
14 sub something {
15 my ($self) = @_;
16 $self->log->debug("started bar"); ### logs with default class catergory "MyApp"
17 ...
18 $self->log('special')->info('bar'); ### logs with category "special"
19 ...
20 $self->log('.special')->info('bar'); ### logs with category "MyApp.special"
21 $self->log('::special')->info('bar');### logs with category "MyApp.special"
22 }
23
25 A logging role building a very lightweight wrapper to Log::Log4perl for
26 use with your Moose or Moo classes. The initialization of the Log4perl
27 instance must be performed prior to logging the first log message.
28 Otherwise the default initialization will happen, probably not doing
29 the things you expect.
30
31 For compatibility the "logger" attribute can be accessed to use a
32 common interface for application logging.
33
34 Using the logger within a class is as simple as consuming a role:
35
36 package MyClass;
37 use Moose;
38 with 'MooseX::Log::Log4perl';
39
40 sub dummy {
41 my $self = shift;
42 $self->log->info("Dummy log entry");
43 }
44
45 The logger needs to be setup before using the logger, which could
46 happen in the main application:
47
48 package main;
49 use Log::Log4perl qw(:easy);
50 use MyClass;
51
52 BEGIN { Log::Log4perl->easy_init() };
53
54 my $myclass = MyClass->new();
55 $myclass->log->info("In my class"); # Access the log of the object
56 $myclass->dummy; # Will log "Dummy log entry"
57
59 For simple logging needs use MooseX::Log::Log4perl::Easy to directly
60 add log_<level> methods to your class instance.
61
62 $self->log_info("Dummy");
63
65 As this module is using Moo, you can use it with Moo instead of Moose
66 too.
67
68 This will allow to simple use it as documented above in a Moo based
69 application, like shown in the example below:
70
71 This is your class consuming the MooseX::Log::Log4perl role.
72
73 package MyCat;
74 use Moo;
75
76 with 'MooseX::Log::Log4perl';
77
78 sub catch_it {
79 my $self = shift;
80 $self->log->debug("Say Miau");
81 }
82
83 Which can be simply used in your main application then.
84
85 package main;
86 use MyCat;
87 use Log::Log4perl qw(:easy);
88 BEGIN { Log::Log4perl->easy_init() };
89
90 my $log = Log::Log4perl->get_logger();
91 $log->info("Application startup...");
92 MyCat->new()->catch_it(); ### Will log "Dummy dodo"
93
95 logger
96 The "logger" attribute holds the Log::Log4perl object that implements
97 all logging methods for the defined log levels, such as "debug" or
98 "error". As this method is defined also in other logging roles/systems
99 like MooseX::Log::LogDispatch this can be thought of as a common
100 logging interface.
101
102 package MyApp::View::JSON;
103
104 extends 'MyApp::View';
105 with 'MooseX:Log::Log4perl';
106
107 sub bar {
108 $self->logger->info("Everything fine so far"); # logs a info message
109 $self->logger->debug("Something is fishy here"); # logs a debug message
110 }
111
112 log([$category])
113 Basically the same as logger, but also allowing to change the log
114 category for this log message. If the category starts with a "+", we
115 pre-pend the current class (what would have been the category if you
116 didn't specify one).
117
118 if ($myapp->log->is_debug()) {
119 $myapp->log->debug("Woot"); # category is class myapp
120 }
121 $myapp->log("TempCat")->info("Foobar"); # category TempCat
122 $myapp->log->info("Grumble"); # category class again myapp
123 $myapp->log(".TempCat")->info("Foobar"); # category myapp.TempCat
124 $myapp->log("::TempCat")->info("Foobar"); # category myapp.TempCat
125
127 Log::Log4perl, Moose, Moo, MooX::Log::Any, MooX::Role::Logger
128
130 Please report any issues at
131 <https://github.com/lammel/moosex-log-log4perl>
132
133 Or come bother us in "#moose" on "irc.perl.org".
134
136 Roland Lammel <lammel@cpan.org>
137
138 Inspired by the work by Chris Prather <perigrin@cpan.org> and Ash
139 Berlin <ash@cpan.org> on MooseX::LogDispatch
140
142 In alphabetical order:
143
144 · abraxxa for Any::Moose deprectation
145
146 · Michael Schilli <m@perlmeister.com> for Log::Log4perl and interface
147 suggestions
148
149 · omega for catgory prefix support
150
151 · Tim Bunce <TIMB@cpan.org> for corrections in the
152 MooseX::Log::Log4perl::Easy module.
153
155 Copyright (c) 2008-2016, Roland Lammel <lammel@cpan.org>,
156 <http://www.quikit.at>
157
158 This module is free software; you can redistribute it and/or modify it
159 under the same terms as Perl itself. See perlartistic.
160
161
162
163perl v5.30.0 2019-07-26 MooseX::Log::Log4perl(3)