Skip to content

Commit

Permalink
add sprintf versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jonswar committed Jul 8, 2009
1 parent 55fe257 commit 716806d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
15 changes: 14 additions & 1 deletion lib/Log/Any.pm
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ In a CPAN or other module:
use Log::Any qw($log);
$log->error("an error occurred");
$log->debug("arguments are: " . Dumper(\@_))
$log->debugf("arguments are: %s", \@_)
if $log->is_debug();
In your application:
Expand Down Expand Up @@ -261,6 +261,19 @@ To log a message, use any of the log levels. e.g.
$log->error("this is an error");
$log->info("this is an info message");
You should B<not> include a newline in your message; that is the
responsibility of the logging mechanism, which may or may not want it.
There are also printf-style versions of each of these methods:
$log->errorf("an error occurred: %s", $@);
$log->debugf("called with %d params: %s", $param_count, \@params);
There are two advantages of the printf-style methods. First, they can be
more readable than concatenated strings (subjective of course); second, any
complex references (like \@params above) are automatically expanded with
Data::Dumper.
=head2 Log level detection
To detect whether a log level is on, use "is_" followed by any of the log
Expand Down
16 changes: 15 additions & 1 deletion lib/Log/Any/Adapter/Base.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package Log::Any::Adapter::Base;
use Carp qw(croak);
use Log::Any::Util qw(make_alias);
use Log::Any::Util qw(make_alias dump_one_line);
use strict;
use warnings;

Expand Down Expand Up @@ -29,4 +29,18 @@ while (my ($alias, $realname) = each(%aliases)) {
make_alias($alias, \&$realname);
}

# Add printf-style versions of all logging methods and aliases - e.g. errorf, debugf
#
foreach my $name (Log::Any->logging_methods, keys(%aliases)) {
my $methodf = $name . "f";
my $method = $aliases{$name} || $name;
make_alias($methodf,
sub {
my ($self, $format, @params) = @_;
my @new_params = map { ref($_) ? dump_one_line($_) : $_ } @params;
my $new_message = sprintf($format, @new_params);
$self->$method($new_message);
});
}

1;
12 changes: 9 additions & 3 deletions lib/Log/Any/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ our @EXPORT = qw(
our @EXPORT_OK = qw(
make_alias
require_dynamic
dump_one_line
dp
dps
);
Expand All @@ -23,12 +24,17 @@ sub make_alias {
*{ $pkg . "::$method" } = $code;
}

sub _dump_value_with_caller {
sub dump_one_line {
my ($value) = @_;

my $dump =
Data::Dumper->new( [$value] )->Indent(1)->Sortkeys(1)->Quotekeys(0)
return Data::Dumper->new( [$value] )->Indent(0)->Sortkeys(1)->Quotekeys(0)
->Terse(1)->Dump();
}

sub _dump_value_with_caller {
my ($value) = @_;

my $dump = dump_one_line($value);
my @caller = caller(1);
return sprintf( "[dp at %s line %d.] [%d] %s\n",
$caller[1], $caller[2], $$, $dump );
Expand Down

0 comments on commit 716806d

Please sign in to comment.