Debian Codegraph @

t/unit/logger/levels.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Fatal qw<exception>;
6
7 use Dancer2::Logger::Capture;
8
9 # Which messages get through, and what a message turns into on the way.
10 #
11 # The level table in Dancer2::Core::Role::Logger puts 'core' at -10 and the
12 # user-facing levels at 1..4, and _should is a single numeric comparison:
13 #
14 # $levels->{configured} <= $levels->{message}
15 #
16 # So 'core' is below every user level by construction, and the only way to see
17 # core messages is to configure log_level: core. That is what keeps Dancer2's
18 # own internal chatter ("looking for get /", "Entering hook ...") out of an
19 # application's log.
20 #
21 # Dancer2::Logger::Capture is used as the engine: it stores messages instead of
22 # printing them and ships as a supported part of this distribution, so no
23 # stand-in is involved.
24
25 my @LEVELS = qw< core debug info warning error >;
26
27 sub logger_at {
28 my $level = shift;
29 return Dancer2::Logger::Capture->new( log_level => $level, config => {} );
30 }
31
32 # Emit one message at every level, return the levels that actually got through.
33 sub emitted_under {
34 my $configured = shift;
35 my $logger = logger_at($configured);
36
37 $logger->$_("message-at-$_") for @LEVELS;
38
39 return [ map { $_->{level} } @{ $logger->trapper->read } ];
40 }
41
42 subtest 'a level lets through itself and everything above it' => sub {
43 # Written out in full rather than computed, so the expectation is readable
44 # and a change has to be made deliberately.
45 my %expected = (
46 core => [qw< core debug info warning error >],
47 debug => [qw< debug info warning error >],
48 info => [qw< info warning error >],
49 warning => [qw< warning error >],
50 error => [qw< error >],
51 );
52
53 for my $configured ( @LEVELS ) {
54 is_deeply( emitted_under($configured), $expected{$configured},
55 "log_level '$configured' emits exactly @{$expected{$configured}}" );
56 }
57 };
58
59 subtest 'core messages are hidden from every user-facing level' => sub {
60 # The point of the negative level. Dancer2 logs its own dispatch trace at
61 # core, and an application that asked for debug must not receive it.
62 for my $configured ( qw< debug info warning error > ) {
63 my $got = emitted_under($configured);
64 ok( !grep( { $_ eq 'core' } @$got ),
65 "log_level '$configured' does not emit core messages" );
66 }
67
68 # And the control: they are not simply never emitted.
69 my $got = emitted_under('core');
70 ok( scalar grep( { $_ eq 'core' } @$got ),
71 "log_level 'core' does emit them" );
72 };
73
74 subtest 'warn is accepted as a synonym for warning' => sub {
75 # 'warn' is in the level table at the same value as 'warning', so it is a
76 # legal log_level even though there is no warn() method.
77 my $logger = logger_at('warn');
78
79 $logger->debug('below');
80 $logger->warning('at');
81 $logger->error('above');
82
83 is_deeply( [ map { $_->{level} } @{ $logger->trapper->read } ],
84 [ 'warning', 'error' ],
85 "log_level 'warn' filters exactly as 'warning' does" );
86 };
87
88 subtest 'an unknown level is refused at construction' => sub {
89 # Better than silently defaulting: a typo in the config is fatal rather
90 # than quietly turning logging up or off.
91 my $err = exception {
92 Dancer2::Logger::Capture->new( log_level => 'verbose', config => {} );
93 };
94
95 ok( defined $err, "log_level 'verbose' is refused" );
96 like( $err, qr/verbose/, 'and the value is named in the error' );
97
98 # Every level in the table is accepted, including the synonym.
99 for my $level ( @LEVELS, 'warn' ) {
100 is( exception {
101 Dancer2::Logger::Capture->new( log_level => $level, config => {} )
102 },
103 undef,
104 "log_level '$level' is accepted",
105 );
106 }
107 };
108
109 subtest 'message arguments are serialised for logging' => sub {
110 my $logger = Dancer2::Logger::Capture->new(
111 log_format => '[%m]',
112 config => {},
113 );
114
115 # A trailing newline is chomped, so a log line is not double-spaced.
116 $logger->info("trailing newline\n");
117
118 # A reference is dumped compactly, with keys sorted so the output is
119 # deterministic.
120 $logger->info( { b => 2, a => 1 } );
121
122 # Several arguments are concatenated, and undef becomes the string 'undef'
123 # rather than triggering a warning.
124 $logger->info( 'x', 'y', undef );
125
126 my @formatted = map { $_->{formatted} } @{ $logger->trapper->read };
127 chomp @formatted;
128
129 is( $formatted[0], '[trailing newline]',
130 'a trailing newline is removed from the message' );
131 is( $formatted[1], "[{'a' => 1,'b' => 2}]",
132 'a hashref is dumped on one line with sorted keys' );
133 is( $formatted[2], '[xyundef]',
134 'multiple arguments concatenate and undef becomes "undef"' );
135 };
136
137 subtest 'a formatted line always ends with a newline' => sub {
138 # The engines write the formatted string as-is, so the trailing newline has
139 # to come from format_message.
140 my $logger = Dancer2::Logger::Capture->new(
141 log_format => '%L: %m',
142 config => {},
143 );
144
145 $logger->info('no newline');
146 $logger->info("one newline\n");
147 $logger->info("two newlines\n\n");
148 $logger->info("newline\nin the middle");
149
150 my @formatted = map { $_->{formatted} } @{ $logger->trapper->read };
151
152 like( $_, qr/\n\z/, 'the line ends with a newline' ) for @formatted;
153
154 is( $formatted[0], "info: no newline\n",
155 'a message with no newline gets exactly one' );
156 is( $formatted[1], "info: one newline\n",
157 'a message with one trailing newline still gets exactly one' );
158
159 # chomp removes a single trailing newline, so a message that ended with two
160 # produces a blank line. Pinned as current behavior: it is a cosmetic wart,
161 # not a correctness problem, and the fix would be a chomp-while loop.
162 is( $formatted[2], "info: two newlines\n\n",
163 'a message ending in two newlines leaves a blank line behind' );
164
165 # An interior newline is untouched - only the trailing one is chomped.
166 is( $formatted[3], "info: newline\nin the middle\n",
167 'a newline inside the message is left alone' );
168 };
169
170 done_testing();