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',
27 $l = Log::Contextual::WarnLogger->new(
28 {levels => undef, env_prefix => 'FOO'})
31 'ok to leave levels undefined',
36 my $l = Log::Contextual::WarnLogger->new({
38 levels => [qw(custom1 custom2)]});
40 foreach my $sub (qw(is_custom1 is_custom2 custom1 custom2)) {
41 is(exception { $l->$sub }, undef, $sub . ' is handled by AUTOLOAD',);
44 foreach my $sub (qw(is_foo foo)) {
46 exception { $l->$sub },
47 undef, 'arbitrary sub ' . $sub . ' is handled by AUTOLOAD',
53 # levels is optional - most things should still work otherwise.
54 my $l = Log::Contextual::WarnLogger->new({env_prefix => 'BAR',});
56 # if we don't know the level, and there are no environment variables set,
57 # just log everything.
59 ok($l->is_custom1, 'is_custom1 defaults to true on WarnLogger');
60 ok($l->is_custom2, 'is_custom2 defaults to true on WarnLogger');
63 # otherwise, go with what the variable says.
65 local $ENV{BAR_CUSTOM1} = 0;
66 local $ENV{BAR_CUSTOM2} = 1;
67 ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger');
68 ok($l->is_custom2, 'is_custom2 is true on WarnLogger');
70 ok($l->is_foo, 'is_foo defaults to true on WarnLogger');
72 local $ENV{BAR_UPTO} = 'foo';
74 exception { $l->is_bar },
75 qr/Unrecognized log level 'foo' in \$ENV{BAR_UPTO}/,
76 'Cannot use an unrecognized log level in UPTO',
81 # these tests taken from t/warnlogger.t
83 my $l = Log::Contextual::WarnLogger->new({
85 levels => [qw(custom1 custom2)]});
88 local $ENV{BAR_CUSTOM1} = 0;
89 local $ENV{BAR_CUSTOM2} = 1;
90 ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger');
91 ok($l->is_custom2, 'is_custom2 is true on WarnLogger');
93 ok(!$l->is_foo, 'is_foo is false (custom levels supplied) on WarnLogger');
97 local $ENV{BAR_UPTO} = 'custom1';
99 ok($l->is_custom1, 'is_custom1 is true on WarnLogger');
100 ok($l->is_custom2, 'is_custom2 is true on WarnLogger');
104 local $ENV{BAR_UPTO} = 'custom2';
106 ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger');
107 ok($l->is_custom2, 'is_custom2 is true on WarnLogger');
111 local $ENV{BAR_UPTO} = 'foo';
114 exception { $l->is_custom1 },
115 qr/Unrecognized log level 'foo'/,
116 'Cannot use an unrecognized log level in UPTO',