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