allow formatted by sprintf output.
This commit is contained in:
parent
9cc7d5dc50
commit
1391ee3503
6 changed files with 41 additions and 9 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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 @_;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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> };
|
||||
|
|
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue