From bcbaf5e75d06d043f62bf3543089ad06174a1d05 Mon Sep 17 00:00:00 2001 From: Denis Fedoseev Date: Fri, 12 Apr 2019 01:01:37 +0300 Subject: [PATCH] null logger --- Changes | 1 + META.json | 2 +- README.md | 12 ++++++++---- lib/Log/Mini.pm | 22 ++++++++++++++-------- lib/Log/Mini/LoggerNULL.pm | 12 ++++++++++++ t/01_logger_builder.t | 6 ++++++ 6 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 lib/Log/Mini/LoggerNULL.pm diff --git a/Changes b/Changes index 0a53712..974952b 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for Perl extension Log-Mini {{$NEXT}} + - Null logger 0.1.1 2019-03-19T20:44:04Z - Error in the constructor was fixed. 0.1.0 removed from CPAN. diff --git a/META.json b/META.json index 4fb9d5c..6bbf0ca 100644 --- a/META.json +++ b/META.json @@ -68,7 +68,7 @@ "web" : "https://github.com/alpha6/Log-Mini" } }, - "version" : "0.1.1", + "version" : "0.2.0", "x_serialization_backend" : "JSON::PP version 2.93", "x_static_install" : 1 } diff --git a/README.md b/README.md index 1d4eae4..8e793bd 100644 --- a/README.md +++ b/README.md @@ -6,23 +6,27 @@ Log::Mini - It's a very simple logger which can log your messages to a file or S use Log::Mini; - my $logger = Log::Mini->get_logger(); #STDERR logger used by default. Error is default log level + my $logger = Log::Mini->new(); #STDERR logger used by default. Error is default log level $logger->error('Error message'); - my $debug_logger = Log::Mini->get_logger('stderr', level => 'debug'); #STDERR logger used by default + my $debug_logger = Log::Mini->new('stderr', level => 'debug'); #STDERR logger used by default $debug_logger->error('Error message'); - my $file_logger = Log::Mini->get_logger(file => 'log_file.log'); + my $file_logger = Log::Mini->new(file => 'log_file.log'); $file_logger->info('message to log file'); #prevent buffered output. May slow down your application! - my $synced_file_logger = Log::Mini->get_logger(file => 'log_file.log', synced => 1); + my $synced_file_logger = Log::Mini->new(file => 'log_file.log', synced => 1); #format message with sprintf $logger->info('Message with %s %s', 'some', 'additional info'); #log method for better compatibility $logger->log('info', 'information message'); + + #Null logger - drops all messages to /dev/null + my $logger = Log::Mini->new('null); + $logger->error('Error message'); #Message will be dropped # DESCRIPTION diff --git a/lib/Log/Mini.pm b/lib/Log/Mini.pm index e49afaa..3311874 100644 --- a/lib/Log/Mini.pm +++ b/lib/Log/Mini.pm @@ -3,12 +3,8 @@ package Log::Mini; use strict; use warnings; -our $VERSION = "0.1.1"; +our $VERSION = "0.2.0"; -use Log::Mini::LoggerFILE; -use Log::Mini::LoggerSTDERR; - -use Data::Dumper; use feature qw/say/; @@ -19,9 +15,15 @@ sub new { $type = 'stderr' unless defined $type; if ( $type eq 'file' ) { + require Log::Mini::LoggerFILE; return Log::Mini::LoggerFILE->new(@_); } + elsif ( $type eq 'null' ) { + require Log::Mini::LoggerNULL; + return Log::Mini::LoggerNULL->new(@_); + } else { + require Log::Mini::LoggerSTDERR; return Log::Mini::LoggerSTDERR->new(@args); } } @@ -42,13 +44,13 @@ Log::Mini - It's a very simple logger which can log your messages to a file or S use Log::Mini; - my $logger = Log::Mini->get_logger(); #STDERR logger used by default. Error is default log level + my $logger = Log::Mini->new(); #STDERR logger used by default. Error is default log level $logger->error('Error message'); - my $debug_logger = Log::Mini->get_logger('stderr', level => 'debug'); #STDERR logger used by default + my $debug_logger = Log::Mini->new('stderr', level => 'debug'); #STDERR logger used by default $debug_logger->error('Error message'); - my $file_logger = Log::Mini->get_logger(file => 'log_file.log'); + my $file_logger = Log::Mini->new(file => 'log_file.log'); $file_logger->info('message to log file'); #prevent buffered output. May slow down your application! @@ -60,6 +62,10 @@ Log::Mini - It's a very simple logger which can log your messages to a file or S #log method for better compatibility $logger->log('info', 'information message'); + #Null logger - drops all messages to /dev/null + my $logger = Log::Mini->new('null); + $logger->error('Error message'); #Message will be dropped + =head1 DESCRIPTION diff --git a/lib/Log/Mini/LoggerNULL.pm b/lib/Log/Mini/LoggerNULL.pm new file mode 100644 index 0000000..68f2209 --- /dev/null +++ b/lib/Log/Mini/LoggerNULL.pm @@ -0,0 +1,12 @@ +package Log::Mini::LoggerNULL; + +use strict; +use warnings; + +use base 'Log::Mini::LoggerBase'; + +sub _print { + return; +} + +1; diff --git a/t/01_logger_builder.t b/t/01_logger_builder.t index 3278cea..0cad439 100644 --- a/t/01_logger_builder.t +++ b/t/01_logger_builder.t @@ -13,6 +13,12 @@ subtest 'creates stderr logger' => sub { isa_ok $logger, 'Log::Mini::LoggerSTDERR'; }; +subtest 'creates null logger' => sub { + my $logger = Log::Mini->new('null'); + + isa_ok $logger, 'Log::Mini::LoggerNULL'; +}; + subtest 'creates file logger' => sub { my $file = File::Temp->new; my $logger = Log::Mini->new('file' => $file->filename);