diff options
-rw-r--r-- | lib/ManaTools/Shared/Logging.pm | 24 | ||||
-rw-r--r-- | t/11-Shared-Logging.t | 28 |
2 files changed, 45 insertions, 7 deletions
diff --git a/lib/ManaTools/Shared/Logging.pm b/lib/ManaTools/Shared/Logging.pm index e2f31eb3..46f102c8 100644 --- a/lib/ManaTools/Shared/Logging.pm +++ b/lib/ManaTools/Shared/Logging.pm @@ -159,6 +159,7 @@ sub DEMOLISH { Sys::Syslog::closelog(); } + #============================================================= =head2 R @@ -321,6 +322,29 @@ sub D { $self->S('debug', $s, @para); } +#============================================================= + +=head2 setmask + +=head3 INPUT + + $self: this object + $mask: new log mask + +=head3 DESCRIPTION + + Sets the log mask for the current process to $mask and returns the old mask. + See Sys::Syslog::setlogmask for details. + +=cut + +#============================================================= +sub setmask { + my ($self, $mask) = @_; + + Sys::Syslog::setlogmask($mask); +} + no Moose; __PACKAGE__->meta->make_immutable; diff --git a/t/11-Shared-Logging.t b/t/11-Shared-Logging.t index db27d5b2..3840eeea 100644 --- a/t/11-Shared-Logging.t +++ b/t/11-Shared-Logging.t @@ -1,6 +1,7 @@ use 5.006; use strict; use warnings FATAL => 'all'; +use Sys::Syslog qw(:macros); use Test::More; use Data::Dumper; @@ -18,24 +19,37 @@ ok ( $obj->E("test err %d", 3), 'err_logging'); ok ( $obj->D("test debug %d", 4), 'debug_logging'); $obj = undef; -my $o = ManaTools::Shared::JournalCtl->new(this_boot=>1,); -$o->identifier('test_logging'); +# let's test an identified logger ok ( $obj = ManaTools::Shared::Logging->new(ident => 'test_logging'), 'new_test_logging'); ok ( $obj->D("test debug %d", 5), 'debug_logging as test_logging'); ok ( $obj->I("test info %d", 6), 'info_logging as test_logging'); ok ( $obj->W("test warning %d", 7), 'warning_logging as test_logging'); ok ( $obj->E("test err %d", 8), 'err_logging as test_logging'); -#let's wait journalctl to be updated + +# disable debug level +ok ( $obj->setmask( LOG_UPTO(LOG_INFO) ), 'setmask up to info' ); +ok ( $obj->I("test info (debug disabled) %d", 9), 'info with debug disabled as test_logging'); +isnt ( $obj->D("test debug (debug disabled) %d", 10), 1, 'debug disabled as test_logging'); + +# let's check logged string int journalctl +my $o = ManaTools::Shared::JournalCtl->new(this_boot=>1,); +$o->identifier('test_logging'); +# let's wait journalctl to be updated sleep 1; my $c = $o->getLog(); my $str = $c->[-1]; -ok($str =~ "test err 8", 'test err found'); +isnt($str =~ /test debug \(debug disabled\) 10/, 1, 'debug disabled'); +ok($str =~ /test info \(debug disabled\) 9/, 'info with debug disabled'); + $str = $c->[-2]; -ok($str =~ "test warning 7", 'test warning found'); +ok($str =~ /test err 8/, 'test err found'); $str = $c->[-3]; -ok($str =~ "test info 6", 'test info found'); +ok($str =~ /test warning 7/, 'test warning found'); $str = $c->[-4]; -ok($str =~ "test debug 5", 'test debug found'); +ok($str =~ /test info 6/, 'test info found'); +$str = $c->[-5]; +ok($str =~ /test debug 5/, 'test debug found'); + done_testing; |