| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
|
| 6 |
use Dancer2::Logger::Capture; |
| 7 |
use Dancer2::Core::Request; |
| 8 |
|
| 9 |
# log_format substitution. Two syntaxes share one regex |
| 10 |
# (Dancer2/Core/Role/Logger.pm:110-116): |
| 11 |
# |
| 12 |
# %X a single-character code, looked up in map_chars_to_subs |
| 13 |
# %{thing}X a block code, handled by the block handler (only t and h) |
| 14 |
# |
| 15 |
# An unrecognised code in either syntax must warn and render '-' rather than |
| 16 |
# abort the log call: losing a log line because its format string has a typo in |
| 17 |
# it would be a poor trade. |
| 18 |
# |
| 19 |
# format_message is called directly rather than through a request, because the |
| 20 |
# substitutions are what is under test and a full dispatch would add nothing. |
| 21 |
|
| 22 |
my $REQUEST = do { |
| 23 |
my $body = ''; |
| 24 |
open my $input, '<', \$body or die "cannot open in-memory body: $!"; |
| 25 |
|
| 26 |
Dancer2::Core::Request->new( |
| 27 |
env => { |
| 28 |
REQUEST_METHOD => 'GET', |
| 29 |
PATH_INFO => '/some/path', |
| 30 |
QUERY_STRING => '', |
| 31 |
SCRIPT_NAME => '', |
| 32 |
SERVER_NAME => 'localhost', |
| 33 |
SERVER_PORT => 80, |
| 34 |
CONTENT_LENGTH => 0, |
| 35 |
REMOTE_ADDR => '10.0.0.9', |
| 36 |
HTTP_USER_AGENT => 'TestAgent/1.0', |
| 37 |
HTTP_X_CUSTOM => 'CUSTOM-HEADER-VALUE', |
| 38 |
'psgi.url_scheme' => 'http', |
| 39 |
'psgi.input' => $input, |
| 40 |
'psgi.errors' => \*STDERR, |
| 41 |
}, |
| 42 |
); |
| 43 |
}; |
| 44 |
|
| 45 |
# Format one message with the given format string, returning the rendered line |
| 46 |
# and any warnings it produced. |
| 47 |
sub render { |
| 48 |
my ( $format, %opt ) = @_; |
| 49 |
|
| 50 |
my $logger = Dancer2::Logger::Capture->new( |
| 51 |
log_format => $format, |
| 52 |
app_name => 'MyApp', |
| 53 |
config => {}, |
| 54 |
( exists $opt{caller_stack_size} |
| 55 |
? ( caller_stack_size => $opt{caller_stack_size} ) : () ), |
| 56 |
); |
| 57 |
$logger->set_request($REQUEST) unless $opt{no_request}; |
| 58 |
|
| 59 |
my @warnings; |
| 60 |
my $line = do { |
| 61 |
local $SIG{__WARN__} = sub { push @warnings, $_[0] }; |
| 62 |
$logger->format_message( 'warning', 'THE-MESSAGE' ); |
| 63 |
}; |
| 64 |
chomp $line; |
| 65 |
|
| 66 |
return ( $line, \@warnings ); |
| 67 |
} |
| 68 |
|
| 69 |
sub rendered { my ($line) = render(@_); return $line } |
| 70 |
|
| 71 |
subtest 'the message, level and app name substitute' => sub { |
| 72 |
is( rendered('%m'), 'THE-MESSAGE', '%m is the message' ); |
| 73 |
is( rendered('%L'), 'warning', '%L is the level' ); |
| 74 |
is( rendered('%a'), 'MyApp', '%a is the app name' ); |
| 75 |
is( rendered('%P'), $$, '%P is the process id' ); |
| 76 |
|
| 77 |
# Surrounding literal text is preserved, and several codes can appear in |
| 78 |
# one format. |
| 79 |
is( rendered('[%L] %m (%a)'), '[warning] THE-MESSAGE (MyApp)', |
| 80 |
'codes substitute in place, literals are kept' ); |
| 81 |
|
| 82 |
# An app that set no name gets the documented default. |
| 83 |
my $unnamed = Dancer2::Logger::Capture->new( |
| 84 |
log_format => '%a', config => {} ); |
| 85 |
is( $unnamed->format_message( 'info', 'x' ), "-\n", |
| 86 |
'the app name defaults to "-"' ); |
| 87 |
}; |
| 88 |
|
| 89 |
subtest 'the request-derived codes substitute' => sub { |
| 90 |
is( rendered('%h'), '10.0.0.9', |
| 91 |
'%h is the host that made the request' ); |
| 92 |
is( rendered('%i'), $REQUEST->id, |
| 93 |
'%i is the request id' ); |
| 94 |
|
| 95 |
# With no request attached, both fall back rather than dying. |
| 96 |
is( rendered( '%h', no_request => 1 ), '-', |
| 97 |
'%h is "-" when there is no request' ); |
| 98 |
is( rendered( '%i', no_request => 1 ), '-', |
| 99 |
'%i is "-" when there is no request' ); |
| 100 |
}; |
| 101 |
|
| 102 |
subtest 'the date codes substitute in their documented formats' => sub { |
| 103 |
# The exact instant is not asserted - only the shape, which is what |
| 104 |
# distinguishes the four codes from each other. |
| 105 |
like( rendered('%t'), qr{^\d{2}/[A-Z][a-z]{2}/\d{4} \d{2}:\d{2}:\d{2}$}, |
| 106 |
'%t is dd/Mon/yyyy hh:mm:ss' ); |
| 107 |
like( rendered('%T'), qr{^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$}, |
| 108 |
'%T is yyyy-mm-dd hh:mm:ss' ); |
| 109 |
like( rendered('%u'), qr{^\d{2}/[A-Z][a-z]{2}/\d{4} \d{2}:\d{2}:\d{2}$}, |
| 110 |
'%u is dd/Mon/yyyy hh:mm:ss, in UTC' ); |
| 111 |
like( rendered('%U'), qr{^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$}, |
| 112 |
'%U is yyyy-mm-dd hh:mm:ss, in UTC' ); |
| 113 |
}; |
| 114 |
|
| 115 |
subtest 'the caller codes substitute from the stack' => sub { |
| 116 |
# caller_stack_size decides which frame %p, %f and %l report. It defaults |
| 117 |
# to 9, which is where the frame sits when the call came through a route; |
| 118 |
# calling format_message directly here means that frame does not exist, so |
| 119 |
# the codes fall back to '-'. |
| 120 |
is( rendered('%p|%f|%l'), '-|-|-', |
| 121 |
'the codes are "-" when the configured frame is off the top of the stack' ); |
| 122 |
|
| 123 |
# Pointed at a frame that does exist, they substitute for real. This is |
| 124 |
# what shows the codes are wired up and not simply always '-'. |
| 125 |
my $line = rendered( '%p|%f|%l', caller_stack_size => 1 ); |
| 126 |
my ( $package, $file, $lineno ) = split /\|/, $line; |
| 127 |
|
| 128 |
is( $package, 'main', '%p is the calling package' ); |
| 129 |
like( $file, qr/format\.t$/, '%f is the calling file' ); |
| 130 |
like( $lineno, qr/^\d+$/, '%l is a line number' ); |
| 131 |
}; |
| 132 |
|
| 133 |
subtest 'a %{...}h block reads the named request header' => sub { |
| 134 |
is( rendered('%{X-Custom}h'), 'CUSTOM-HEADER-VALUE', |
| 135 |
'a custom header is read by name' ); |
| 136 |
is( rendered('%{User-Agent}h'), 'TestAgent/1.0', |
| 137 |
'and so is a standard one' ); |
| 138 |
|
| 139 |
# A header that is not present renders '-' rather than the empty string, so |
| 140 |
# the field stays visible in the log line. |
| 141 |
is( rendered('%{X-Missing}h'), '-', |
| 142 |
'an absent header renders as "-"' ); |
| 143 |
|
| 144 |
is( rendered( '%{X-Custom}h', no_request => 1 ), '-', |
| 145 |
'and so does any header when there is no request' ); |
| 146 |
}; |
| 147 |
|
| 148 |
subtest 'a %{...}t block is a strftime format' => sub { |
| 149 |
is( rendered('%{%Y}t'), POSIX::strftime( '%Y', localtime(time) ), |
| 150 |
'the block is passed to strftime' ); |
| 151 |
like( rendered('%{%Y-%m}t'), qr/^\d{4}-\d{2}$/, |
| 152 |
'a multi-part format works too' ); |
| 153 |
}; |
| 154 |
|
| 155 |
subtest 'an unrecognised code warns and renders "-"' => sub { |
| 156 |
# The log call must survive. A format string with a typo should degrade the |
| 157 |
# one field, not lose the message. |
| 158 |
my ( $line, $warnings ) = render('%Z'); |
| 159 |
|
| 160 |
is( $line, '-', 'the unknown code renders as "-"' ); |
| 161 |
is( scalar @$warnings, 1, 'and warns once' ); |
| 162 |
like( $warnings->[0], qr/%Z not supported/, |
| 163 |
'naming the code that was not recognised' ); |
| 164 |
|
| 165 |
# The rest of the format still renders, which is the part that matters: |
| 166 |
# the message is not lost. |
| 167 |
my ( $mixed, $mixed_warnings ) = render('%L|%Z|%m'); |
| 168 |
is( $mixed, 'warning|-|THE-MESSAGE', |
| 169 |
'the surrounding codes still substitute' ); |
| 170 |
is( scalar @$mixed_warnings, 1, 'with one warning for the bad code' ); |
| 171 |
}; |
| 172 |
|
| 173 |
subtest 'an unrecognised block type warns and renders "-"' => sub { |
| 174 |
my ( $line, $warnings ) = render('%{whatever}z'); |
| 175 |
|
| 176 |
is( $line, '-', 'the unknown block renders as "-"' ); |
| 177 |
is( scalar @$warnings, 1, 'and warns once' ); |
| 178 |
like( $warnings->[0], qr/\{whatever\}z not supported/, |
| 179 |
'naming the block and its type' ); |
| 180 |
}; |
| 181 |
|
| 182 |
subtest 'an unsupported format character behaves the same whether or not it was ever documented' => sub { |
| 183 |
|
| 184 |
# %D used to be listed in the log_format POD ("timer") despite |
| 185 |
# map_chars_to_subs never implementing it. Rather than invent a timer |
| 186 |
# with no state in the logger role to back it, the fix was to remove %D |
| 187 |
# from the docs, so it is now just another unsupported character. This |
| 188 |
# checks it is indistinguishable from %Z, which was never documented at |
| 189 |
# all: both warn once per logged message and render '-'. |
| 190 |
for my $char (qw(D Z)) { |
| 191 |
my ( $line, $warnings ) = render("%$char"); |
| 192 |
|
| 193 |
is( $line, '-', "%$char renders as \"-\"" ); |
| 194 |
is( scalar @$warnings, 1, "%$char warns once per logged message" ); |
| 195 |
like( $warnings->[0], qr/%\Q$char\E not supported/, |
| 196 |
"the warning names %$char" ); |
| 197 |
} |
| 198 |
|
| 199 |
# The comparison that makes the point: a once-documented code and one that |
| 200 |
# was never documented are treated identically. |
| 201 |
my ( $d_line ) = render('%D'); |
| 202 |
my ( $z_line ) = render('%Z'); |
| 203 |
is( $d_line, $z_line, '%D behaves exactly like %Z' ); |
| 204 |
}; |
| 205 |
|
| 206 |
subtest 'every documented format character renders without warning' => sub { |
| 207 |
# The flip side of the subtest above: every character actually listed in |
| 208 |
# the log_format POD must resolve through map_chars_to_subs (or the block |
| 209 |
# handler), never fall through to the unsupported-code path. If this list |
| 210 |
# and the POD list ever drift apart, this is where it shows up. |
| 211 |
for my $char (qw(a h t T u U P L m p f l i)) { |
| 212 |
my ( undef, $warnings ) = render("%$char"); |
| 213 |
is( scalar @$warnings, 0, "%$char does not warn" ); |
| 214 |
} |
| 215 |
|
| 216 |
# The two block forms take an argument, so they are exercised separately |
| 217 |
# from the single-character codes above. |
| 218 |
for my $block ( '%{X-Custom}h', '%{%Y}t' ) { |
| 219 |
my ( undef, $warnings ) = render($block); |
| 220 |
is( scalar @$warnings, 0, "$block does not warn" ); |
| 221 |
} |
| 222 |
}; |
| 223 |
|
| 224 |
subtest 'the default log_format renders every one of its parts' => sub { |
| 225 |
# '[%a:%P] %L @%T> %m in %f l. %l' - the default. Rendered with a stack |
| 226 |
# size that resolves, so no part is '-' for reasons unrelated to the format. |
| 227 |
my $logger = Dancer2::Logger::Capture->new( |
| 228 |
app_name => 'MyApp', |
| 229 |
caller_stack_size => 1, |
| 230 |
config => {}, |
| 231 |
); |
| 232 |
$logger->set_request($REQUEST); |
| 233 |
|
| 234 |
my $line = $logger->format_message( 'warning', 'THE-MESSAGE' ); |
| 235 |
chomp $line; |
| 236 |
|
| 237 |
like( |
| 238 |
$line, |
| 239 |
qr{^ |
| 240 |
\[MyApp:$$\]\ # [%a:%P] |
| 241 |
warning\ @ # %L @ |
| 242 |
\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2}>\ # %T> |
| 243 |
THE-MESSAGE\ in\ # %m in |
| 244 |
\S+format\.t\ l\.\ \d+ # %f l. %l |
| 245 |
$}x, |
| 246 |
'the default format renders as documented, with no unsubstituted codes', |
| 247 |
); |
| 248 |
|
| 249 |
unlike( $line, qr/%[a-zA-Z]/, 'no format code is left unsubstituted' ); |
| 250 |
unlike( $line, qr/(?<![\w.])-(?![\w-])/, 'and no field fell back to "-"' ); |
| 251 |
}; |
| 252 |
|
| 253 |
done_testing(); |