e7440ccc22c239ed8adcbfc8ebd30ad08161ac9a
[p5sagit/Log-Contextual.git] / t / easy.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 use lib 't/lib';
7 use My::Module; # makes use of Log::Contextual::Easy;
8
9 # capture logging messages of My::Module, mapping "[...] xxx" to "...$sep"
10 sub logshort($$) {
11     my ($cap, $sep) = @_;
12     sub {
13         local $_ = shift;
14         s/^\[(.+)\] (xxx|"xxx")\n$/$1$sep/; 
15         $$cap .= $_;
16     }
17 }
18
19 # capture warnings
20 my ($cap_warn, $cap_with, $cap_set);
21 local $SIG{__WARN__} = logshort \$cap_warn, '!';
22
23 {
24     My::Module::log();
25     is($cap_warn, undef, 'no logging by default');
26 }
27
28 {
29     local $ENV{MY_MODULE_UPTO} = 'info';
30     My::Module::log();
31     is($cap_warn, "info!warn!error!fatal!", 'WarnLogger enabled via ENV');
32     $cap_warn = '';
33 }
34
35 {
36     use Log::Contextual::SimpleLogger;
37     use Log::Contextual qw(with_logger set_logger);
38
39     set_logger( Log::Contextual::SimpleLogger->new({
40         levels  => [qw(info warn error)],
41         coderef => logshort \$cap_set, '/'
42     }) );
43
44     my $with_logger = Log::Contextual::SimpleLogger->new({
45         levels  => [qw(trace info fatal)],
46         coderef => logshort \$cap_with, '|'
47     });
48     
49     with_logger $with_logger => sub {
50         My::Module::log();
51     };
52     is($cap_with, 'trace|info|fatal|', 'with_logger');
53
54     My::Module::log();
55     is($cap_set, 'info/warn/error/', 'set_logger');
56
57     is($cap_warn, '', 'no warnings if with_logger or set_logger');
58 }
59
60 done_testing;
61
62