diff options
-rw-r--r-- | lib/ManaTools/Shared/Logging.pm | 68 | ||||
-rw-r--r-- | t/11-Shared-Logging.t | 22 |
2 files changed, 90 insertions, 0 deletions
diff --git a/lib/ManaTools/Shared/Logging.pm b/lib/ManaTools/Shared/Logging.pm index 71be268a..e2f31eb3 100644 --- a/lib/ManaTools/Shared/Logging.pm +++ b/lib/ManaTools/Shared/Logging.pm @@ -93,6 +93,74 @@ has 'loc' => ( #============================================================= +=head2 new + +=head3 INPUT + + hash ref containing + ident: optional string used as identifier into syslog + +=head3 DESCRIPTION + + new is inherited from Moose, to create a Logging object + +=cut + +#============================================================= +has 'ident' => ( + is => 'ro', + isa => 'Str', + default => '' +); + +#============================================================= + +=head2 BUILD + +=head3 INPUT + + $self: this object + +=head3 DESCRIPTION + + The BUILD method is called after a Moose object is created, + Into this method additional data are initialized. + This method just calls openlog if "ident" is set. + +=cut + +#============================================================= +sub BUILD { + my $self = shift; + + Sys::Syslog::openlog($self->ident) if $self->ident; +} + +#============================================================= + +=head2 DEMOLISH + +=head3 INPUT + + $val: boolean value indicating whether or not this method + was called as part of the global destruction process + (when the Perl interpreter exits) + +=head3 DESCRIPTION + + Moose provides a hook for object destruction with the + DEMOLISH method as it does for construtor with BUILD + +=cut + +#============================================================= +sub DEMOLISH { + my ($self, $val) = @_; + + Sys::Syslog::closelog(); +} +#============================================================= + =head2 R =head3 INPUT diff --git a/t/11-Shared-Logging.t b/t/11-Shared-Logging.t index 5b18ae9c..db27d5b2 100644 --- a/t/11-Shared-Logging.t +++ b/t/11-Shared-Logging.t @@ -6,6 +6,7 @@ use Data::Dumper; BEGIN { use_ok( 'ManaTools::Shared::Logging' ) || print "ManaTools::Shared::Logging failed!\n"; + use_ok( 'ManaTools::Shared::JournalCtl' ) || print "JournalCtl failed!\n"; } ok ( my $obj = ManaTools::Shared::Logging->new(), 'new_logging'); @@ -16,4 +17,25 @@ ok ( $obj->W("test warning %d", 2), 'warning_logging'); 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'); + +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 +sleep 1; +my $c = $o->getLog(); +my $str = $c->[-1]; +ok($str =~ "test err 8", 'test err found'); +$str = $c->[-2]; +ok($str =~ "test warning 7", 'test warning found'); +$str = $c->[-3]; +ok($str =~ "test info 6", 'test info found'); +$str = $c->[-4]; +ok($str =~ "test debug 5", 'test debug found'); + done_testing; |