From: Arthur Axel 'fREW' Schmidt Date: Fri, 11 Jan 2013 01:32:25 +0000 (-0600) Subject: perltidy code and add .perltidyrc to repo X-Git-Tag: v0.005000_01~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6ae293d71e67272c06271aa5a122dc018a835da7;p=p5sagit%2FLog-Contextual.git perltidy code and add .perltidyrc to repo --- diff --git a/.perltidyrc b/.perltidyrc new file mode 100644 index 0000000..a74e3c3 --- /dev/null +++ b/.perltidyrc @@ -0,0 +1,12 @@ +-st +-se +-i=3 +-bar +-ce +-sot +-sct +-pt=2 +-sbt=2 +-bt=2 +-nolq +-nasc diff --git a/HACKING b/HACKING new file mode 100644 index 0000000..99c82c9 --- /dev/null +++ b/HACKING @@ -0,0 +1,2 @@ +Before submitting patches please run perltidy with the .perltidyrc included in +this repository. diff --git a/lib/Log/Contextual.pm b/lib/Log/Contextual.pm index e2fa68f..9e8ca08 100644 --- a/lib/Log/Contextual.pm +++ b/lib/Log/Contextual.pm @@ -20,9 +20,9 @@ eval { # ____ is because tags must have at least one export and we don't want to # export anything but the levels selected -sub ____ {} +sub ____ { } -exports (qw(____ set_logger with_logger )); +exports(qw(____ set_logger with_logger )); export_tag dlog => ('____'); export_tag log => ('____'); @@ -32,95 +32,131 @@ sub router { our $Router_Instance ||= do { require Log::Contextual::Router; Log::Contextual::Router->new - } + } } -sub arg_logger { $_[1] } -sub arg_levels { $_[1] || [qw(debug trace warn info error fatal)] } +sub arg_logger { $_[1] } +sub arg_levels { $_[1] || [qw(debug trace warn info error fatal)] } sub arg_package_logger { $_[1] } sub arg_default_logger { $_[1] } sub before_import { my ($class, $importer, $spec) = @_; - my $router = $class->router; - my $exports = $spec->exports; - my %router_args = (exporter => $class, target => $importer, arguments => $spec->argument_info); + my $router = $class->router; + my $exports = $spec->exports; + my %router_args = ( + exporter => $class, + target => $importer, + arguments => $spec->argument_info + ); die 'Log::Contextual does not have a default import list' - if $spec->config->{default}; + if $spec->config->{default}; $router->before_import(%router_args); - $spec->add_export('&set_logger', sub { - my $router = $class->router; + $spec->add_export( + '&set_logger', + sub { + my $router = $class->router; - die ref($router) . " does not support set_logger()" - unless $router->does('Log::Contextual::Role::Router::SetLogger'); + die ref($router) . " does not support set_logger()" + unless $router->does('Log::Contextual::Role::Router::SetLogger'); - return $router->set_logger(@_); - }) if $exports->{'&set_logger'}; + return $router->set_logger(@_); + }) if $exports->{'&set_logger'}; - $spec->add_export('&with_logger', sub { - my $router = $class->router; + $spec->add_export( + '&with_logger', + sub { + my $router = $class->router; - die ref($router) . " does not support with_logger()" - unless $router->does('Log::Contextual::Role::Router::WithLogger'); + die ref($router) . " does not support with_logger()" + unless $router->does('Log::Contextual::Role::Router::WithLogger'); - return $router->with_logger(@_); - }) if $exports->{'&with_logger'}; + return $router->with_logger(@_); + }) if $exports->{'&with_logger'}; my @levels = @{$class->arg_levels($spec->config->{levels})}; for my $level (@levels) { if ($spec->config->{log}) { - $spec->add_export("&log_$level", sub (&@) { - my ($code, @args) = @_; - $router->handle_log_request( - exporter => $class, caller_package => scalar(caller), caller_level => 1, - message_level => $level, message_sub => $code, message_args => \@args, - ); - return @args; - }); - $spec->add_export("&logS_$level", sub (&@) { - my ($code, @args) = @_; - $router->handle_log_request( - exporter => $class, caller_package => scalar(caller), caller_level => 1, - message_level => $level, message_sub => $code, message_args => \@args, - ); - return $args[0]; - }); + $spec->add_export( + "&log_$level", + sub (&@) { + my ($code, @args) = @_; + $router->handle_log_request( + exporter => $class, + caller_package => scalar(caller), + caller_level => 1, + message_level => $level, + message_sub => $code, + message_args => \@args, + ); + return @args; + }); + $spec->add_export( + "&logS_$level", + sub (&@) { + my ($code, @args) = @_; + $router->handle_log_request( + exporter => $class, + caller_package => scalar(caller), + caller_level => 1, + message_level => $level, + message_sub => $code, + message_args => \@args, + ); + return $args[0]; + }); } if ($spec->config->{dlog}) { - $spec->add_export("&Dlog_$level", sub (&@) { - my ($code, @args) = @_; - my $wrapped = sub { - local $_ = (@_?Data::Dumper::Concise::Dumper @_:'()'); - &$code; - }; - $router->handle_log_request( - exporter => $class, caller_package => scalar(caller), caller_level => 1, - message_level => $level, message_sub => $wrapped, message_args => \@args, - ); - return @args; - }); - $spec->add_export("&DlogS_$level", sub (&$) { - my ($code, $ref) = @_; - my $wrapped = sub { - local $_ = Data::Dumper::Concise::Dumper($_[0]); - &$code; - }; - $router->handle_log_request( - exporter => $class, caller_package => scalar(caller), caller_level => 1, - message_level => $level, message_sub => $wrapped, message_args => [ $ref ], - ); - return $ref; - }); + $spec->add_export( + "&Dlog_$level", + sub (&@) { + my ($code, @args) = @_; + my $wrapped = sub { + local $_ = (@_ ? Data::Dumper::Concise::Dumper @_ : '()'); + &$code; + }; + $router->handle_log_request( + exporter => $class, + caller_package => scalar(caller), + caller_level => 1, + message_level => $level, + message_sub => $wrapped, + message_args => \@args, + ); + return @args; + }); + $spec->add_export( + "&DlogS_$level", + sub (&$) { + my ($code, $ref) = @_; + my $wrapped = sub { + local $_ = Data::Dumper::Concise::Dumper($_[0]); + &$code; + }; + $router->handle_log_request( + exporter => $class, + caller_package => scalar(caller), + caller_level => 1, + message_level => $level, + message_sub => $wrapped, + message_args => [$ref], + ); + return $ref; + }); } } } sub after_import { my ($class, $importer, $spec) = @_; - my %router_args = (exporter => $class, target => $importer, arguments => $spec->argument_info); + my %router_args = ( + exporter => $class, + target => $importer, + arguments => $spec->argument_info + ); $class->router->after_import(%router_args); } diff --git a/lib/Log/Contextual/Role/Router.pm b/lib/Log/Contextual/Role/Router.pm index b9623fc..0dfd157 100644 --- a/lib/Log/Contextual/Role/Router.pm +++ b/lib/Log/Contextual/Role/Router.pm @@ -17,18 +17,18 @@ Log::Contextual::Role::Router - Abstract interface between loggers and logging c =head1 SYNOPSIS package MyApp::Log::Router; - + use Moo; use Log::Contextual::SimpleLogger; - + with 'Log::Contextual::Role::Router'; - + has logger => (is => 'lazy'); - + sub _build_logger { return Log::Contextual::SimpleLogger->new({ levels_upto => 'debug' }); } - + sub before_import { my ($self, %export_info) = @_; my $exporter = $export_info{exporter}; @@ -50,7 +50,7 @@ Log::Contextual::Role::Router - Abstract interface between loggers and logging c my $log_level_name = $message_info{message_level}; my $logger = $self->logger; my $is_active = $logger->can("is_${log_level_name}"); - + return unless defined $is_active && $logger->$is_active; my $log_message = $log_code_block->(@$args); $logger->$log_level_name($log_message); @@ -60,7 +60,7 @@ Log::Contextual::Role::Router - Abstract interface between loggers and logging c use Moo; use MyApp::Log::Router; - + extends 'Log::Contextual'; #This example router is a singleton @@ -73,7 +73,7 @@ Log::Contextual::Role::Router - Abstract interface between loggers and logging c use strict; use warnings; use MyApp::Log::Contextual qw(:log); - + log_info { "Hello there" }; =head1 DESCRIPTION @@ -133,7 +133,7 @@ value would be 'main'. =item arguments -This is a hash reference containing the configuration values that were provided for the import. +This is a hash reference containing the configuration values that were provided for the import. The key is the name of the configuration item that was specified without the leading hyphen ('-'). For instance if the logging API is imported as follows @@ -176,7 +176,7 @@ the log message and then pass the message as a string to the logger. =item message_args This is an array reference that contains the arguments given to the message generating code block. -When invoking the message generator it will almost certainly be expecting these argument values +When invoking the message generator it will almost certainly be expecting these argument values as well. =back diff --git a/lib/Log/Contextual/Router.pm b/lib/Log/Contextual/Router.pm index 05fd7dc..99dd2a5 100644 --- a/lib/Log/Contextual/Router.pm +++ b/lib/Log/Contextual/Router.pm @@ -4,8 +4,8 @@ use Moo; use Scalar::Util 'blessed'; with 'Log::Contextual::Role::Router', - 'Log::Contextual::Role::Router::SetLogger', - 'Log::Contextual::Role::Router::WithLogger'; + 'Log::Contextual::Role::Router::SetLogger', + 'Log::Contextual::Role::Router::WithLogger'; eval { require Log::Log4perl; @@ -14,20 +14,20 @@ eval { }; has _default_logger => ( - is => 'ro', - default => sub { {} }, + is => 'ro', + default => sub { {} }, init_arg => undef, ); has _package_logger => ( - is => 'ro', - default => sub { {} }, + is => 'ro', + default => sub { {} }, init_arg => undef, ); has _get_logger => ( - is => 'ro', - default => sub { {} }, + is => 'ro', + default => sub { {} }, init_arg => undef, ); @@ -36,8 +36,8 @@ sub before_import { } sub after_import { my ($self, %import_info) = @_; my $exporter = $import_info{exporter}; - my $target = $import_info{target}; - my $config = $import_info{arguments}; + my $target = $import_info{target}; + my $config = $import_info{arguments}; if (my $l = $exporter->arg_logger($config->{logger})) { $self->set_logger($l); @@ -54,10 +54,13 @@ sub after_import { sub with_logger { my $logger = $_[1]; - if(ref $logger ne 'CODE') { + if (ref $logger ne 'CODE') { die 'logger was not a CodeRef or a logger object. Please try again.' - unless blessed($logger); - $logger = do { my $l = $logger; sub { $l } } + unless blessed($logger); + $logger = do { + my $l = $logger; + sub { $l } + } } local $_[0]->_get_logger->{l} = $logger; $_[2]->(); @@ -65,59 +68,68 @@ sub with_logger { sub set_logger { my $logger = $_[1]; - if(ref $logger ne 'CODE') { + if (ref $logger ne 'CODE') { die 'logger was not a CodeRef or a logger object. Please try again.' - unless blessed($logger); - $logger = do { my $l = $logger; sub { $l } } + unless blessed($logger); + $logger = do { + my $l = $logger; + sub { $l } + } } warn 'set_logger (or -logger) called more than once! This is a bad idea!' - if $_[0]->_get_logger->{l}; + if $_[0]->_get_logger->{l}; $_[0]->_get_logger->{l} = $logger; } sub _set_default_logger_for { my $logger = $_[2]; - if(ref $logger ne 'CODE') { + if (ref $logger ne 'CODE') { die 'logger was not a CodeRef or a logger object. Please try again.' - unless blessed($logger); - $logger = do { my $l = $logger; sub { $l } } + unless blessed($logger); + $logger = do { + my $l = $logger; + sub { $l } + } } $_[0]->_default_logger->{$_[1]} = $logger } sub _set_package_logger_for { my $logger = $_[2]; - if(ref $logger ne 'CODE') { + if (ref $logger ne 'CODE') { die 'logger was not a CodeRef or a logger object. Please try again.' - unless blessed($logger); - $logger = do { my $l = $logger; sub { $l } } + unless blessed($logger); + $logger = do { + my $l = $logger; + sub { $l } + } } $_[0]->_package_logger->{$_[1]} = $logger } sub get_loggers { my ($self, %info) = @_; - my $package = $info{caller_package}; + my $package = $info{caller_package}; my $log_level = $info{message_level}; - my $logger = ( - $_[0]->_package_logger->{$package} || - $_[0]->_get_logger->{l} || - $_[0]->_default_logger->{$package} || - die q( no logger set! you can't try to log something without a logger! ) - ); + my $logger = + ( $_[0]->_package_logger->{$package} + || $_[0]->_get_logger->{l} + || $_[0]->_default_logger->{$package} + || die + q( no logger set! you can't try to log something without a logger! )); $info{caller_level}++; $logger = $logger->($package, \%info); - return $logger if $logger->${\"is_${log_level}"}; + return $logger if $logger ->${\"is_${log_level}"}; return (); } sub handle_log_request { my ($self, %message_info) = @_; my $generator = $message_info{message_sub}; - my $args = $message_info{message_args}; + my $args = $message_info{message_args}; my $log_level = $message_info{message_level}; $message_info{caller_level}++; diff --git a/lib/Log/Contextual/SimpleLogger.pm b/lib/Log/Contextual/SimpleLogger.pm index 5c4e3bc..c68292e 100644 --- a/lib/Log/Contextual/SimpleLogger.pm +++ b/lib/Log/Contextual/SimpleLogger.pm @@ -4,53 +4,53 @@ use strict; use warnings; { - for my $name (qw( trace debug info warn error fatal )) { + for my $name (qw( trace debug info warn error fatal )) { - no strict 'refs'; + no strict 'refs'; - *{$name} = sub { - my $self = shift; + *{$name} = sub { + my $self = shift; - $self->_log( $name, @_ ) - if ($self->{$name}); - }; + $self->_log($name, @_) + if ($self->{$name}); + }; - *{"is_$name"} = sub { - my $self = shift; - return $self->{$name}; - }; - } + *{"is_$name"} = sub { + my $self = shift; + return $self->{$name}; + }; + } } sub new { - my ($class, $args) = @_; - my $self = bless {}, $class; - - $self->{$_} = 1 for @{$args->{levels}}; - $self->{coderef} = $args->{coderef} || sub { print STDERR @_}; - - if (my $upto = $args->{levels_upto}) { - - my @levels = (qw( trace debug info warn error fatal )); - my $i = 0; - for (@levels) { - last if $upto eq $_; - $i++ - } - for ($i..$#levels) { - $self->{$levels[$_]} = 1 - } - - } - return $self; + my ($class, $args) = @_; + my $self = bless {}, $class; + + $self->{$_} = 1 for @{$args->{levels}}; + $self->{coderef} = $args->{coderef} || sub { print STDERR @_ }; + + if (my $upto = $args->{levels_upto}) { + + my @levels = (qw( trace debug info warn error fatal )); + my $i = 0; + for (@levels) { + last if $upto eq $_; + $i++ + } + for ($i .. $#levels) { + $self->{$levels[$_]} = 1 + } + + } + return $self; } sub _log { - my $self = shift; - my $level = shift; - my $message = join( "\n", @_ ); - $message .= "\n" unless $message =~ /\n$/; - $self->{coderef}->(sprintf( "[%s] %s", $level, $message )); + my $self = shift; + my $level = shift; + my $message = join("\n", @_); + $message .= "\n" unless $message =~ /\n$/; + $self->{coderef}->(sprintf("[%s] %s", $level, $message)); } 1; diff --git a/lib/Log/Contextual/TeeLogger.pm b/lib/Log/Contextual/TeeLogger.pm index 7416853..2389956 100644 --- a/lib/Log/Contextual/TeeLogger.pm +++ b/lib/Log/Contextual/TeeLogger.pm @@ -4,38 +4,38 @@ use strict; use warnings; { - for my $name (qw( trace debug info warn error fatal )) { + for my $name (qw( trace debug info warn error fatal )) { - no strict 'refs'; + no strict 'refs'; - *{$name} = sub { - my $self = shift; + *{$name} = sub { + my $self = shift; - foreach my $logger (@{$self->{loggers}}) { - $logger->$name(@_); - } - }; + foreach my $logger (@{$self->{loggers}}) { + $logger->$name(@_); + } + }; - my $is_name = "is_${name}"; + my $is_name = "is_${name}"; - *{$is_name} = sub { - my $self = shift; - foreach my $logger (@{$self->{loggers}}) { - return 1 if $logger->$is_name(@_); - } - return 0; - }; - } + *{$is_name} = sub { + my $self = shift; + foreach my $logger (@{$self->{loggers}}) { + return 1 if $logger->$is_name(@_); + } + return 0; + }; + } } sub new { - my ($class, $args) = @_; - my $self = bless {}, $class; + my ($class, $args) = @_; + my $self = bless {}, $class; - ref($self->{loggers} = $args->{loggers}) eq 'ARRAY' - or die "No loggers passed to tee logger"; + ref($self->{loggers} = $args->{loggers}) eq 'ARRAY' + or die "No loggers passed to tee logger"; - return $self; + return $self; } 1; diff --git a/lib/Log/Contextual/WarnLogger.pm b/lib/Log/Contextual/WarnLogger.pm index 0a7b921..0c9f329 100644 --- a/lib/Log/Contextual/WarnLogger.pm +++ b/lib/Log/Contextual/WarnLogger.pm @@ -7,109 +7,110 @@ use Carp 'croak'; my @default_levels = qw( trace debug info warn error fatal ); - # generate subs to handle the default levels # anything else will have to be handled by AUTOLOAD at runtime { - for my $level (@default_levels) { + for my $level (@default_levels) { - no strict 'refs'; + no strict 'refs'; - my $is_name = "is_$level"; - *{$level} = sub { - my $self = shift; + my $is_name = "is_$level"; + *{$level} = sub { + my $self = shift; - $self->_log( $level, @_ ) - if $self->$is_name; - }; + $self->_log($level, @_) + if $self->$is_name; + }; - *{$is_name} = sub { - my $self = shift; - return 1 if $ENV{$self->{env_prefix} . '_' . uc $level}; - my $upto = $ENV{$self->{env_prefix} . '_UPTO'}; - return unless $upto; - $upto = lc $upto; - - return $self->{level_num}{$level} >= $self->{level_num}{$upto}; - }; - } + *{$is_name} = sub { + my $self = shift; + return 1 if $ENV{$self->{env_prefix} . '_' . uc $level}; + my $upto = $ENV{$self->{env_prefix} . '_UPTO'}; + return unless $upto; + $upto = lc $upto; + + return $self->{level_num}{$level} >= $self->{level_num}{$upto}; + }; + } } our $AUTOLOAD; + sub AUTOLOAD { - my $self = $_[0]; + my $self = $_[0]; - (my $name = our $AUTOLOAD) =~ s/.*:://; - return if $name eq 'DESTROY'; + (my $name = our $AUTOLOAD) =~ s/.*:://; + return if $name eq 'DESTROY'; - # extract the log level from the sub name - my ($is, $level) = $name =~ m/^(is_)?(.+)$/; - my $is_name = "is_$level"; + # extract the log level from the sub name + my ($is, $level) = $name =~ m/^(is_)?(.+)$/; + my $is_name = "is_$level"; - no strict 'refs'; - *{$level} = sub { - my $self = shift; + no strict 'refs'; + *{$level} = sub { + my $self = shift; - $self->_log( $level, @_ ) - if $self->$is_name; - }; + $self->_log($level, @_) + if $self->$is_name; + }; - *{$is_name} = sub { - my $self = shift; + *{$is_name} = sub { + my $self = shift; - my $prefix_field = $self->{env_prefix} . '_' . uc $level; - return 1 if $ENV{$prefix_field}; + my $prefix_field = $self->{env_prefix} . '_' . uc $level; + return 1 if $ENV{$prefix_field}; - # don't log if the variable specifically says not to - return 0 if defined $ENV{$prefix_field} and not $ENV{$prefix_field}; + # don't log if the variable specifically says not to + return 0 if defined $ENV{$prefix_field} and not $ENV{$prefix_field}; - my $upto_field = $self->{env_prefix} . '_UPTO'; - my $upto = $ENV{$upto_field}; + my $upto_field = $self->{env_prefix} . '_UPTO'; + my $upto = $ENV{$upto_field}; - if ($upto) { - $upto = lc $upto; + if ($upto) { + $upto = lc $upto; - croak "Unrecognized log level '$upto' in \$ENV{$upto_field}" - if not defined $self->{level_num}{$upto}; + croak "Unrecognized log level '$upto' in \$ENV{$upto_field}" + if not defined $self->{level_num}{$upto}; - return $self->{level_num}{$level} >= $self->{level_num}{$upto}; - } + return $self->{level_num}{$level} >= $self->{level_num}{$upto}; + } - # if we don't recognize this level and nothing says otherwise, log! - return 1 if not $self->{custom_levels}; - }; - goto &$AUTOLOAD; + # if we don't recognize this level and nothing says otherwise, log! + return 1 if not $self->{custom_levels}; + }; + goto &$AUTOLOAD; } sub new { - my ($class, $args) = @_; + my ($class, $args) = @_; - my $levels = $args->{levels}; - croak 'invalid levels specification: must be non-empty arrayref' - if defined $levels and (ref $levels ne 'ARRAY' or !@$levels); + my $levels = $args->{levels}; + croak 'invalid levels specification: must be non-empty arrayref' + if defined $levels and (ref $levels ne 'ARRAY' or !@$levels); - my $custom_levels = defined $levels; - $levels ||= [ @default_levels ]; + my $custom_levels = defined $levels; + $levels ||= [@default_levels]; - my %level_num; @level_num{ @$levels } = (0 .. $#{$levels}); + my %level_num; + @level_num{@$levels} = (0 .. $#{$levels}); - my $self = bless { - levels => $levels, - level_num => \%level_num, - custom_levels => $custom_levels, - }, $class; + my $self = bless { + levels => $levels, + level_num => \%level_num, + custom_levels => $custom_levels, + }, $class; - $self->{env_prefix} = $args->{env_prefix} or - die 'no env_prefix passed to Log::Contextual::WarnLogger->new'; - return $self; + $self->{env_prefix} = $args->{env_prefix} + or die 'no env_prefix passed to Log::Contextual::WarnLogger->new'; + return $self; } sub _log { - my $self = shift; - my $level = shift; - my $message = join( "\n", @_ ); - $message .= "\n" unless $message =~ /\n$/; - warn "[$level] $message"; + my $self = shift; + my $level = shift; + my $message = join("\n", @_); + $message .= "\n" unless $message =~ /\n$/; + warn "[$level] $message"; } 1; diff --git a/t/arg.t b/t/arg.t index 501bec3..70d7bec 100644 --- a/t/arg.t +++ b/t/arg.t @@ -10,9 +10,9 @@ my @levels = qw(debug trace warn info error fatal); BEGIN { $var_log = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var = shift } - }) + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var = shift } + }) } use Log::Contextual qw{ :log :dlog}, -logger => $var_log; @@ -20,14 +20,15 @@ use Log::Contextual qw{ :log :dlog}, -logger => $var_log; my @args = qw(fizz buzz fizzbuzz); for my $level (@levels) { - for my $prefix (qw(log logS Dlog DlogS)) { + for my $prefix (qw(log logS Dlog DlogS)) { - my $original = local $_ = "don't tread on me"; - my $method_name = "${prefix}_${level}"; - my $ref = __PACKAGE__->can($method_name) or die "no ref found for method $method_name"; + my $original = local $_ = "don't tread on me"; + my $method_name = "${prefix}_${level}"; + my $ref = __PACKAGE__->can($method_name) + or die "no ref found for method $method_name"; - $ref->(sub { "$method_name" }, @args); - ok($_ eq $original, "\$_ was not disturbed by $method_name"); - ok($var eq "[$level] $method_name\n", "log argument was correct"); - } + $ref->(sub { "$method_name" }, @args); + ok($_ eq $original, "\$_ was not disturbed by $method_name"); + ok($var eq "[$level] $method_name\n", "log argument was correct"); + } } diff --git a/t/base.t b/t/base.t index 873650d..23e27d9 100644 --- a/t/base.t +++ b/t/base.t @@ -11,18 +11,21 @@ my @levels = qw(lol wut zomg); VANILLA: { for (@levels) { main->can("log_$_")->(sub { 'fiSMBoC' }); - is( $DumbLogger2::var, "[$_] fiSMBoC\n", "$_ works"); + is($DumbLogger2::var, "[$_] fiSMBoC\n", "$_ works"); - my @vars = main->can("log_$_")->(sub { 'fiSMBoC: ' . $_[1] }, qw{foo bar baz}); - is( $DumbLogger2::var, "[$_] fiSMBoC: bar\n", "log_$_ works with input"); - ok( eq_array(\@vars, [qw{foo bar baz}]), "log_$_ passes data through correctly"); + my @vars = + main->can("log_$_")->(sub { 'fiSMBoC: ' . $_[1] }, qw{foo bar baz}); + is($DumbLogger2::var, "[$_] fiSMBoC: bar\n", "log_$_ works with input"); + ok( + eq_array(\@vars, [qw{foo bar baz}]), + "log_$_ passes data through correctly" + ); my $val = main->can("logS_$_")->(sub { 'fiSMBoC: ' . $_[0] }, 'foo'); - is( $DumbLogger2::var, "[$_] fiSMBoC: foo\n", "logS_$_ works with input"); - is( $val, 'foo', "logS_$_ passes data through correctly"); + is($DumbLogger2::var, "[$_] fiSMBoC: foo\n", "logS_$_ works with input"); + is($val, 'foo', "logS_$_ passes data through correctly"); } } ok(!eval { Log::Contextual->import; 1 }, 'Blank Log::Contextual import dies'); - diff --git a/t/caller.t b/t/caller.t index 55391e7..8c6637f 100644 --- a/t/caller.t +++ b/t/caller.t @@ -7,9 +7,12 @@ use Log::Contextual qw(:log set_logger); my $var; my @caller_info; my $var_log = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { chomp($_[0]); $var = "$_[0] at $caller_info[1] line $caller_info[2].\n" } -}); + levels => [qw(trace debug info warn error fatal)], + coderef => sub { + chomp($_[0]); + $var = "$_[0] at $caller_info[1] line $caller_info[2].\n" + } + }); my $warn_faker = sub { my ($package, $args) = @_; @caller_info = caller($args->{caller_level}); @@ -18,13 +21,27 @@ my $warn_faker = sub { set_logger($warn_faker); log_debug { 'test log_debug' }; -is($var, "[debug] test log_debug at " . __FILE__ . " line " . (__LINE__-1) . ".\n", 'fake warn'); +is($var, + "[debug] test log_debug at " . __FILE__ . " line " . (__LINE__- 2) . ".\n", + 'fake warn'); logS_debug { 'test logS_debug' }; -is($var, "[debug] test logS_debug at " . __FILE__ . " line " . (__LINE__-1) . ".\n", 'fake warn'); +is( + $var, + "[debug] test logS_debug at " . __FILE__ . " line " . (__LINE__- 3) . ".\n", + 'fake warn' +); logS_debug { 'test Dlog_debug' }; -is($var, "[debug] test Dlog_debug at " . __FILE__ . " line " . (__LINE__-1) . ".\n", 'fake warn'); +is( + $var, + "[debug] test Dlog_debug at " . __FILE__ . " line " . (__LINE__- 3) . ".\n", + 'fake warn' +); logS_debug { 'test DlogS_debug' }; -is($var, "[debug] test DlogS_debug at " . __FILE__ . " line " . (__LINE__-1) . ".\n", 'fake warn'); +is( + $var, + "[debug] test DlogS_debug at " . __FILE__ . " line " . (__LINE__- 3) . ".\n", + 'fake warn' +); diff --git a/t/default_logger.t b/t/default_logger.t index 797bf4b..b509cd2 100644 --- a/t/default_logger.t +++ b/t/default_logger.t @@ -8,40 +8,47 @@ my $var1; my $var2; my $var3; my $var_logger1 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var1 = shift }, + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var1 = shift }, }); my $var_logger2; + BEGIN { $var_logger2 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var2 = shift }, - }) + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var2 = shift }, + }) } my $var_logger3; + BEGIN { $var_logger3 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var3 = shift }, - }) + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var3 = shift }, + }) } { + package J; - use Log::Contextual qw{:dlog :log with_logger set_logger}, -default_logger => $var_logger3; + use Log::Contextual qw{:dlog :log with_logger set_logger}, + -default_logger => $var_logger3; sub foo { log_debug { 'bar' }; } + sub bar { Dlog_debug { "bar: $_" } 'frew'; } } { + package K; - use Log::Contextual qw{:log with_logger set_logger}, -default_logger => $var_logger2; + use Log::Contextual qw{:log with_logger set_logger}, + -default_logger => $var_logger2; sub foo { log_debug { 'foo' }; @@ -58,5 +65,5 @@ $var2 = ''; set_logger($var_logger1); K::foo; -is($var2, q(), '... but set_logger wins'); +is($var2, q(), '... but set_logger wins'); is($var1, "[debug] foo\n", '... and gets the value'); diff --git a/t/dlog.t b/t/dlog.t index c9a8fe3..d795c4f 100644 --- a/t/dlog.t +++ b/t/dlog.t @@ -10,43 +10,42 @@ my @levels = qw(debug trace warn info error fatal); BEGIN { $var_log = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var = shift } - }) + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var = shift } + }) } use Log::Contextual qw{:dlog}, -logger => $var_log; for my $level (@levels) { - my @foo = main->can("Dlog_$level")->( - sub { "Look ma, data: $_" }, - qw{frew bar baz} - ); + my @foo = + main->can("Dlog_$level")->(sub { "Look ma, data: $_" }, qw{frew bar baz}); ok( eq_array(\@foo, [qw{frew bar baz}]), "Dlog_$level passes data through correctly" ); is( - $var, qq([$level] Look ma, data: "frew"\n"bar"\n"baz"\n), + $var, + qq([$level] Look ma, data: "frew"\n"bar"\n"baz"\n), "Output for Dlog_$level is correct" ); - my $bar = main->can("DlogS_$level")->( - sub { "Look ma, data: $_" }, - [qw{frew bar baz}] - ); + my $bar = + main->can("DlogS_$level") + ->(sub { "Look ma, data: $_" }, [qw{frew bar baz}]); ok( eq_array($bar, [qw{frew bar baz}]), 'DlogS_trace passes data through correctly' ); is( - $var, qq([$level] Look ma, data: [\n "frew",\n "bar",\n "baz"\n]\n), + $var, + qq([$level] Look ma, data: [\n "frew",\n "bar",\n "baz"\n]\n), "Output for DlogS_$level is correct" ); @foo = main->can("Dlog_$level")->(sub { "nothing: $_" }, ()); - ok( eq_array(\@foo, []), "Dlog_$level passes nothing through correctly"); - is( $var, "[$level] nothing: ()\n", "Output for Dlog_$level is correct"); + ok(eq_array(\@foo, []), "Dlog_$level passes nothing through correctly"); + is($var, "[$level] nothing: ()\n", "Output for Dlog_$level is correct"); } diff --git a/t/eg.t b/t/eg.t index 0d7843c..e28a116 100644 --- a/t/eg.t +++ b/t/eg.t @@ -5,35 +5,35 @@ use Log::Contextual::SimpleLogger; use Test::More qw(no_plan); use Log::Contextual qw(:log set_logger); -my ($var1,$var2,$var3); +my ($var1, $var2, $var3); my $complex_dispatcher = do { my $l1 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var1 = shift }, + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var1 = shift }, }); my $l2 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var2 = shift }, + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var2 = shift }, }); my $l3 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var3 = shift }, + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var3 = shift }, }); my %registry = ( -logger => $l3, - A1 => { + A1 => { -logger => $l1, lol => $l2, }, - A2 => { -logger => $l2 }, + A2 => {-logger => $l2}, ); sub { - my ( $package, $info ) = @_; + my ($package, $info) = @_; my $logger = $registry{'-logger'}; if (my $r = $registry{$package}) { @@ -43,7 +43,7 @@ my $complex_dispatcher = do { $logger = $r->{$sub} if $r->{$sub}; } return $logger; - } + } }; set_logger $complex_dispatcher; @@ -74,19 +74,29 @@ A3::squint(); is($var3, "[debug] 2.var3\n", "global default logger works"); BEGIN { + package A1; use Log::Contextual ':log'; - sub lol { log_debug { '1.var2' } } - sub rofl { log_debug { '1.var1' } } + sub lol { + log_debug { '1.var2' } + } + + sub rofl { + log_debug { '1.var1' } + } package A2; use Log::Contextual ':log'; - sub foo { log_debug { '2.var2' } } + sub foo { + log_debug { '2.var2' } + } package A3; use Log::Contextual ':log'; - sub squint { log_debug { '2.var3' } } + sub squint { + log_debug { '2.var3' } + } } diff --git a/t/lib/BaseLogger.pm b/t/lib/BaseLogger.pm index 3b832fd..25ddf4e 100644 --- a/t/lib/BaseLogger.pm +++ b/t/lib/BaseLogger.pm @@ -7,23 +7,23 @@ my $logger = DumbLogger2->new; sub arg_levels { $_[1] || [qw(lol wut zomg)] } sub arg_logger { $_[1] || $logger } + sub router { our $Router_Instance ||= do { require Log::Contextual::Router; Log::Contextual::Router->new - } + } } - package DumbLogger2; our $var; sub new { bless {}, 'DumbLogger2' } -sub is_wut { 1 } -sub wut { $var = "[wut] $_[1]\n" } -sub is_lol { 1 } -sub lol { $var = "[lol] $_[1]\n" } +sub is_wut { 1 } +sub wut { $var = "[wut] $_[1]\n" } +sub is_lol { 1 } +sub lol { $var = "[lol] $_[1]\n" } sub is_zomg { 1 } -sub zomg { $var = "[zomg] $_[1]\n" } +sub zomg { $var = "[zomg] $_[1]\n" } 1; diff --git a/t/lib/TestExporter.pm b/t/lib/TestExporter.pm index ba7ab95..da9b7e4 100644 --- a/t/lib/TestExporter.pm +++ b/t/lib/TestExporter.pm @@ -9,4 +9,4 @@ sub router { our $Router ||= TestRouter->new } -1; \ No newline at end of file +1; diff --git a/t/lib/TestRouter.pm b/t/lib/TestRouter.pm index 67e57c1..9b6f888 100644 --- a/t/lib/TestRouter.pm +++ b/t/lib/TestRouter.pm @@ -22,4 +22,4 @@ sub handle_log_request { $self->captured->{message} = \%message_info; } -1; \ No newline at end of file +1; diff --git a/t/log-with-levels.t b/t/log-with-levels.t index e6371a5..d4f3cc6 100644 --- a/t/log-with-levels.t +++ b/t/log-with-levels.t @@ -1,7 +1,8 @@ use strict; use warnings; -use Log::Contextual qw{:dlog :log with_logger set_logger}, -levels => ['custom']; +use Log::Contextual qw{:dlog :log with_logger set_logger}, + -levels => ['custom']; use Log::Contextual::SimpleLogger; use Test::More qw(no_plan); @@ -10,15 +11,18 @@ my $logger = DumbLogger->new; set_logger(sub { $logger }); log_custom { 'fiSMBoC' }; -is( $DumbLogger::var, "fiSMBoC", "custom works"); +is($DumbLogger::var, "fiSMBoC", "custom works"); my @vars = log_custom { 'fiSMBoC: ' . $_[1] } qw{foo bar baz}; -is( $DumbLogger::var, "fiSMBoC: bar", "log_custom works with input"); -ok( eq_array(\@vars, [qw{foo bar baz}]), "log_custom passes data through correctly"); +is($DumbLogger::var, "fiSMBoC: bar", "log_custom works with input"); +ok( + eq_array(\@vars, [qw{foo bar baz}]), + "log_custom passes data through correctly" +); my $val = logS_custom { 'fiSMBoC: ' . $_[0] } 'foo'; -is( $DumbLogger::var, "fiSMBoC: foo", "logS_custom works with input"); -is( $val, 'foo', "logS_custom passes data through correctly"); +is($DumbLogger::var, "fiSMBoC: foo", "logS_custom works with input"); +is($val, 'foo', "logS_custom passes data through correctly"); my @foo = Dlog_custom { "Look ma, data: $_" } qw{frew bar baz}; @@ -27,30 +31,31 @@ ok( "Dlog_custom passes data through correctly" ); is( - $DumbLogger::var, qq(Look ma, data: "frew"\n"bar"\n"baz"\n), + $DumbLogger::var, + qq(Look ma, data: "frew"\n"bar"\n"baz"\n), "Output for Dlog_custom is correct" ); -my $bar = DlogS_custom { "Look ma, data: $_" } [qw{frew bar baz}]; -ok( - eq_array($bar, [qw{frew bar baz}]), - 'DlogS_custom passes data through correctly' -); +my $bar = DlogS_custom { "Look ma, data: $_" }[qw{frew bar baz}]; +ok(eq_array($bar, [qw{frew bar baz}]), + 'DlogS_custom passes data through correctly'); is( - $DumbLogger::var, qq(Look ma, data: [\n "frew",\n "bar",\n "baz"\n]\n), + $DumbLogger::var, + qq(Look ma, data: [\n "frew",\n "bar",\n "baz"\n]\n), "Output for DlogS_custom is correct" ); @foo = Dlog_custom { "nothing: $_" } (); -ok( eq_array(\@foo, []), "Dlog_custom passes nothing through correctly"); -is( $DumbLogger::var, "nothing: ()", "Output for Dlog_custom is correct"); +ok(eq_array(\@foo, []), "Dlog_custom passes nothing through correctly"); +is($DumbLogger::var, "nothing: ()", "Output for Dlog_custom is correct"); ok(!main->can($_), "$_ not imported") - for map +( "log_$_", "logS_$_" ), qw(debug trace warn info error fatal); + for map +("log_$_", "logS_$_"), qw(debug trace warn info error fatal); ok(!eval { Log::Contextual->import; 1 }, 'Blank Log::Contextual import dies'); BEGIN { + package DumbLogger; our $var; diff --git a/t/log.t b/t/log.t index ea0734d..8303a43 100644 --- a/t/log.t +++ b/t/log.t @@ -11,24 +11,24 @@ my $var1; my $var2; my $var3; my $var_logger1 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var1 = shift }, + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var1 = shift }, }); my $var_logger2 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var2 = shift }, + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var2 = shift }, }); my $var_logger3 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var3 = shift }, + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var3 = shift }, }); SETLOGGER: { set_logger(sub { $var_logger3 }); log_debug { 'set_logger' }; - is( $var3, "[debug] set_logger\n", 'set logger works' ); + is($var3, "[debug] set_logger\n", 'set logger works'); } SETLOGGERTWICE: { @@ -36,7 +36,8 @@ SETLOGGERTWICE: { local $SIG{__WARN__} = sub { $foo = shift }; set_logger(sub { $var_logger3 }); like( - $foo, qr/set_logger \(or -logger\) called more than once! This is a bad idea! at/, + $foo, + qr/set_logger \(or -logger\) called more than once! This is a bad idea! at/, 'set_logger twice warns correctly' ); } @@ -51,29 +52,32 @@ WITHLOGGER: { }; - is( $var1, "[debug] nothing!\n", 'inner scoped logger works' ); - is( $var2, "[debug] frew!\n", 'outer scoped logger works' ); + is($var1, "[debug] nothing!\n", 'inner scoped logger works'); + is($var2, "[debug] frew!\n", 'outer scoped logger works'); } SETWITHLOGGER: { with_logger $var_logger1 => sub { log_debug { 'nothing again!' }; + # do this just so the following set_logger won't warn - local $SIG{__WARN__} = sub {}; + local $SIG{__WARN__} = sub { }; set_logger(sub { $var_logger3 }); log_debug { 'this is a set inside a with' }; }; - is( $var1, "[debug] nothing again!\n", + is( + $var1, + "[debug] nothing again!\n", 'inner scoped logger works after using set_logger' ); - is( $var3, "[debug] this is a set inside a with\n", - 'set inside with works' - ); + is($var3, "[debug] this is a set inside a with\n", 'set inside with works'); log_debug { 'frioux!' }; - is( $var3, "[debug] frioux!\n", + is( + $var3, + "[debug] frioux!\n", q{set_logger's logger comes back after scoped logger} ); } @@ -81,15 +85,19 @@ SETWITHLOGGER: { VANILLA: { for (@levels) { main->can("log_$_")->(sub { 'fiSMBoC' }); - is( $var3, "[$_] fiSMBoC\n", "$_ works"); + is($var3, "[$_] fiSMBoC\n", "$_ works"); - my @vars = main->can("log_$_")->(sub { 'fiSMBoC: ' . $_[1] }, qw{foo bar baz}); - is( $var3, "[$_] fiSMBoC: bar\n", "log_$_ works with input"); - ok( eq_array(\@vars, [qw{foo bar baz}]), "log_$_ passes data through correctly"); + my @vars = + main->can("log_$_")->(sub { 'fiSMBoC: ' . $_[1] }, qw{foo bar baz}); + is($var3, "[$_] fiSMBoC: bar\n", "log_$_ works with input"); + ok( + eq_array(\@vars, [qw{foo bar baz}]), + "log_$_ passes data through correctly" + ); my $val = main->can("logS_$_")->(sub { 'fiSMBoC: ' . $_[0] }, 'foo'); - is( $var3, "[$_] fiSMBoC: foo\n", "logS_$_ works with input"); - is( $val, 'foo', "logS_$_ passes data through correctly"); + is($var3, "[$_] fiSMBoC: foo\n", "logS_$_ works with input"); + is($val, 'foo', "logS_$_ passes data through correctly"); } } diff --git a/t/log4perl.t b/t/log4perl.t index d3682a7..40e748b 100644 --- a/t/log4perl.t +++ b/t/log4perl.t @@ -3,12 +3,13 @@ use warnings; use Test::More; -if (eval <<'EOE' +if ( + eval <<'EOE' require Log::Log4perl; die if $Log::Log4perl::VERSION < 1.29; 1 EOE -) { + ) { plan tests => 2; } else { plan skip_all => 'Log::Log4perl 1.29 not installed' @@ -32,7 +33,10 @@ open my $log, '<', 'myerrs.log'; my @datas = <$log>; close $log; -is $datas[0], "file:t/log4perl.t line:$elines[0] method:main:: - err FIRST\n", 'file and line work with Log4perl'; -is $datas[1], "file:t/log4perl.t line:$elines[1] method:main::foo - err SECOND\n", 'file and line work with Log4perl in a sub'; +is $datas[0], "file:t/log4perl.t line:$elines[0] method:main:: - err FIRST\n", + 'file and line work with Log4perl'; +is $datas[1], + "file:t/log4perl.t line:$elines[1] method:main::foo - err SECOND\n", + 'file and line work with Log4perl in a sub'; unlink 'myerrs.log'; diff --git a/t/package_logger.t b/t/package_logger.t index 796878d..27823c7 100644 --- a/t/package_logger.t +++ b/t/package_logger.t @@ -8,40 +8,47 @@ my $var1; my $var2; my $var3; my $var_logger1 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var1 = shift }, + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var1 = shift }, }); my $var_logger2; + BEGIN { $var_logger2 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var2 = shift }, - }) + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var2 = shift }, + }) } my $var_logger3; + BEGIN { $var_logger3 = Log::Contextual::SimpleLogger->new({ - levels => [qw(trace debug info warn error fatal)], - coderef => sub { $var3 = shift }, - }) + levels => [qw(trace debug info warn error fatal)], + coderef => sub { $var3 = shift }, + }) } { + package J; - use Log::Contextual qw{:dlog :log with_logger set_logger}, -package_logger => $var_logger3; + use Log::Contextual qw{:dlog :log with_logger set_logger}, + -package_logger => $var_logger3; sub foo { log_debug { 'bar' }; } + sub bar { Dlog_debug { "bar: $_" } 'frew'; } } { + package K; - use Log::Contextual qw{:log with_logger set_logger}, -package_logger => $var_logger2; + use Log::Contextual qw{:log with_logger set_logger}, + -package_logger => $var_logger2; sub foo { log_debug { 'foo' }; @@ -59,5 +66,5 @@ $var1 = ''; set_logger($var_logger1); K::foo; -is($var1, q(), '... and set_logger does not win'); +is($var1, q(), '... and set_logger does not win'); is($var2, "[debug] foo\n", '... and package_logger still gets the value'); diff --git a/t/router_api.t b/t/router_api.t index 276b918..21ea1e3 100644 --- a/t/router_api.t +++ b/t/router_api.t @@ -3,31 +3,43 @@ use warnings; use Test::More; use lib 't/lib'; -use TestExporter qw(:log), -logger => 'logger value', -default_logger => 'default logger value', - -package_logger => 'package logger value'; +use TestExporter qw(:log), + -logger => 'logger value', + -default_logger => 'default logger value', + -package_logger => 'package logger value'; my @test_args = qw( some argument values ); log_info { "Ignored value" } @test_args; -my $results = TestExporter->router->captured; +my $results = TestExporter->router->captured; my %export_info = ( - exporter => 'TestExporter', target => 'main', arguments => { - logger => 'logger value', default_logger => 'default logger value', + exporter => 'TestExporter', + target => 'main', + arguments => { + logger => 'logger value', + default_logger => 'default logger value', package_logger => 'package logger value' }, ); my %message_info = ( - exporter => 'TestExporter', caller_package => 'main', caller_level => 1, - message_level => 'info', message_args => \@test_args, + exporter => 'TestExporter', + caller_package => 'main', + caller_level => 1, + message_level => 'info', + message_args => \@test_args, ); -is_deeply($results->{before_import}, \%export_info, 'before_import() values are correct'); -is_deeply($results->{after_import}, \%export_info, 'after_import() values are correct'); +is_deeply($results->{before_import}, + \%export_info, 'before_import() values are correct'); +is_deeply($results->{after_import}, + \%export_info, 'after_import() values are correct'); #can't really compare the sub ref value so make sure it exists and is the right type #and remove it for the later result check my $message_block = delete $results->{message}->{message_sub}; -is(ref $message_block, 'CODE', 'handle_log_request() got a sub ref for the message generator'); -is_deeply($results->{message}, \%message_info, 'handle_log_request() other values are correct'); +is(ref $message_block, + 'CODE', 'handle_log_request() got a sub ref for the message generator'); +is_deeply($results->{message}, \%message_info, + 'handle_log_request() other values are correct'); -done_testing; \ No newline at end of file +done_testing; diff --git a/t/simplelogger.t b/t/simplelogger.t index 793c24d..842163a 100644 --- a/t/simplelogger.t +++ b/t/simplelogger.t @@ -4,66 +4,101 @@ use warnings; use File::Temp; use Log::Contextual::SimpleLogger; use Log::Contextual qw{:log set_logger} => -logger => - Log::Contextual::SimpleLogger->new({levels => [qw{debug}]}); + Log::Contextual::SimpleLogger->new({levels => [qw{debug}]}); use Test::More qw(no_plan); my $l = Log::Contextual::SimpleLogger->new({levels => [qw{debug}]}); ok(!$l->is_trace, 'is_trace is false on SimpleLogger'); -ok($l->is_debug, 'is_debug is true on SimpleLogger'); -ok(!$l->is_info, 'is_info is false on SimpleLogger'); -ok(!$l->is_warn, 'is_warn is false on SimpleLogger'); +ok($l->is_debug, 'is_debug is true on SimpleLogger'); +ok(!$l->is_info, 'is_info is false on SimpleLogger'); +ok(!$l->is_warn, 'is_warn is false on SimpleLogger'); ok(!$l->is_error, 'is_error is false on SimpleLogger'); ok(!$l->is_fatal, 'is_fatal is false on SimpleLogger'); -ok(eval { log_trace { die 'this should live' }; 1}, 'trace does not get called'); -ok(!eval { log_debug { die 'this should die' }; 1}, 'debug gets called'); -ok(eval { log_info { die 'this should live' }; 1}, 'info does not get called'); -ok(eval { log_warn { die 'this should live' }; 1}, 'warn does not get called'); -ok(eval { log_error { die 'this should live' }; 1}, 'error does not get called'); -ok(eval { log_fatal { die 'this should live' }; 1}, 'fatal does not get called'); +ok( + eval { + log_trace { die 'this should live' }; + 1 + }, + 'trace does not get called' +); +ok( + !eval { + log_debug { die 'this should die' }; + 1 + }, + 'debug gets called' +); +ok( + eval { + log_info { die 'this should live' }; + 1 + }, + 'info does not get called' +); +ok( + eval { + log_warn { die 'this should live' }; + 1 + }, + 'warn does not get called' +); +ok( + eval { + log_error { die 'this should live' }; + 1 + }, + 'error does not get called' +); +ok( + eval { + log_fatal { die 'this should live' }; + 1 + }, + 'fatal does not get called' +); { - my $tempfile = File::Temp->new (UNLINK => 1, TEMPLATE => 'stderrXXXXXX'); - my $fn = fileno ($tempfile); - open (STDERR, ">&$fn") or die $!; - log_debug { 'frew' }; + my $tempfile = File::Temp->new(UNLINK => 1, TEMPLATE => 'stderrXXXXXX'); + my $fn = fileno($tempfile); + open(STDERR, ">&$fn") or die $!; + log_debug { 'frew' }; - my $out = do { local @ARGV = $tempfile; <> }; - is($out, "[debug] frew\n", 'SimpleLogger outputs to STDERR correctly'); + my $out = do { local @ARGV = $tempfile; <> }; + is($out, "[debug] frew\n", 'SimpleLogger outputs to STDERR correctly'); } - my $response; my $l2 = Log::Contextual::SimpleLogger->new({ - levels => [qw{trace debug info warn error fatal}], - coderef => sub { $response = $_[0] }, + levels => [qw{trace debug info warn error fatal}], + coderef => sub { $response = $_[0] }, }); { - local $SIG{__WARN__} = sub {}; # do this just to hide warning for tests + local $SIG{__WARN__} = sub { }; # do this just to hide warning for tests set_logger($l2); } log_trace { 'trace' }; is($response, "[trace] trace\n", 'trace renders correctly'); log_debug { 'debug' }; is($response, "[debug] debug\n", 'debug renders correctly'); -log_info { 'info' }; +log_info { 'info' }; is($response, "[info] info\n", 'info renders correctly'); -log_warn { 'warn' }; +log_warn { 'warn' }; is($response, "[warn] warn\n", 'warn renders correctly'); log_error { 'error' }; is($response, "[error] error\n", 'error renders correctly'); log_fatal { 'fatal' }; is($response, "[fatal] fatal\n", 'fatal renders correctly'); -log_debug { 'line 1', 'line 2' }; +log_debug {'line 1', 'line 2'}; is($response, "[debug] line 1\nline 2\n", 'multiline log renders correctly'); my $u = Log::Contextual::SimpleLogger->new({levels_upto => 'debug'}); ok(!$u->is_trace, 'is_trace is false on SimpleLogger'); -ok($u->is_debug, 'is_debug is true on SimpleLogger'); -ok($u->is_info, 'is_info is true on SimpleLogger'); -ok($u->is_warn, 'is_warn is true on SimpleLogger'); -ok($u->is_error, 'is_error is true on SimpleLogger'); -ok($u->is_fatal, 'is_fatal is true on SimpleLogger'); +ok($u->is_debug, 'is_debug is true on SimpleLogger'); +ok($u->is_info, 'is_info is true on SimpleLogger'); +ok($u->is_warn, 'is_warn is true on SimpleLogger'); +ok($u->is_error, 'is_error is true on SimpleLogger'); +ok($u->is_fatal, 'is_fatal is true on SimpleLogger'); diff --git a/t/warnlogger-with-levels.t b/t/warnlogger-with-levels.t index 1d7cad7..0741feb 100644 --- a/t/warnlogger-with-levels.t +++ b/t/warnlogger-with-levels.t @@ -1,128 +1,119 @@ use strict; use warnings; -use Log::Contextual::WarnLogger; # -levels => [qw(custom1 custom2)]; +use Log::Contextual::WarnLogger; # -levels => [qw(custom1 custom2)]; use Log::Contextual qw{:log set_logger} => -logger => - Log::Contextual::WarnLogger->new({ env_prefix => 'FOO' }); + Log::Contextual::WarnLogger->new({env_prefix => 'FOO'}); use Test::More qw(no_plan); use Test::Fatal; { - my $l; - like( - exception { $l = Log::Contextual::WarnLogger->new({ levels => '' }) }, - qr/invalid levels specification: must be non-empty arrayref/, - 'cannot pass empty string for levels', - ); - - like( - exception { $l = Log::Contextual::WarnLogger->new({ levels => [] }) }, - qr/invalid levels specification: must be non-empty arrayref/, - 'cannot pass empty list for levels', - ); - - is( - exception { $l = Log::Contextual::WarnLogger->new({ levels => undef, env_prefix => 'FOO' }) }, - undef, - 'ok to leave levels undefined', - ); + my $l; + like( + exception { $l = Log::Contextual::WarnLogger->new({levels => ''}) }, + qr/invalid levels specification: must be non-empty arrayref/, + 'cannot pass empty string for levels', + ); + + like( + exception { $l = Log::Contextual::WarnLogger->new({levels => []}) }, + qr/invalid levels specification: must be non-empty arrayref/, + 'cannot pass empty list for levels', + ); + + is( + exception { + $l = Log::Contextual::WarnLogger->new( + {levels => undef, env_prefix => 'FOO'}) + }, + undef, + 'ok to leave levels undefined', + ); } - { - my $l = Log::Contextual::WarnLogger->new({ - env_prefix => 'BAR', - levels => [qw(custom1 custom2)] - }); - - foreach my $sub (qw(is_custom1 is_custom2 custom1 custom2)) - { - is( - exception { $l->$sub }, - undef, - $sub . ' is handled by AUTOLOAD', - ); - } - - foreach my $sub (qw(is_foo foo)) - { - is( - exception { $l->$sub }, - undef, - 'arbitrary sub ' . $sub . ' is handled by AUTOLOAD', - ); - } + my $l = Log::Contextual::WarnLogger->new({ + env_prefix => 'BAR', + levels => [qw(custom1 custom2)]}); + + foreach my $sub (qw(is_custom1 is_custom2 custom1 custom2)) { + is(exception { $l->$sub }, undef, $sub . ' is handled by AUTOLOAD',); + } + + foreach my $sub (qw(is_foo foo)) { + is( + exception { $l->$sub }, + undef, 'arbitrary sub ' . $sub . ' is handled by AUTOLOAD', + ); + } } { - # levels is optional - most things should still work otherwise. - my $l = Log::Contextual::WarnLogger->new({ - env_prefix => 'BAR', - }); - - # if we don't know the level, and there are no environment variables set, - # just log everything. - { - ok($l->is_custom1, 'is_custom1 defaults to true on WarnLogger'); - ok($l->is_custom2, 'is_custom2 defaults to true on WarnLogger'); - } - - # otherwise, go with what the variable says. - { - local $ENV{BAR_CUSTOM1} = 0; - local $ENV{BAR_CUSTOM2} = 1; - ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger'); - ok($l->is_custom2, 'is_custom2 is true on WarnLogger'); - - ok($l->is_foo, 'is_foo defaults to true on WarnLogger'); - - local $ENV{BAR_UPTO} = 'foo'; - like( - exception { $l->is_bar }, - qr/Unrecognized log level 'foo' in \$ENV{BAR_UPTO}/, - 'Cannot use an unrecognized log level in UPTO', - ); - } + # levels is optional - most things should still work otherwise. + my $l = Log::Contextual::WarnLogger->new({env_prefix => 'BAR',}); + + # if we don't know the level, and there are no environment variables set, + # just log everything. + { + ok($l->is_custom1, 'is_custom1 defaults to true on WarnLogger'); + ok($l->is_custom2, 'is_custom2 defaults to true on WarnLogger'); + } + + # otherwise, go with what the variable says. + { + local $ENV{BAR_CUSTOM1} = 0; + local $ENV{BAR_CUSTOM2} = 1; + ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger'); + ok($l->is_custom2, 'is_custom2 is true on WarnLogger'); + + ok($l->is_foo, 'is_foo defaults to true on WarnLogger'); + + local $ENV{BAR_UPTO} = 'foo'; + like( + exception { $l->is_bar }, + qr/Unrecognized log level 'foo' in \$ENV{BAR_UPTO}/, + 'Cannot use an unrecognized log level in UPTO', + ); + } } # these tests taken from t/warnlogger.t my $l = Log::Contextual::WarnLogger->new({ - env_prefix => 'BAR', - levels => [qw(custom1 custom2)] -}); + env_prefix => 'BAR', + levels => [qw(custom1 custom2)]}); { - local $ENV{BAR_CUSTOM1} = 0; - local $ENV{BAR_CUSTOM2} = 1; - ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger'); - ok($l->is_custom2, 'is_custom2 is true on WarnLogger'); + local $ENV{BAR_CUSTOM1} = 0; + local $ENV{BAR_CUSTOM2} = 1; + ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger'); + ok($l->is_custom2, 'is_custom2 is true on WarnLogger'); - ok(!$l->is_foo, 'is_foo is false (custom levels supplied) on WarnLogger'); + ok(!$l->is_foo, 'is_foo is false (custom levels supplied) on WarnLogger'); } { - local $ENV{BAR_UPTO} = 'custom1'; + local $ENV{BAR_UPTO} = 'custom1'; - ok($l->is_custom1, 'is_custom1 is true on WarnLogger'); - ok($l->is_custom2, 'is_custom2 is true on WarnLogger'); + ok($l->is_custom1, 'is_custom1 is true on WarnLogger'); + ok($l->is_custom2, 'is_custom2 is true on WarnLogger'); } { - local $ENV{BAR_UPTO} = 'custom2'; + local $ENV{BAR_UPTO} = 'custom2'; - ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger'); - ok($l->is_custom2, 'is_custom2 is true on WarnLogger'); + ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger'); + ok($l->is_custom2, 'is_custom2 is true on WarnLogger'); } { - local $ENV{BAR_UPTO} = 'foo'; + local $ENV{BAR_UPTO} = 'foo'; - like( - exception { $l->is_custom1 }, - qr/Unrecognized log level 'foo'/, - 'Cannot use an unrecognized log level in UPTO', - ); + like( + exception { $l->is_custom1 }, + qr/Unrecognized log level 'foo'/, + 'Cannot use an unrecognized log level in UPTO', + ); } diff --git a/t/warnlogger.t b/t/warnlogger.t index 1a07292..53d9aa7 100644 --- a/t/warnlogger.t +++ b/t/warnlogger.t @@ -3,21 +3,21 @@ use warnings; use Log::Contextual::WarnLogger; use Log::Contextual qw{:log set_logger} => -logger => - Log::Contextual::WarnLogger->new({ env_prefix => 'FOO' }); + Log::Contextual::WarnLogger->new({env_prefix => 'FOO'}); use Test::More qw(no_plan); -my $l = Log::Contextual::WarnLogger->new({ env_prefix => 'BAR' }); +my $l = Log::Contextual::WarnLogger->new({env_prefix => 'BAR'}); { local $ENV{BAR_TRACE} = 0; local $ENV{BAR_DEBUG} = 1; - local $ENV{BAR_INFO} = 0; - local $ENV{BAR_WARN} = 0; + local $ENV{BAR_INFO} = 0; + local $ENV{BAR_WARN} = 0; local $ENV{BAR_ERROR} = 0; local $ENV{BAR_FATAL} = 0; ok(!$l->is_trace, 'is_trace is false on WarnLogger'); - ok($l->is_debug, 'is_debug is true on WarnLogger'); - ok(!$l->is_info, 'is_info is false on WarnLogger'); - ok(!$l->is_warn, 'is_warn is false on WarnLogger'); + ok($l->is_debug, 'is_debug is true on WarnLogger'); + ok(!$l->is_info, 'is_info is false on WarnLogger'); + ok(!$l->is_warn, 'is_warn is false on WarnLogger'); ok(!$l->is_error, 'is_error is false on WarnLogger'); ok(!$l->is_fatal, 'is_fatal is false on WarnLogger'); } @@ -27,8 +27,8 @@ my $l = Log::Contextual::WarnLogger->new({ env_prefix => 'BAR' }); ok($l->is_trace, 'is_trace is true on WarnLogger'); ok($l->is_debug, 'is_debug is true on WarnLogger'); - ok($l->is_info, 'is_info is true on WarnLogger'); - ok($l->is_warn, 'is_warn is true on WarnLogger'); + ok($l->is_info, 'is_info is true on WarnLogger'); + ok($l->is_warn, 'is_warn is true on WarnLogger'); ok($l->is_error, 'is_error is true on WarnLogger'); ok($l->is_fatal, 'is_fatal is true on WarnLogger'); } @@ -38,32 +38,68 @@ my $l = Log::Contextual::WarnLogger->new({ env_prefix => 'BAR' }); ok(!$l->is_trace, 'is_trace is false on WarnLogger'); ok(!$l->is_debug, 'is_debug is false on WarnLogger'); - ok(!$l->is_info, 'is_info is false on WarnLogger'); - ok($l->is_warn, 'is_warn is true on WarnLogger'); - ok($l->is_error, 'is_error is true on WarnLogger'); - ok($l->is_fatal, 'is_fatal is true on WarnLogger'); + ok(!$l->is_info, 'is_info is false on WarnLogger'); + ok($l->is_warn, 'is_warn is true on WarnLogger'); + ok($l->is_error, 'is_error is true on WarnLogger'); + ok($l->is_fatal, 'is_fatal is true on WarnLogger'); } { local $ENV{FOO_TRACE} = 0; local $ENV{FOO_DEBUG} = 1; - local $ENV{FOO_INFO} = 0; - local $ENV{FOO_WARN} = 0; + local $ENV{FOO_INFO} = 0; + local $ENV{FOO_WARN} = 0; local $ENV{FOO_ERROR} = 0; local $ENV{FOO_FATAL} = 0; - ok(eval { log_trace { die 'this should live' }; 1}, 'trace does not get called'); - ok(!eval { log_debug { die 'this should die' }; 1}, 'debug gets called'); - ok(eval { log_info { die 'this should live' }; 1}, 'info does not get called'); - ok(eval { log_warn { die 'this should live' }; 1}, 'warn does not get called'); - ok(eval { log_error { die 'this should live' }; 1}, 'error does not get called'); - ok(eval { log_fatal { die 'this should live' }; 1}, 'fatal does not get called'); + ok( + eval { + log_trace { die 'this should live' }; + 1 + }, + 'trace does not get called' + ); + ok( + !eval { + log_debug { die 'this should die' }; + 1 + }, + 'debug gets called' + ); + ok( + eval { + log_info { die 'this should live' }; + 1 + }, + 'info does not get called' + ); + ok( + eval { + log_warn { die 'this should live' }; + 1 + }, + 'warn does not get called' + ); + ok( + eval { + log_error { die 'this should live' }; + 1 + }, + 'error does not get called' + ); + ok( + eval { + log_fatal { die 'this should live' }; + 1 + }, + 'fatal does not get called' + ); } { local $ENV{FOO_TRACE} = 1; local $ENV{FOO_DEBUG} = 1; - local $ENV{FOO_INFO} = 1; - local $ENV{FOO_WARN} = 1; + local $ENV{FOO_INFO} = 1; + local $ENV{FOO_WARN} = 1; local $ENV{FOO_ERROR} = 1; local $ENV{FOO_FATAL} = 1; my $cap; @@ -75,9 +111,9 @@ my $l = Log::Contextual::WarnLogger->new({ env_prefix => 'BAR' }); is($cap, "[trace] trace\n", 'trace renders correctly'); log_debug { 'debug' }; is($cap, "[debug] debug\n", 'debug renders correctly'); - log_info { 'info' }; + log_info { 'info' }; is($cap, "[info] info\n", 'info renders correctly'); - log_warn { 'warn' }; + log_warn { 'warn' }; is($cap, "[warn] warn\n", 'warn renders correctly'); log_error { 'error' }; is($cap, "[error] error\n", 'error renders correctly');