1Log::Any(3)           User Contributed Perl Documentation          Log::Any(3)
2
3
4

NAME

6       Log::Any -- Bringing loggers and listeners together
7

SYNOPSIS

9       In a CPAN or other module:
10
11           package Foo;
12           use Log::Any qw($log);
13
14           $log->error("an error occurred");
15           $log->debugf("arguments are: %s", \@_)
16               if $log->is_debug();
17
18           my $log2 = Log::Any->get_logger(category => 'My::Class');
19
20       In your application:
21
22           use Log::Any::Adapter;
23
24           # Send all logs to Log::Log4perl
25           Log::Any::Adapter->set('Log4perl');
26
27           # Send all logs to Log::Dispatch
28           my $log = Log::Dispatch->new(outputs => [[ ... ]]);
29           Log::Any::Adapter->set( 'Dispatch', dispatcher => $log );
30
31           # See Log::Any::Adapter documentation for more options
32

DESCRIPTION

34       "Log::Any" allows CPAN modules to safely and efficiently log messages,
35       while letting the application choose (or decline to choose) a logging
36       mechanism such as "Log::Dispatch" or "Log::Log4perl".
37
38       "Log::Any" has a very tiny footprint and no dependencies beyond Perl
39       5.6, which makes it appropriate for even small CPAN modules to use. It
40       defaults to 'null' logging activity, so a module can safely log without
41       worrying about whether the application has chosen (or will ever choose)
42       a logging mechanism.
43
44       The application, in turn, may choose one or more logging mechanisms via
45       Log::Any::Adapter.
46

LOG LEVELS

48       "Log::Any" supports the following log levels and aliases, which is
49       meant to be inclusive of the major logging packages:
50
51            trace
52            debug
53            info (inform)
54            notice
55            warning (warn)
56            error (err)
57            critical (crit, fatal)
58            alert
59            emergency
60
61       Levels are translated as appropriate to the underlying logging
62       mechanism. For example, log4perl only has six levels, so we translate
63       'notice' to 'info' and the top three levels to 'fatal'.
64

CATEGORIES

66       Every logger has a category, generally the name of the class that asked
67       for the logger. Some logging mechanisms, like log4perl, can direct logs
68       to different places depending on category.
69

PRODUCING LOGS (FOR MODULES)

71   Getting a logger
72       The most convenient way to get a logger in your module is:
73
74           use Log::Any qw($log);
75
76       This creates a package variable $log and assigns it to the logger for
77       the current package. It is equivalent to
78
79           our $log = Log::Any->get_logger(category => __PACKAGE__);
80
81       In general, to get a logger for a specified category:
82
83           my $log = Log::Any->get_logger(category => $category)
84
85       If no category is specified, the caller package is used.
86
87   Logging
88       To log a message, use any of the log levels or aliases. e.g.
89
90           $log->error("this is an error");
91           $log->warn("this is a warning");
92           $log->warning("this is also a warning");
93
94       You should not include a newline in your message; that is the
95       responsibility of the logging mechanism, which may or may not want the
96       newline.
97
98       There are also printf-style versions of each of these methods:
99
100           $log->errorf("an error occurred: %s", $@);
101           $log->debugf("called with %d params: %s", $param_count, \@params);
102
103       The printf-style methods have a few advantages, besides being arguably
104       more readable:
105
106       ·   Any complex references (like "\@params" above) are automatically
107           converted to single-line strings with "Data::Dumper".
108
109       ·   Any undefined values are automatically converted to the string
110           "<undef>".
111
112       ·   A logging mechanism could potentially use the unchanging format
113           string (or a digest thereof) to group related log messages
114           together.
115
116   Log level detection
117       To detect whether a log level is on, use "is_" followed by any of the
118       log levels or aliases. e.g.
119
120           if ($log->is_info()) { ... }
121           $log->debug("arguments are: " . Dumper(\@_))
122               if $log->is_debug();
123
124       This is important for efficiency, as you can avoid the work of putting
125       together the logging message (in the above case, stringifying @_) if
126       the log level is not active.
127
128       Some logging mechanisms don't support detection of log levels. In these
129       cases the detection methods will always return 1.
130
131       In contrast, the default logging mechanism - Null - will return 0 for
132       all detection methods.
133
134   Testing
135       Log::Any::Test provides a mechanism to test code that uses "Log::Any".
136

