in reply to Why can't some of my modules do log output to a file?
it should work, right?
In addition to the comments by the others, I would suggest using flock, turning on Data::Dumper's Useqq option, and adding more error checking.
use warnings; use strict; use Fcntl qw/:flock/; use Data::Dumper; sub write_log { my ($file, $obj) = @_; open my $fh, '>>', $file or die "open $file: $!"; flock($fh, LOCK_EX) or die "flock $file: $!"; print {$fh} Data::Dumper->new([$obj])->Useqq(1)->Dump or die "print $file: $!"; close $fh or die "close $file: $!"; } write_log("/tmp/log.txt", {hello=>"world"});
If that didn't work or die, personally I would assume the code really wasn't being run, or perhaps that some other process was messing with the logfile. (Note it's been a while since I've used flock on Windows, but I assume you're on *NIX based on your filename.)
There's definitely nothing wrong with the logfile
Well now that you've said that, I have to ask ;-) Are you sure? SELinux? Are all the processes spawned by the script running as the same user?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Why can't some of my modules do log output to a file?
by LanX (Saint) on Oct 10, 2022 at 13:29 UTC | |
by haukex (Archbishop) on Oct 10, 2022 at 14:00 UTC | |
by Marshall (Canon) on Oct 10, 2022 at 21:57 UTC | |
by haukex (Archbishop) on Oct 11, 2022 at 06:20 UTC | |
Re^2: Why can't some of my modules do log output to a file?
by eyepopslikeamosquito (Archbishop) on Oct 11, 2022 at 10:23 UTC | |
Re^2: Why can't some of my modules do log output to a file?
by LittleJack (Beadle) on Oct 12, 2022 at 01:06 UTC | |
by haukex (Archbishop) on Oct 12, 2022 at 05:40 UTC | |
by AnomalousMonk (Archbishop) on Oct 12, 2022 at 04:13 UTC |