Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use numeric level id in generated methods #54

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/Log/Dispatch.pm
Expand Up @@ -9,19 +9,22 @@ our $VERSION = '2.68';

use Carp ();
use Log::Dispatch::Types;
use Log::Dispatch::Vars qw( %CanonicalLevelNames @OrderedLevels );
use Log::Dispatch::Vars
qw( %CanonicalLevelNames @OrderedLevels %LevelNamesToNumbers);
use Module::Runtime qw( use_package_optimistically );
use Params::ValidationCompiler qw( validation_for );

use base qw( Log::Dispatch::Base );

BEGIN {
foreach my $l ( keys %CanonicalLevelNames ) {
my $sub = sub {
my $level_id = $LevelNamesToNumbers{$l};
my $sub = sub {
my $self = shift;
$self->log(
level => $CanonicalLevelNames{$l},
message => @_ > 1 ? "@_" : $_[0],
level => $CanonicalLevelNames{$l},
_level_id => $level_id,
message => @_ > 1 ? "@_" : $_[0],
);
};

Expand Down Expand Up @@ -149,7 +152,7 @@ sub log {
my $self = shift;
my %p = @_;

return unless $self->would_log( $p{level} );
return unless $self->would_log( $p{level}, $p{_level_id} );

$self->_log_to_outputs( $self->_prepare_message(%p) );
}
Expand All @@ -172,9 +175,8 @@ sub _log_to_outputs {
my $self = shift;
my %p = @_;

foreach ( keys %{ $self->{outputs} } ) {
$p{name} = $_;
$self->_log_to(%p);
foreach ( values %{ $self->{outputs} } ) {
$_->log(%p);
}
}

Expand Down Expand Up @@ -255,10 +257,12 @@ sub level_is_valid {
}

sub would_log {
my $self = shift;
my $level = shift;
my ( $self, $level, $level_id ) = @_;

return 0 unless $self->level_is_valid($level);
# assume that level is correct if ID given
if ( !defined $level_id ) {
return 0 unless $self->level_is_valid($level);
}

foreach ( values %{ $self->{outputs} } ) {
return 1 if $_->_should_log($level);
Expand Down
7 changes: 4 additions & 3 deletions lib/Log/Dispatch/Output.pm
Expand Up @@ -39,7 +39,7 @@ sub new {
my $self = shift;
my %p = $validator->(@_);

return unless $self->_should_log( $p{level} );
return unless $self->_should_log( $p{level}, $p{_level_id} );

local $! = undef;
$p{message} = $self->_apply_callbacks(%p)
Expand Down Expand Up @@ -129,9 +129,10 @@ sub accepted_levels {
}

sub _should_log {
my $self = shift;
my ( $self, $level, $level_id ) = @_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use multiple my $x = shift; lines.


my $msg_level = $self->_level_as_number(shift);
my $msg_level
= defined $level_id ? $level_id : $self->_level_as_number($level);
return ( ( $msg_level >= $self->{min_level} )
&& ( $msg_level <= $self->{max_level} ) );
}
Expand Down
10 changes: 6 additions & 4 deletions t/basic.t
Expand Up @@ -1237,11 +1237,13 @@ subtest(
\@calls,
[
{
level => 'error',
message => 'foo',
level => 'error',
_level_id => 4,
message => 'foo',
}, {
level => 'critical',
message => 'baz',
level => 'critical',
_level_id => 5,
message => 'baz',
},
],
'code received the expected messages'
Expand Down