Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
made format method in Mojo::Log a little more versatile
  • Loading branch information
kraih committed Jun 3, 2013
1 parent fc55b36 commit db4af0f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lib/Mojo/Log.pm
Expand Up @@ -35,7 +35,8 @@ sub fatal { shift->log(fatal => @_) }

sub format {
my ($self, $level, @lines) = @_;
return '[' . localtime(time) . "] [$level] " . join("\n", @lines) . "\n";
return encode 'UTF-8',
'[' . localtime(time) . "] [$level] " . join("\n", @lines, '');
}

sub info { shift->log(info => @_) }
Expand All @@ -57,13 +58,12 @@ sub log { shift->emit('message', lc(shift), @_) }
sub warn { shift->log(warn => @_) }

sub _message {
my ($self, $level, @lines) = @_;
my ($self, $level) = (shift, shift);

return unless $self->is_level($level) && (my $handle = $self->handle);

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

Expand Down Expand Up @@ -171,32 +171,36 @@ default logger.
=head2 debug
$log = $log->debug('You screwed up, but that is ok');
$log = $log->debug('You screwed up, but that is ok.');
$log = $log->debug('All', 'cool!');
Log debug message.
=head2 error
$log = $log->error('You really screwed up this time');
$log = $log->error('You really screwed up this time.');
$log = $log->error('Wow', 'seriously!');
Log error message.
=head2 fatal
$log = $log->fatal('Its over...');
$log = $log->fatal('Bye', 'bye!');
Log fatal message.
=head2 format
my $msg = $log->format('debug', 'Hi there!');
my $msg = $log->format('debug', 'Hi', 'there!');
my $msg = $log->format(debug => 'Hi there!');
my $msg = $log->format(debug => 'Hi', 'there!');
Format log message.
=head2 info
$log = $log->info('You are bad, but you prolly know already');
$log = $log->info('You are bad, but you prolly know already.');
$log = $log->info('Ok', 'then!');
Log info message.
Expand Down Expand Up @@ -238,13 +242,15 @@ Check for warn log level.
=head2 log
$log = $log->log(debug => 'This should work');
$log = $log->log(debug => 'This should work.');
$log = $log->log(debug => 'This', 'too!');
Emit C<message> event.
=head2 warn
$log = $log->warn('Dont do that Dave...');
$log = $log->warn('No', 'really!');
Log warn message.
Expand Down
2 changes: 2 additions & 0 deletions t/mojo/log.t
Expand Up @@ -41,6 +41,8 @@ like $log->format(debug => 'Test 123.'), qr/^\[.*\] \[debug\] Test 123\.\n$/,
'right format';
like $log->format(qw(debug Test 1 2 3)),
qr/^\[.*\] \[debug\] Test\n1\n2\n3\n$/, 'right format';
like decode('UTF-8', $log->format(error => 'I ♥ Mojolicious.')),
qr/^\[.*\] \[error\] I ♥ Mojolicious\.\n$/, 'right format';

# Events
my $msgs = [];
Expand Down

0 comments on commit db4af0f

Please sign in to comment.