diff --git a/lib/Log/Mini.pm b/lib/Log/Mini.pm index 088f3a3..7b70d49 100644 --- a/lib/Log/Mini.pm +++ b/lib/Log/Mini.pm @@ -3,7 +3,7 @@ package Log::Mini; use strict; use warnings; -our $VERSION = "0.0.3"; +our $VERSION = "0.1.0"; use Log::Mini::LoggerFILE; use Log::Mini::LoggerSTDERR; diff --git a/lib/Log/Mini/LoggerBase.pm b/lib/Log/Mini/LoggerBase.pm index d1b3e3e..f533810 100644 --- a/lib/Log/Mini/LoggerBase.pm +++ b/lib/Log/Mini/LoggerBase.pm @@ -7,6 +7,7 @@ use Carp qw(croak); use List::Util qw(first); use Time::Moment; + my $LEVELS = { error => 1, warn => 2, @@ -46,6 +47,7 @@ sub level { return $self->{level} || 'error'; } +sub log { return shift->_log( shift, @_) } sub info { return shift->_log( 'info', @_ ) } sub error { return shift->_log( 'error', @_ ) } sub warn { return shift->_log( 'warn', @_ ) } @@ -54,13 +56,15 @@ sub trace { return shift->_log( 'trace', @_ ) } sub _log { my $self = shift; - my ( $level, $message ) = @_; + my $level = shift; + my $message = shift; return if $LEVELS->{$level} > $LEVELS->{ $self->{'level'} }; my $time = Time::Moment->now->strftime('%Y-%m-%d %T%3f'); my $text = sprintf("%s [%s] %s\n", $time, $level, $message); + $text = sprintf($text, @_) if (@_); $self->_print($text); diff --git a/lib/Log/Mini/LoggerFILE.pm b/lib/Log/Mini/LoggerFILE.pm index da0a822..087b096 100644 --- a/lib/Log/Mini/LoggerFILE.pm +++ b/lib/Log/Mini/LoggerFILE.pm @@ -4,6 +4,7 @@ use strict; use warnings; use IO::Handle; + use base 'Log::Mini::LoggerBase'; sub new { @@ -15,7 +16,7 @@ sub new { open my $fh, '>>', $params{file} or die $!; if (defined $params{'synced'}) { - $fh->autoflush; + $fh->autoflush(1); } $self->{fh} = $fh; @@ -25,10 +26,9 @@ sub new { sub _print { my $self = shift; - my ($message) = @_; my $fh = $self->{fh}; - print $fh $message; + print $fh @_; } diff --git a/lib/Log/Mini/LoggerSTDERR.pm b/lib/Log/Mini/LoggerSTDERR.pm index 7eed120..a05384e 100644 --- a/lib/Log/Mini/LoggerSTDERR.pm +++ b/lib/Log/Mini/LoggerSTDERR.pm @@ -6,10 +6,7 @@ use warnings; use base 'Log::Mini::LoggerBase'; sub _print { - my $self = shift; - my ($message) = @_; - - print STDERR $message; + print STDERR $_[1]; } 1; diff --git a/t/03_LoggerFILE.t b/t/03_LoggerFILE.t index f2ac69b..292daf7 100644 --- a/t/03_LoggerFILE.t +++ b/t/03_LoggerFILE.t @@ -7,6 +7,7 @@ use Test::Fatal; use File::Temp; use Log::Mini::LoggerFILE; + subtest 'creates correct object' => sub { isa_ok(Log::Mini::LoggerFILE->new, 'Log::Mini::LoggerFILE'); }; @@ -55,6 +56,22 @@ subtest 'prints to stderr with \n' => sub { } }; +subtest 'prints sprintf formatted line' => sub { + for my $level (qw/error warn debug/) { + my $file = File::Temp->new; + my $log = _build_logger(file => $file); + + $log->$level('message %s', 'formatted'); + + undef $log; + + my $content = _slurp($file); + + like $content, + qr/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d.\d{3} \[$level\] message formatted$/; + } +}; + sub _slurp { my $file = shift; my $content = do { local $/; open my $fh, '<', $file->filename or die $!; <$fh> }; diff --git a/t/04_LoggerSTDERR.t b/t/04_LoggerSTDERR.t index d40b714..48b46df 100644 --- a/t/04_LoggerSTDERR.t +++ b/t/04_LoggerSTDERR.t @@ -35,6 +35,20 @@ subtest 'prints to stderr with \n' => sub { } }; +subtest 'prints sprintf formatted line' => sub { + my $output = []; + my $log = _build_logger(output => $output); + + for my $level (qw/error warn debug/) { + my $stderr = capture_stderr { + $log->$level('message %s', 'formatted'); + + }; + like $stderr, + qr/^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d.\d{3} \[$level\] message formatted$/; + } +}; + sub _build_logger { my $logger = Log::Mini::LoggerSTDERR->new; $logger->set_level('debug');