make it more clear that "with_logger" will not override package_logger
[p5sagit/Log-Contextual.git] / t / warnlogger-with-levels.t
CommitLineData
e7b18282 1use strict;
2use warnings;
3
6ae293d7 4use Log::Contextual::WarnLogger; # -levels => [qw(custom1 custom2)];
e7b18282 5use Log::Contextual qw{:log set_logger} => -logger =>
6ae293d7 6 Log::Contextual::WarnLogger->new({env_prefix => 'FOO'});
e7b18282 7
8use Test::More qw(no_plan);
9use Test::Fatal;
10
11{
6ae293d7 12 my $l;
13 like(
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',
17 );
18
19 like(
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',
23 );
24
25 is(
26 exception {
27 $l = Log::Contextual::WarnLogger->new(
28 {levels => undef, env_prefix => 'FOO'})
29 },
30 undef,
31 'ok to leave levels undefined',
32 );
e7b18282 33}
34
e7b18282 35{
6ae293d7 36 my $l = Log::Contextual::WarnLogger->new({
37 env_prefix => 'BAR',
38 levels => [qw(custom1 custom2)]});
39
40 foreach my $sub (qw(is_custom1 is_custom2 custom1 custom2)) {
41 is(exception { $l->$sub }, undef, $sub . ' is handled by AUTOLOAD',);
42 }
43
44 foreach my $sub (qw(is_foo foo)) {
45 is(
46 exception { $l->$sub },
47 undef, 'arbitrary sub ' . $sub . ' is handled by AUTOLOAD',
48 );
49 }
e7b18282 50}
51
52{
6ae293d7 53 # levels is optional - most things should still work otherwise.
54 my $l = Log::Contextual::WarnLogger->new({env_prefix => 'BAR',});
55
56 # if we don't know the level, and there are no environment variables set,
57 # just log everything.
58 {
59 ok($l->is_custom1, 'is_custom1 defaults to true on WarnLogger');
60 ok($l->is_custom2, 'is_custom2 defaults to true on WarnLogger');
61 }
62
63 # otherwise, go with what the variable says.
64 {
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');
69
70 ok($l->is_foo, 'is_foo defaults to true on WarnLogger');
71
72 local $ENV{BAR_UPTO} = 'foo';
73 like(
74 exception { $l->is_bar },
75 qr/Unrecognized log level 'foo' in \$ENV{BAR_UPTO}/,
76 'Cannot use an unrecognized log level in UPTO',
77 );
78 }
e7b18282 79}
80
81# these tests taken from t/warnlogger.t
82
83my $l = Log::Contextual::WarnLogger->new({
6ae293d7 84 env_prefix => 'BAR',
85 levels => [qw(custom1 custom2)]});
e7b18282 86
87{
6ae293d7 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');
e7b18282 92
6ae293d7 93 ok(!$l->is_foo, 'is_foo is false (custom levels supplied) on WarnLogger');
e7b18282 94}
95
96{
6ae293d7 97 local $ENV{BAR_UPTO} = 'custom1';
e7b18282 98
6ae293d7 99 ok($l->is_custom1, 'is_custom1 is true on WarnLogger');
100 ok($l->is_custom2, 'is_custom2 is true on WarnLogger');
e7b18282 101}
102
103{
6ae293d7 104 local $ENV{BAR_UPTO} = 'custom2';
e7b18282 105
6ae293d7 106 ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger');
107 ok($l->is_custom2, 'is_custom2 is true on WarnLogger');
e7b18282 108}
109
110{
6ae293d7 111 local $ENV{BAR_UPTO} = 'foo';
e7b18282 112
6ae293d7 113 like(
114 exception { $l->is_custom1 },
115 qr/Unrecognized log level 'foo'/,
116 'Cannot use an unrecognized log level in UPTO',
117 );
e7b18282 118}
119