I run my go application as http server service. I use follow lines to assign the output log file.
os.OpenFile("/var/log/my.log, os.O_WRONLY | os.O_CREATE| os.O_APPEND, 0644)
defer f.Close()
log.SetOutput(f)
I also enabled Linux log rotate service. The issue is, there is no new log file created after Linux rotates the log file into tar.
You can see that using _lsof | grep deleted_. Even if the file was removed, it's still open in a process. The process doesn't know anything about logrotate and fd is still valid.
This case should be detected inside io.Writer, which passed to the Logger.SetOutput, as Logger doesn't know anything about that and can't find it out.
An implementation could check inode number and dev id before each write to detect a file rotation. Also AFAIK, logrotate can send SIGHUP to an application to notify about the rotation. So you can handle this signal and reopen the file.
yep, @noxiouz nailed it. Not a Logrus issue.
Another simple way: put copytruncate in your lograte config. This option will copy current logfile content to a rotated one and truncate the original.
@luizdepra this saved me a lot of wasted time, thank you!
@sirupsen @noxiouz , I know this is old and closed, but I ran in to this issue in a little meta-file monitoring app I'm developing that uses Logrus for it's logging. (Awesome package!)
I wasn't satisfied with the copytrucate work-around, nor checking before every write or relying on SIGHUP.
My solution is to add an fsnotify watcher running in a go routine. If it detects a rotation, it sends the filepath over a channel to a func that re-spawns the file.
This is still rough at the moment, but I feel a good solution and behaving as I expect. If you're interested you can check it out here: https://github.com/IkiM0no/logmon/blob/master/monitor.go
Thanks for your continued work on Logrus!
..
@luizdepra The disadvantage of using copytruncate is that logs can be lost with this method, especially when your log files are large.
Most helpful comment
Another simple way: put
copytruncatein your lograte config. This option will copy current logfile content to a rotated one and truncate the original.