Skip to content

Commit

Permalink
Log with localtime instead of gmtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Henning Thorsen committed Feb 19, 2016
1 parent 230e0ed commit 938556a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 44 deletions.
10 changes: 8 additions & 2 deletions lib/Mojo/Date.pm
Expand Up @@ -2,7 +2,6 @@ package Mojo::Date;
use Mojo::Base -base;
use overload bool => sub {1}, '""' => sub { shift->to_string }, fallback => 1;

use Mojo::Util 'epoch_to_datetime';
use Time::Local 'timegm';

has epoch => sub {time};
Expand Down Expand Up @@ -53,7 +52,14 @@ sub parse {
(defined $epoch && ($epoch += $offset) >= 0) ? $epoch : undef);
}

sub to_datetime { epoch_to_datetime(shift->epoch) }
sub to_datetime {

# RFC 3339 (1994-11-06T08:49:37Z)
my ($s, $m, $h, $day, $month, $year) = gmtime(my $epoch = shift->epoch);
my $str = sprintf '%04d-%02d-%02dT%02d:%02d:%02d', $year + 1900, $month + 1,
$day, $h, $m, $s;
return $str . ($epoch =~ /(\.\d+)$/ ? "$1Z" : 'Z');
}

sub to_string {

Expand Down
6 changes: 4 additions & 2 deletions lib/Mojo/Log.pm
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use Fcntl ':flock';
use Mojo::Util qw(epoch_to_datetime deprecated encode monkey_patch);
use Mojo::Util qw(deprecated encode monkey_patch);

has format => sub { \&_format };
has handle => sub {
Expand Down Expand Up @@ -56,7 +56,9 @@ sub new {
sub warn { shift->_log(warn => @_) }

sub _format {
'[' . epoch_to_datetime(shift) . '] [' . shift() . '] ' . join "\n", @_, '';
my ($s, $m, $h, $day, $month, $year) = localtime(shift);
sprintf '[%04d-%02d-%02dT%02d:%02d:%02d] [%s] %s', $year + 1900, $month + 1,
$day, $h, $m, $s, shift(@_), join "\n", @_, '';
}

sub _log { shift->emit('message', shift, @_) }
Expand Down
28 changes: 5 additions & 23 deletions lib/Mojo/Util.pm
Expand Up @@ -53,12 +53,11 @@ my %CACHE;

our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode deprecated dumper encode epoch_to_datetime hmac_sha1_sum),
qw(html_unescape md5_bytes md5_sum monkey_patch punycode_decode),
qw(punycode_encode quote secure_compare sha1_bytes sha1_sum slurp),
qw(split_cookie_header split_header spurt squish steady_time tablify),
qw(term_escape trim unindent unquote url_escape url_unescape xml_escape),
qw(xor_encode)
qw(decode deprecated dumper encode hmac_sha1_sum html_unescape md5_bytes),
qw(md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_cookie_header),
qw(split_header spurt squish steady_time tablify term_escape trim unindent),
qw(unquote url_escape url_unescape xml_escape xor_encode)
);

# DEPRECATED in Clinking Beer Mugs!
Expand Down Expand Up @@ -114,12 +113,6 @@ sub dumper {

sub encode { _encoding($_[0])->encode("$_[1]") }

sub epoch_to_datetime {
my ($s, $m, $h, $day, $month, $year) = gmtime(my $epoch = shift);
return sprintf '%04d-%02d-%02dT%02d:%02d:%02d%s', $year + 1900, $month + 1,
$day, $h, $m, $s, ($epoch && $epoch =~ /(\.\d+)$/ ? "$1Z" : 'Z');
}

sub hmac_sha1_sum { hmac_sha1_hex @_ }

sub html_unescape {
Expand Down Expand Up @@ -583,17 +576,6 @@ Dump a Perl data structure with L<Data::Dumper>.
Encode characters to bytes.
=head2 epoch_to_datetime
my $str = epoch_to_datetime($epoch);
my $str = epoch_to_datetime;
Converts an epoch time to a RFC 3339 datetime string. The epoch timestamp
defaults to "now".
# "1994-11-06T08:49:37Z".
epoch_to_datetime 784111777;
=head2 hmac_sha1_sum
my $checksum = hmac_sha1_sum $bytes, 'passw0rd';
Expand Down
11 changes: 8 additions & 3 deletions t/mojo/log.t
Expand Up @@ -15,9 +15,14 @@ $log->fatal('I ♥ Mojolicious');
$log->debug('Does not work');
undef $log;
my $content = decode 'UTF-8', slurp($path);
like $content, qr/\[.*\] \[error\] Just works/, 'right error message';
like $content, qr/\[.*\] \[fatal\] I ♥ Mojolicious/, 'right fatal message';
unlike $content, qr/\[.*\] \[debug\] Does not work/, 'no debug message';
like $content, qr/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\] \[error\] Just works/,
'right error message';
like $content,
qr/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\] \[fatal\] I ♥ Mojolicious/,
'right fatal message';
unlike $content,
qr/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\] \[debug\] Does not work/,
'no debug message';

# Logging to STDERR
my $buffer = '';
Expand Down
19 changes: 5 additions & 14 deletions t/mojo/util.t
Expand Up @@ -12,12 +12,11 @@ use Mojo::DeprecationTest;

use Mojo::Util
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode deprecated dumper encode epoch_to_datetime hmac_sha1_sum),
qw(html_unescape md5_bytes md5_sum monkey_patch punycode_decode),
qw(punycode_encode quote secure_compare sha1_bytes sha1_sum slurp),
qw(split_cookie_header split_header spurt squish steady_time tablify),
qw(term_escape trim unindent unquote url_escape url_unescape xml_escape),
qw(xor_encode);
qw(decode dumper encode hmac_sha1_sum html_unescape md5_bytes md5_sum),
qw(monkey_patch punycode_decode punycode_encode quote secure_compare),
qw(secure_compare sha1_bytes sha1_sum slurp split_cookie_header),
qw(split_header spurt squish steady_time tablify term_escape trim unindent),
qw(unquote url_escape url_unescape xml_escape xor_encode);

# camelize
is camelize('foo_bar_baz'), 'FooBarBaz', 'right camelized result';
Expand Down Expand Up @@ -165,14 +164,6 @@ is decode('does_not_exist', ''), undef, 'decoding with invalid encoding worked';
eval { encode('does_not_exist', '') };
like $@, qr/Unknown encoding 'does_not_exist'/, 'right error';

# epoch_to_datetime
is epoch_to_datetime(1408567500), '2014-08-20T20:45:00Z',
'epoch_to_datetime 1408567500';
like epoch_to_datetime(Time::HiRes::time),
qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z$/, 'epoch_to_datetime hires';
like epoch_to_datetime(), qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/,
'epoch_to_datetime now';

# url_escape
is url_escape('business;23'), 'business%3B23', 'right URL escaped result';

Expand Down

0 comments on commit 938556a

Please sign in to comment.