Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Log to print undef values #2091

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Mojo/Log.pm
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ sub _default {
my ($s, $m, $h, $day, $month, $year) = localtime $time;
$time = sprintf '%04d-%02d-%02d %02d:%02d:%08.5f', $year + 1900, $month + 1, $day, $h, $m,
"$s." . ((split /\./, $time)[1] // 0);
return "[$time] [$$] [$level] " . join(' ', @_) . "\n";
return "[$time] [$$] [$level] " . join(' ', map {defined $_ ? $_ : 'undef'} @_) . "\n";
}

sub _log {
Expand All @@ -123,7 +123,7 @@ sub _message {
sub _short {
my ($time, $level) = (shift, shift);
my ($magic, $short) = ("<$MAGIC{$level}>", substr($level, 0, 1));
return "${magic}[$$] [$short] " . join(' ', @_) . "\n";
return "${magic}[$$] [$short] " . join(' ', map {defined $_ ? $_ : 'undef'} @_) . "\n";
}

package Mojo::Log::_Capture;
Expand Down
3 changes: 3 additions & 0 deletions t/mojo/log.t
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ subtest 'Logging to STDERR' => sub {
$log->fatal('I ♥ Mojolicious');
$log->debug('Works too');
$log->debug(sub { return 'And this', 'too' });
$log->info(undef, 'works');
}
my $content = decode 'UTF-8', $buffer;
like $content, qr/\[.*\] \[error\] Just works\n/, 'right error message';
like $content, qr/\[.*\] \[fatal\] I ♥ Mojolicious\n/, 'right fatal message';
like $content, qr/\[.*\] \[debug\] Works too\n/, 'right debug message';
like $content, qr/\[.*\] \[debug\] And this too\n/, 'right debug message';
like $content, qr/\[.*\] \[info\] undef works\n/, 'right debug message';
};

subtest 'Formatting' => sub {
Expand Down Expand Up @@ -73,6 +75,7 @@ subtest 'Short log messages (systemd)' => sub {
like $log->format->(time, 'error', 'Test 123'), qr/^<3>\[\d+\] \[e\] Test 123\n$/, 'right format';
like $log->format->(time, 'fatal', 'Test 123'), qr/^<2>\[\d+\] \[f\] Test 123\n$/, 'right format';
like $log->format->(time, 'debug', 'Test', '1', '2', '3'), qr/^<6>\[\d+\] \[d\] Test 1 2 3\n$/, 'right format';
like $log->format->(time, 'debug', undef, 'works'), qr/^<6>\[\d+\] \[d\] undef works\n$/, 'right format';
};

subtest 'Colorized log messages' => sub {
Expand Down