} elsif ( $val eq '-logger' ) {
set_logger($_[$idx + 1]);
$skipnext = 1;
+ } elsif ( $val eq '-args' ) {
+ _set_package_logger_args_for(scalar caller, $_[$idx + 1]);
+ $skipnext = 1;
} elsif ( $val eq '-package_logger' ) {
_set_package_logger_for(scalar caller, $_[$idx + 1]);
$skipnext = 1;
our $Get_Logger;
our %Default_Logger;
our %Package_Logger;
+our %Package_Logger_Args;
sub _set_default_logger_for {
my $logger = $_[1];
$Default_Logger{$_[0]} = $logger
}
+sub _set_package_logger_args_for {
+ my $args = $_[1];
+ die 'args are not an arrayref. Please try again.'
+ unless ref $args eq 'ARRAY';
+ $Package_Logger_Args{$_[0]} = $args
+}
+
sub _set_package_logger_for {
my $logger = $_[1];
if(ref $logger ne 'CODE') {
$Get_Logger ||
$Default_Logger{$package} ||
die q( no logger set! you can't try to log something without a logger! )
- )->($package);
+ )->({
+ package => $package,
+ args => $Package_Logger_Args{$package},
+ });
}
sub set_logger {
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More;
+
+my @out;
+
+# these shouldn't be packages, but I'm not sure how else I'm going to
+# do it yet.
+
+{
+ package CatLogger;
+
+ use Log::Contextual::NullLogger;
+ use base 'Log::Contextual::SimpleLogger';
+
+ sub include_category { $_[1] eq 'awesome' }
+
+ sub log_contextual_coderef {
+ my $logger = CatLogger->new({ levels_upto => 'trace', coderef => sub { push @out, $_[0] } });
+
+ return Log::Contextual::NullLogger->new
+ unless $logger->include_category($_[0]{args}[0]);
+ return $logger;
+ }
+
+ 1;
+}
+
+{
+ package Foo;
+
+ use Log::Contextual ':log', -args => ['lame'],
+ -package_logger => \&CatLogger::log_contextual_coderef;
+
+ log_debug { 'test' };
+}
+
+ok @out == 0, 'no output because category is lame';
+
+{
+ package Bar;
+ use Log::Contextual::NullLogger;
+
+ use Log::Contextual ':log', -args => ['awesome'],
+ -package_logger => \&CatLogger::log_contextual_coderef;
+
+ log_debug { 'test' };
+}
+
+ok @out == 1, 'output because category is awesome';
+
+done_testing;