4 use Log::Contextual qw{:log with_logger set_logger};
5 use Log::Contextual::SimpleLogger;
6 use Test::More qw(no_plan);
8 my @levels = qw(debug trace warn info error fatal);
13 my $var_logger1 = Log::Contextual::SimpleLogger->new({
14 levels => [qw(trace debug info warn error fatal)],
15 coderef => sub { $var1 = shift },
18 my $var_logger2 = Log::Contextual::SimpleLogger->new({
19 levels => [qw(trace debug info warn error fatal)],
20 coderef => sub { $var2 = shift },
23 my $var_logger3 = Log::Contextual::SimpleLogger->new({
24 levels => [qw(trace debug info warn error fatal)],
25 coderef => sub { $var3 = shift },
29 set_logger(sub { $var_logger3 });
30 log_debug { 'set_logger' };
31 is($var3, "[debug] set_logger\n", 'set logger works');
36 local $SIG{__WARN__} = sub { $foo = shift };
37 set_logger(sub { $var_logger3 });
40 qr/set_logger \(or -logger\) called more than once! This is a bad idea! at/,
41 'set_logger twice warns correctly'
46 with_logger sub { $var_logger2 } => sub {
48 with_logger $var_logger1 => sub {
49 log_debug { 'nothing!' }
51 log_debug { 'frew!' };
55 is($var1, "[debug] nothing!\n", 'inner scoped logger works');
56 is($var2, "[debug] frew!\n", 'outer scoped logger works');
60 with_logger $var_logger1 => sub {
61 log_debug { 'nothing again!' };
63 # do this just so the following set_logger won't warn
64 local $SIG{__WARN__} = sub { };
65 set_logger(sub { $var_logger3 });
66 log_debug { 'this is a set inside a with' };
71 "[debug] nothing again!\n",
72 'inner scoped logger works after using set_logger'
75 is($var3, "[debug] this is a set inside a with\n", 'set inside with works');
77 log_debug { 'frioux!' };
81 q{set_logger's logger comes back after scoped logger}
87 main->can("log_$_")->(sub { 'fiSMBoC' });
88 is($var3, "[$_] fiSMBoC\n", "$_ works");
91 main->can("log_$_")->(sub { 'fiSMBoC: ' . $_[1] }, qw{foo bar baz});
92 is($var3, "[$_] fiSMBoC: bar\n", "log_$_ works with input");
94 eq_array(\@vars, [qw{foo bar baz}]),
95 "log_$_ passes data through correctly"
98 my $val = main->can("logS_$_")->(sub { 'fiSMBoC: ' . $_[0] }, 'foo');
99 is($var3, "[$_] fiSMBoC: foo\n", "logS_$_ works with input");
100 is($val, 'foo', "logS_$_ passes data through correctly");
104 ok(!eval { Log::Contextual->import; 1 }, 'Blank Log::Contextual import dies');