1MooX::Role::Logger(3) User Contributed Perl DocumentationMooX::Role::Logger(3)
2
3
4
6 MooX::Role::Logger - Provide logging via Log::Any
7
9 version 0.005
10
12 In your modules:
13
14 package MyModule;
15 use Moose;
16 with 'MooX::Role::Logger';
17
18 sub run {
19 my ($self) = @_;
20 $self->cry;
21 }
22
23 sub cry {
24 my ($self) = @_;
25 $self->_logger->info("I'm sad");
26 }
27
28 In your application:
29
30 use MyModule;
31 use Log::Any::Adapter ('File', '/path/to/file.log');
32
33 MyModule->run;
34
36 This role provides universal logging via Log::Any. The class using
37 this role doesn't need to know or care about the details of log
38 configuration, implementation or destination.
39
40 Use it when you want your module to offer logging capabilities, but
41 don't know who is going to use your module or what kind of logging they
42 will implement. This role lets you do your part and leaves actual log
43 setup and routing to someone else.
44
45 The application that ultimately uses your module can then choose to
46 direct log messages somewhere based on its own needs and configuration
47 with Log::Any::Adapter.
48
49 This role is based on Moo so it should work with either Moo or Moose
50 based classes.
51
53 Testing
54 Testing with Log::Any is pretty easy, thanks to Log::Any::Test. Just
55 load that before Log::Any loads and your log messages get sent to a
56 test adapter that includes testing methods:
57
58 use Test::More 0.96;
59 use Log::Any::Test;
60 use Log::Any qw/$log/;
61
62 use lib 't/lib';
63 use MyModule;
64
65 MyModule->new->cry;
66 $log->contains_ok( qr/I'm sad/, "got log message" );
67
68 done_testing;
69
70 Customizing
71 If you have a whole set of classes that should log with a single
72 category, create your own role and set the "_build__logger_category"
73 there:
74
75 package MyLibrary::Role::Logger;
76 use Moo::Role;
77 with 'MooX::Role::Logger';
78
79 sub _build__logger_category { "MyLibrary" }
80
81 Then in your other classes, use your custom role:
82
83 package MyLibrary::Foo;
84 use Moo;
85 with 'MyLibrary::Role::Logger'
86
88 _logger
89 Returns a logging object. See Log::Any for a list of logging methods
90 it accepts.
91
92 _build__logger_category
93 Override to set the category used for logging. Defaults to the class
94 name of the object (which could be a subclass). You can override to
95 lock it to a particular name:
96
97 sub _build__logger_category { __PACKAGE__ }
98
100 Since MooX::Role::Logger is universal, you have to use it with one of
101 several Log::Any::Adapter classes:
102
103 · Log::Any::Adapter::File
104
105 · Log::Any::Adapter::Stderr
106
107 · Log::Any::Adapter::Stdout
108
109 · Log::Any::Adapter::ScreenColoredLevel
110
111 · Log::Any::Adapter::Dispatch
112
113 · Log::Any::Adapter::Syslog
114
115 · Log::Any::Adapter::Log4perl
116
117 These other logging roles are specific to particular logging packages,
118 rather than being universal:
119
120 · MooseX::LazyLogDispatch
121
122 · MooseX::Log::Log4perl
123
124 · MooseX::LogDispatch
125
126 · MooseX::Role::LogHandler
127
128 · MooseX::Role::Loggable (uses Log::Dispatchouli)
129
130 · Role::Log::Syslog::Fast
131
133 Bugs / Feature Requests
134 Please report any bugs or feature requests through the issue tracker at
135 <https://github.com/dagolden/MooX-Role-Logger/issues>. You will be
136 notified automatically of any progress on your issue.
137
138 Source Code
139 This is open source software. The code repository is available for
140 public review and contribution under the terms of the license.
141
142 <https://github.com/dagolden/MooX-Role-Logger>
143
144 git clone https://github.com/dagolden/MooX-Role-Logger.git
145
147 David Golden <dagolden@cpan.org>
148
150 This software is Copyright (c) 2013 by David Golden.
151
152 This is free software, licensed under:
153
154 The Apache License, Version 2.0, January 2004
155
156
157
158perl v5.32.0 2020-07-28 MooX::Role::Logger(3)