make it more clear that "with_logger" will not override package_logger
[p5sagit/Log-Contextual.git] / t / warnlogger-with-levels.t
1 use strict;
2 use warnings;
3
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'});
7
8 use Test::More qw(no_plan);
9 use Test::Fatal;
10
11 {
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    );
33 }
34
35 {
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    }
50 }
51
52 {
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    }
79 }
80
81 # these tests taken from t/warnlogger.t
82
83 my $l = Log::Contextual::WarnLogger->new({
84       env_prefix => 'BAR',
85       levels     => [qw(custom1 custom2)]});
86
87 {
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');
92
93    ok(!$l->is_foo, 'is_foo is false (custom levels supplied) on WarnLogger');
94 }
95
96 {
97    local $ENV{BAR_UPTO} = 'custom1';
98
99    ok($l->is_custom1, 'is_custom1 is true on WarnLogger');
100    ok($l->is_custom2, 'is_custom2 is true on WarnLogger');
101 }
102
103 {
104    local $ENV{BAR_UPTO} = 'custom2';
105
106    ok(!$l->is_custom1, 'is_custom1 is false on WarnLogger');
107    ok($l->is_custom2,  'is_custom2 is true on WarnLogger');
108 }
109
110 {
111    local $ENV{BAR_UPTO} = 'foo';
112
113    like(
114       exception { $l->is_custom1 },
115       qr/Unrecognized log level 'foo'/,
116       'Cannot use an unrecognized log level in UPTO',
117    );
118 }
119