a0c372640f90eefbee9c7c358824fbdabeeff0c5
[p5sagit/Log-Contextual.git] / t / warnlogger.t
1 use strict;
2 use warnings;
3
4 use Log::Contextual::WarnLogger;
5 use Log::Contextual qw{:log set_logger} => -logger =>
6    Log::Contextual::WarnLogger->new({ env_prefix => 'FOO' });
7 use Test::More qw(no_plan);
8 my $l = Log::Contextual::WarnLogger->new({ env_prefix => 'BAR' });
9
10 {
11    local $ENV{BAR_TRACE} = 0;
12    local $ENV{BAR_DEBUG} = 1;
13    local $ENV{BAR_INFO} = 0;
14    local $ENV{BAR_WARN} = 0;
15    local $ENV{BAR_ERROR} = 0;
16    local $ENV{BAR_FATAL} = 0;
17    ok(!$l->is_trace, 'is_trace is false on WarnLogger');
18    ok($l->is_debug, 'is_debug is true on WarnLogger');
19    ok(!$l->is_info, 'is_info is false on WarnLogger');
20    ok(!$l->is_warn, 'is_warn is false on WarnLogger');
21    ok(!$l->is_error, 'is_error is false on WarnLogger');
22    ok(!$l->is_fatal, 'is_fatal is false on WarnLogger');
23 }
24
25 {
26    local $ENV{FOO_TRACE} = 0;
27    local $ENV{FOO_DEBUG} = 1;
28    local $ENV{FOO_INFO} = 0;
29    local $ENV{FOO_WARN} = 0;
30    local $ENV{FOO_ERROR} = 0;
31    local $ENV{FOO_FATAL} = 0;
32    ok(eval { log_trace { die 'this should live' }; 1}, 'trace does not get called');
33    ok(!eval { log_debug { die 'this should die' }; 1}, 'debug gets called');
34    ok(eval { log_info { die 'this should live' }; 1}, 'info does not get called');
35    ok(eval { log_warn { die 'this should live' }; 1}, 'warn does not get called');
36    ok(eval { log_error { die 'this should live' }; 1}, 'error does not get called');
37    ok(eval { log_fatal { die 'this should live' }; 1}, 'fatal does not get called');
38 }
39
40 {
41    local $ENV{FOO_TRACE} = 1;
42    local $ENV{FOO_DEBUG} = 1;
43    local $ENV{FOO_INFO} = 1;
44    local $ENV{FOO_WARN} = 1;
45    local $ENV{FOO_ERROR} = 1;
46    local $ENV{FOO_FATAL} = 1;
47    my $cap;
48    local $SIG{__WARN__} = sub { $cap = shift };
49
50    log_debug { 'frew' };
51    is($cap, "[debug] frew\n", 'WarnLogger outputs to STDERR correctly');
52    log_trace { 'trace' };
53    is($cap, "[trace] trace\n", 'trace renders correctly');
54    log_debug { 'debug' };
55    is($cap, "[debug] debug\n", 'debug renders correctly');
56    log_info  { 'info'  };
57    is($cap, "[info] info\n", 'info renders correctly');
58    log_warn  { 'warn'  };
59    is($cap, "[warn] warn\n", 'warn renders correctly');
60    log_error { 'error' };
61    is($cap, "[error] error\n", 'error renders correctly');
62    log_fatal { 'fatal' };
63    is($cap, "[fatal] fatal\n", 'fatal renders correctly');
64
65 }