1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
use strict;
use warnings;
use Log::Contextual::WarnLogger; # -levels => [qw(custom1 custom2)];
use Log::Contextual qw{:log set_logger} => -logger =>
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 = 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',
);
}
}
# these tests taken from t/warnlogger.t
my $l = Log::Contextual::WarnLogger->new({
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');
ok(!$l->is_foo, 'is_foo is false (custom levels supplied) on WarnLogger');
}
{
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');
}
{
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');
}
{
local $ENV{BAR_UPTO} = 'foo';
like(
exception { $l->is_custom1 },
qr/Unrecognized log level 'foo'/,
'Cannot use an unrecognized log level in UPTO',
);
}
|