Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Log-Dispatch CPAN distribution.

Report information
The Basics
Id: 23913
Status: resolved
Priority: 0/
Queue: Log-Dispatch

People
Owner: Nobody in particular
Requestors:
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: 2.15



Wish: Support for passing code refs instead of just scalars. This way code is only executed when the log level causes the message to be logged. The code ref must simply return the message to log.
Subject: Fix to allow code refs as log message
All that needs to be done is to inject this code below the 2nd line in 'sub log' in Log::Dispatch: if (ref($p{message}) eq 'CODE') { if ($self->would_log($p{level})) { $p{message} = &{$p{message}}(); } else { return; } }
--- Dispatch.pm.orig 2006-09-12 11:14:30.000000000 +0200 +++ Dispatch.pm 2006-12-14 15:42:34.000000000 +0100 @@ -65,6 +65,15 @@ my $self = shift; my %p = @_; + if (ref($p{message}) eq 'CODE') { + if ($self->would_log($p{level})) { + $p{message} = &{$p{message}}(); + } + else { + return; + } + } + $p{message} = $self->_apply_callbacks(%p) if $self->{callbacks};
Sorry, just in case: In the previous example, it's important to be explicit like this (ref($p{message}) eq 'CODE') instead of doing this: (ref($p{message})) because the latter will fail if objects are passed that have overloaded string operators (see use overload "'") that otherwise work fine in string context.