Skip to content

Commit

Permalink
added testing attribute to Mojo::Log
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 25, 2013
1 parent bc4ac25 commit 072a3d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lib/Mojo/Log.pm
Expand Up @@ -3,6 +3,7 @@ use Mojo::Base 'Mojo::EventEmitter';

use Carp 'croak';
use Fcntl ':flock';
use Test::More ();

has handle => sub {

Expand All @@ -19,6 +20,7 @@ has handle => sub {
};
has level => 'debug';
has 'path';
has testing => sub { $ENV{HARNESS_ACTIVE} };

# Supported log level
my $LEVEL = {debug => 1, info => 2, warn => 3, error => 4, fatal => 5};
Expand Down Expand Up @@ -62,8 +64,9 @@ sub _message {
return unless $self->is_level($level) && (my $handle = $self->handle);

flock $handle, LOCK_EX;
croak "Can't write to log: $!"
unless defined $handle->syswrite($self->format($level, @lines));
my $msg = $self->format($level, @lines);
if ($self->testing) { Test::More::diag $msg }
else { defined $handle->syswrite($msg) or croak "Can't write to log: $!" }
flock $handle, LOCK_UN;
}

Expand Down Expand Up @@ -157,6 +160,14 @@ These levels are currently available:
Log file path used by C<handle>.
=head2 testing
my $testing = $log->testing;
$log = $log->testing(0);
Output log messages as diagnostics information through L<Test::More>, defaults
to the value of the HARNESS_ACTIVE environment variable.
=head1 METHODS
L<Mojo::Log> inherits all methods from L<Mojo::EventEmitter> and implements
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/log.t
Expand Up @@ -9,7 +9,7 @@ use Mojo::Util 'slurp';
# Logging to file
my $dir = tempdir CLEANUP => 1;
my $path = catdir $dir, 'test.log';
my $log = Mojo::Log->new(level => 'error', path => $path);
my $log = Mojo::Log->new(level => 'error', path => $path, testing => 0);
$log->error('Just works.');
$log->fatal('Works too.');
$log->debug('Does not work.');
Expand Down

0 comments on commit 072a3d5

Please sign in to comment.