Subject: | Logging to a file is always buffered |
Date: | Wed, 23 May 2018 09:21:03 +0300 |
To: | bug-Log-Tiny [...] rt.cpan.org |
From: | purification [...] ukr.net |
This is Log::Tiny 1.0 on perl 5.26.2.
Testcase:
In terminal #1:
% touch myapp.log
% tail -f myapp.log
In terminal #2:
% perl -E 'use Log::Tiny; my $LT = Log::Tiny->new("./myapp.log"); $LT->INFO("first"); sleep 5; $LT->INFO("second"); sleep 5; $LT->INFO("third")'
Expected result: logfile can be tail'ed to see messages in real time.
Actual result: logfile stays empty until program finishes.
Reason of bug:
Tiny.pm lines 201-205:
{
my $autoflush = $|++;
$ret = print {$self->{logfh}} $tmp;
$| = $autoflush;
}
$| only affects currently selected output (i.e. usually STDOUT) and will not have
any effect on a filehandle opened into a file.
To fix this, $logfh->autoflush(1) can be used in sub new after open() call.