4 use Log::Contextual::WarnLogger; # -levels => [qw(custom1 custom2)];
5 use Log::Contextual qw{:log set_logger} => -logger =>
6 Log::Contextual::WarnLogger->new({ env_prefix => 'FOO' });
8 use Test::More qw(no_plan);
14 exception { $l = Log::Contextual::WarnLogger->new({ levels => '' }) },
15 qr/invalid levels specification: must be non-empty arrayref/,
16 'cannot pass empty string for levels',
20 exception { $l = Log::Contextual::WarnLogger->new({ levels => [] }) },
21 qr/invalid levels specification: must be non-empty arrayref/,
22 'cannot pass empty list for levels',
26 exception { $l = Log::Contextual::WarnLogger->new({ levels => undef, env_prefix => 'FOO' }) },
28 'ok to leave levels undefined',
34 my $l = Log::Contextual::WarnLogger->new({
36 levels => [qw(custom1 custom2)]
39 foreach my $sub (qw(is_custom1 is_custom2 custom1 custom2))
42 exception { $l->$sub },
44 $sub . ' is handled by AUTOLOAD',
48 foreach my $sub (qw(is_foo foo))
51 exception { $l->$sub },
53 'arbitrary sub ' . $sub . ' is handled by AUTOLOAD',
59 # levels is optional - most things should still work otherwise.
60 my $l = Log::Contextual::WarnLogger->new({
64 # if we don't know the level, and there are no environment variables set,
65 # just log everything.
67 ok($l->is_custom1, 'is_custom1 defaults to true on WarnLogger');
68 ok($l->is_custom2, 'is_custom2 defaults to true on WarnLogger');
71 # otherwise, go with what the variable says.
73 local $ENV{BAR_CUSTOM1} = 0;
74 local $ENV{BAR_CUSTOM2} = 1;
75 ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger');
76 ok($l->is_custom2, 'is_custom2 is true on WarnLogger');
78 ok($l->is_foo, 'is_foo defaults to true on WarnLogger');
80 local $ENV{BAR_UPTO} = 'foo';
82 exception { $l->is_bar },
83 qr/Unrecognized log level 'foo' in \$ENV{BAR_UPTO}/,
84 'Cannot use an unrecognized log level in UPTO',
89 # these tests taken from t/warnlogger.t
91 my $l = Log::Contextual::WarnLogger->new({
93 levels => [qw(custom1 custom2)]
97 local $ENV{BAR_CUSTOM1} = 0;
98 local $ENV{BAR_CUSTOM2} = 1;
99 ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger');
100 ok($l->is_custom2, 'is_custom2 is true on WarnLogger');
102 ok(!$l->is_foo, 'is_foo is false (custom levels supplied) on WarnLogger');
106 local $ENV{BAR_UPTO} = 'custom1';
108 ok($l->is_custom1, 'is_custom1 is true on WarnLogger');
109 ok($l->is_custom2, 'is_custom2 is true on WarnLogger');
113 local $ENV{BAR_UPTO} = 'custom2';
115 ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger');
116 ok($l->is_custom2, 'is_custom2 is true on WarnLogger');
120 local $ENV{BAR_UPTO} = 'foo';
123 exception { $l->is_custom1 },
124 qr/Unrecognized log level 'foo'/,
125 'Cannot use an unrecognized log level in UPTO',