CONSUMING LOGS (FOR APPLICATIONS)

138       To direct logs somewhere - a file, the screen, etc. - you must use
139       Log::Any::Adapter. This is intentionally kept in a separate
140       distributions to keep "Log::Any" as simple and unchanging as possible.
141

MOTIVATION

143       Many modules have something interesting to say. Unfortunately there is
144       no standard way for them to say it - some output to STDERR, others to
145       "warn", others to custom file logs. And there is no standard way to get
146       a module to start talking - sometimes you must call a uniquely named
147       method, other times set a package variable.
148
149       This being Perl, there are many logging mechanisms available on CPAN.
150       Each has their pros and cons. Unfortunately, the existence of so many
151       mechanisms makes it difficult for a CPAN author to commit his/her users
152       to one of them. This may be why many CPAN modules invent their own
153       logging or choose not to log at all.
154
155       To untangle this situation, we must separate the two parts of a logging
156       API.  The first, log production, includes methods to output logs (like
157       "$log->debug") and methods to inspect whether a log level is activated
158       (like "$log->is_debug"). This is generally all that CPAN modules care
159       about. The second, log consumption, includes a way to configure where
160       logging goes (a file, the screen, etc.) and the code to send it there.
161       This choice generally belongs to the application.
162
163       "Log::Any" provides a standard log production API for modules.
164       "Log::Any::Adapter" allows applications to choose the mechanism for log
165       consumption.
166
167       See http://www.openswartz.com/2007/09/06/standard-logging-api/ for the
168       original post proposing this module.
169

Q & A

171       Isn't Log::Any just yet another logging mechanism?
172           No. "Log::Any" does not, and never will, include code that knows
173           how to log to a particular place (file, screen, etc.) It can only
174           forward logging requests to another logging mechanism.
175
176       Why don't you just pick the best logging mechanism, and use and promote
177       it?
178           Each of the logging mechanisms have their pros and cons,
179           particularly in terms of how they are configured. For example,
180           log4perl offers a great deal of power and flexibility but uses a
181           global and potentially heavy configuration, whereas "Log::Dispatch"
182           is extremely configuration-light but doesn't handle categories.
183           There is also the unnamed future logger that may have advantages
184           over either of these two, and all the custom in-house loggers
185           people have created and cannot (for whatever reason) stop using.
186
187       Is it safe for my critical module to depend on Log::Any?
188           Our intent is to keep "Log::Any" minimal, and change it only when
189           absolutely necessary. Most of the "innovation", if any, is expected
190           to occur in "Log::Any::Adapter", which your module should not have
191           to depend on (unless it wants to direct logs somewhere specific).
192           "Log::Any" has no module dependencies other than Test::Simple for
193           testing.
194
195       Why doesn't Log::Any use insert modern Perl technique?
196           To encourage CPAN module authors to adopt and use "Log::Any", we
197           aim to have as few dependencies and chances of breakage as
198           possible. Thus, no "Moose" or other niceties.
199

AUTHOR

201       Jonathan Swartz
202

SEE ALSO

204       Log::Any::Adapter; the many Log:: modules on CPAN
205
207       Copyright (C) 2009 Jonathan Swartz.
208
209       Log::Any is provided "as is" and without any express or implied
210       warranties, including, without limitation, the implied warranties of
211       merchantibility and fitness for a particular purpose.
212
213       This program is free software; you canredistribute it and/or modify it
214       under the same terms as Perl itself.
215
216
217
218perl v5.12.2                      2010-02-12                       Log::Any(3)
Impressum