1 package Log::Contextual;
6 our $VERSION = '1.000';
9 use Data::Dumper::Concise;
11 BEGIN { our @ISA = qw(Exporter) }
14 Dlog_debug DlogS_debug
15 Dlog_trace DlogS_trace
18 Dlog_error DlogS_error
19 Dlog_fatal DlogS_fatal
33 qw{set_logger with_logger}
43 die 'Log::Contextual does not have a default import list'
46 for my $idx ( 0 .. $#_ ) {
47 if ( $_[$idx] eq '-logger' ) {
48 set_logger($_[$idx + 1]);
53 $package->export_to_level(1, $package, @_);
60 $logger = do { my $l = $logger; sub { $l } }
61 if ref $logger ne 'CODE';
62 $Get_Logger = $logger;
67 $logger = do { my $l = $logger; sub { $l } }
68 if ref $logger ne 'CODE';
69 local $Get_Logger = $logger;
74 my $log = $Get_Logger->();
75 $log->trace($_[0]->())
80 my $log = $Get_Logger->();
81 $log->debug($_[0]->())
86 my $log = $Get_Logger->();
92 my $log = $Get_Logger->();
98 my $log = $Get_Logger->();
99 $log->error($_[0]->())
104 my $log = $Get_Logger->();
105 $log->fatal($_[0]->())
111 sub Dlog_trace (&@) {
115 do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
120 sub DlogS_trace (&$) {
124 do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
129 sub Dlog_debug (&@) {
133 do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
138 sub DlogS_debug (&$) {
142 do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
151 do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
156 sub DlogS_info (&$) {
160 do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
169 do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
174 sub DlogS_warn (&$) {
178 do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
183 sub Dlog_error (&@) {
187 do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
192 sub DlogS_error (&$) {
196 do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
201 sub Dlog_fatal (&@) {
205 do { local $_ = Data::Dumper::Concise::Dumper @values; $code->() };
210 sub DlogS_fatal (&$) {
214 do { local $_ = Data::Dumper::Concise::Dumper $value; $code->() };
225 Log::Contextual - Super simple logging interface
229 use Log::Contextual qw{:log set_logger with_logger};
231 my $logger = Log::Contextual::SimpleLogger->new({ levels => [qw{debug}]});
233 set_logger { $logger };
235 log_debug { "program started" };
238 with_logger Log::Contextual::SimpleLogger->new({
239 levels => [qw{trace debug}]
241 log_trace { 'foo entered' };
243 log_trace { 'foo left' };
249 This module is a simple interface to extensible logging.
255 my $logger = WarnLogger->new;
258 Arguments: Ref|CodeRef $returning_logger
260 C<set_logger> will just set the current logger to whatever you pass it. It
261 expects a C<CodeRef>, but if you pass it something else it will wrap it in a
266 my $logger = WarnLogger->new;
267 with_logger $logger => sub {
269 log_fatal { 'Non Logical Universe Detected' };
271 log_info { 'All is good' };
275 Arguments: Ref|CodeRef $returning_logger, CodeRef $to_execute
277 C<with_logger> sets the logger for the scope of the C<CodeRef> C<$to_execute>.
278 as with L<set_logger>, C<with_logger> will wrap C<$returning_logger> with a
279 C<CodeRef> if needed.
283 Arguments: CodeRef $returning_message
285 All of the following six functions work the same except that a different method
286 is called on the underlying C<$logger> object. The basic pattern is:
289 if ($logger->is_$level) {
290 $logger->$level(shift->());
296 log_trace { 'entered method foo with args ' join q{,}, @args };
300 log_debug { 'entered method foo' };
304 log_info { 'started process foo' };
308 log_warn { 'possible misconfiguration at line 10' };
312 log_error { 'non-numeric user input!' };
316 log_fatal { '1 is never equal to 0!' };
320 Arguments: CodeRef $returning_message, @args
322 All of the following six functions work the same as their L<log_$level> brethren,
323 except they return what is passed into them and as a bonus put the stringified
324 (with L<Data::Dumper::Concise>) version of their args into C<$_>. This means
325 you can do cool things like the following:
327 my @nicks = Dlog_debug { "names: $_" } map $_->value, $frew->names->all;
329 and the output might look something like:
339 my ($foo, $bar) = Dlog_trace { "entered method foo with args $_" } @_;
343 Dlog_debug { "random data structure: $_" } { foo => $bar };
347 return Dlog_info { "html from method returned: $_" } "<html>...</html>";
351 Dlog_warn { "probably invalid value: $_" } $foo;
355 Dlog_error { "non-numeric user input! ($_)" } $port;
359 Dlog_fatal { '1 is never equal to 0!' } 'ZOMG ZOMG' if 1 == 0;
363 Arguments: CodeRef $returning_message, Item $arg
365 All of the following six functions work the same as the related L<Dlog_$level>
366 functions, except they only take a single scalar after the
367 C<$returning_message> instead of slurping up (and also setting C<wantarray>)
370 my $pals_rs = DlogS_debug { "pals resultset: $_" }
371 $schema->resultset('Pals')->search({ perlers => 1 });
375 my ($foo, $bar) = DlogS_trace { "entered method foo with first arg $_" } @_;
379 DlogS_debug { "random data structure: $_" } { foo => $bar };
383 return DlogS_info { "html from method returned: $_" } "<html>...</html>";
387 DlogS_warn { "probably invalid value: $_" } $foo;
391 DlogS_error { "non-numeric user input! ($_)" } $port;
395 DlogS_fatal { '1 is never equal to 0!' } 'ZOMG ZOMG' if 1 == 0;
399 frew - Arthur Axel "fREW" Schmidt <frioux@gmail.com>
403 mst - Matt S. Trout <mst@shadowcat.co.uk>
407 Copyright (c) 2010 the Log::Contextual L</AUTHOR> and L</DESIGNER> as listed
412 This library is free software and may be distributed under the same terms as