File: easy.t

package info (click to toggle)
liblog-contextual-perl 0.007000-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 320 kB
  • ctags: 75
  • sloc: perl: 615; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 1,709 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use strict;
use warnings;

use Test::More;

use lib 't/lib';
use My::Module;     # makes use of Log::Contextual::Easy::Default;
use My::Module2;    # makes use of Log::Contextual::Easy::Package;

# capture logging messages of My::Module, mapping "[...] xxx" to "...$sep"
sub logshort($$) {
   my ($cap, $sep) = @_;
   sub {
      local $_ = shift;
      s/^\[(.+)\] (xxx|"xxx")\n$/$1$sep/;
      $$cap .= $_;
     }
}

# capture warnings
my ($cap_warn, $cap_with, $cap_set);
local $SIG{__WARN__} = logshort \$cap_warn, '!';

{
   My::Module::log();
   My::Module2::log();
   is($cap_warn, undef, 'no logging by default');
}

{
   local $ENV{MY_MODULE_UPTO}  = 'info';
   local $ENV{MY_MODULE2_UPTO} = 'info';
   My::Module::log();
   My::Module2::log();
   is(
      $cap_warn,
      "info!warn!error!fatal!info!warn!error!fatal!",
      'WarnLogger enabled via ENV'
   );
   $cap_warn = '';
}

{
   use Log::Contextual::SimpleLogger;
   use Log::Contextual qw(with_logger set_logger);

   set_logger(
      Log::Contextual::SimpleLogger->new({
            levels  => [qw(info warn error)],
            coderef => logshort \$cap_set,
            '/'
         }));

   my $with_logger = Log::Contextual::SimpleLogger->new({
      levels  => [qw(trace info fatal)],
      coderef => logshort \$cap_with,
      '|'
   });

   with_logger $with_logger => sub {
      My::Module::log();
      My::Module2::log();    # will not be overridden
   };
   is($cap_with, 'trace|info|fatal|', 'with_logger');

   My::Module::log();
   My::Module2::log();       # will not be overridden
   is($cap_set, 'info/warn/error/', 'set_logger');

   is($cap_warn, '', 'no warnings if with_logger or set_logger');
}

done_testing;