Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions lib/Net/Daemon/Log.pm
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ sub OpenLog($) {
if ($@) {
die "Cannot open Syslog: $@";
}
if ( $^O ne 'solaris'
&& $^O ne 'freebsd'
&& eval { Sys::Syslog::_PATH_LOG() } ) {
Sys::Syslog::setlogsock('unix');
}
Sys::Syslog::openlog(
$self->{'logname'} || ref($self), 'pid',
$self->{'facility'} || 'daemon'
Expand Down
94 changes: 93 additions & 1 deletion t/logfile.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";
use Test::More tests => 7;
use Test::More tests => 17;
use File::Temp qw(tempdir tempfile);
use File::Spec;

Expand Down Expand Up @@ -78,3 +78,95 @@ use_ok('Net::Daemon::Log');

like($content, qr/hello from handle test/, 'log message was written via IO handle');
}

# Test 4: Debug() only logs when debug flag is true
{
my $dir = tempdir(CLEANUP => 1);
my $logpath = File::Spec->catfile($dir, 'debug.log');

my $logger = TestLogger->new(logfile => $logpath, debug => 0);
$logger->Debug('should not appear');

ok(!-e $logpath || -z $logpath, 'Debug() suppressed when debug is false');

my $logger2 = TestLogger->new(logfile => $logpath, debug => 1);
$logger2->Debug('debug message visible');

open my $fh, '<', $logpath or die "Cannot read $logpath: $!";
my $content = do { local $/; <$fh> };
close $fh;

like($content, qr/debug message visible/, 'Debug() logs when debug flag is true');
}

# Test 5: Error() logs at err level
{
my $dir = tempdir(CLEANUP => 1);
my $logpath = File::Spec->catfile($dir, 'error.log');

my $logger = TestLogger->new(logfile => $logpath);
$logger->Error('something went wrong');

open my $fh, '<', $logpath or die "Cannot read $logpath: $!";
my $content = do { local $/; <$fh> };
close $fh;

like($content, qr/err.*something went wrong/, 'Error() logs at err level');
}

# Test 6: Fatal() logs and dies
{
my $dir = tempdir(CLEANUP => 1);
my $logpath = File::Spec->catfile($dir, 'fatal.log');

my $logger = TestLogger->new(logfile => $logpath);
my $died = !eval { $logger->Fatal('fatal error %s', 'happened'); 1 };

ok($died, 'Fatal() throws an exception');
like($@, qr/fatal error happened/, 'Fatal() exception contains the formatted message');

open my $fh, '<', $logpath or die "Cannot read $logpath: $!";
my $content = do { local $/; <$fh> };
close $fh;

like($content, qr/fatal error happened/, 'Fatal() logs the message before dying');
}

# Test 7: Log() with format arguments (sprintf-style)
{
my $dir = tempdir(CLEANUP => 1);
my $logpath = File::Spec->catfile($dir, 'format.log');

my $logger = TestLogger->new(logfile => $logpath);
$logger->Log('notice', 'count=%d name=%s', 42, 'test');

open my $fh, '<', $logpath or die "Cannot read $logpath: $!";
my $content = do { local $/; <$fh> };
close $fh;

like($content, qr/count=42 name=test/, 'Log() applies sprintf formatting to arguments');
}

# Test 8: LogTime() returns a timestamp string
{
my $logger = TestLogger->new(logfile => 1);
my $time = $logger->LogTime();
ok(defined $time && length($time) > 0, 'LogTime() returns a non-empty string');
like($time, qr/\w+ \w+\s+\d+/, 'LogTime() looks like a localtime string');
}

# Test 9: Log() as class method writes to STDERR
{
my ($tmp_fh, $tmp_file) = tempfile(CLEANUP => 1);
close $tmp_fh;
open my $save_stderr, '>&', \*STDERR or die "Cannot dup STDERR: $!";
open STDERR, '>', $tmp_file or die "Cannot redirect STDERR: $!";
Net::Daemon::Log->Log('notice', 'class method call');
open STDERR, '>&', $save_stderr or die "Cannot restore STDERR: $!";
close $save_stderr;

open my $rfh, '<', $tmp_file or die "Cannot read $tmp_file: $!";
my $content = do { local $/; <$rfh> };
close $rfh;
like($content, qr/class method call/, 'Log() as class method writes to STDERR');
}
Loading