Skip to content

Commit

Permalink
fixed encoding bug in Mojo::Log (closes #495)
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 24, 2013
1 parent 7718f7b commit de42dd6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,4 +1,7 @@

4.05 2013-05-24
- Fixed encoding bug in Mojo::Log.

4.04 2013-05-23
- Added WebSocket subprotocol support to Mojo::UserAgent::Transactor.

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Log.pm
Expand Up @@ -63,7 +63,7 @@ sub _message {

flock $handle, LOCK_EX;
croak "Can't write to log: $!"
unless defined $handle->syswrite($self->format($level, @lines));
unless $handle->print($self->format($level, @lines));
flock $handle, LOCK_UN;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -41,7 +41,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Top Hat';
our $VERSION = '4.04';
our $VERSION = '4.05';

sub AUTOLOAD {
my $self = shift;
Expand Down
27 changes: 22 additions & 5 deletions t/mojo/log.t
Expand Up @@ -4,21 +4,38 @@ use Test::More;
use File::Spec::Functions 'catdir';
use File::Temp 'tempdir';
use Mojo::Log;
use Mojo::Util 'slurp';
use Mojo::Util qw(decode slurp);

# Logging to file
my $dir = tempdir CLEANUP => 1;
my $path = catdir $dir, 'test.log';
my $log = Mojo::Log->new(level => 'error', path => $path);
$log->error('Just works.');
$log->fatal('Works too.');
$log->fatal('I ♥ Mojolicious.');
$log->debug('Does not work.');
undef $log;
my $content = slurp $path;
like $content, qr/\[.*\] \[error\] Just works\.\n/, 'has error message';
like $content, qr/\[.*\] \[fatal\] Works too\.\n/, 'has fatal message';
my $content = decode 'UTF-8', slurp($path);
like $content, qr/\[.*\] \[error\] Just works\.\n/, 'right error message';
like $content, qr/\[.*\] \[fatal\] I ♥ Mojolicious\.\n/,
'right fatal message';
unlike $content, qr/\[.*\] \[debug\] Does not work\.\n/, 'no debug message';

# Logging to STDERR
my $buffer = '';
{
open my $handle, '>', \$buffer;
local *STDERR = $handle;
my $log = Mojo::Log->new;
$log->error('Just works.');
$log->fatal('I ♥ Mojolicious.');
$log->debug('Works too.');
}
$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';

# Formatting
$log = Mojo::Log->new;
like $log->format(debug => 'Test 123.'), qr/^\[.*\] \[debug\] Test 123\.\n$/,
Expand Down

0 comments on commit de42dd6

Please sign in to comment.