

use with linux logrotate - inode not updating when the log is rotated · Issue #7...
source link: https://github.com/uber-go/zap/issues/797
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Hello,
I tried to use zap with the linux logrotate
, but when stdout.log
is rotated to stdout.log.1
and a new stdout.log
is created, zap still writes to the old stdout.log.1
.
Is there any way to force zap to close the old file and write to the new file (like refreshing the inode of the log file)?
I'm aware that I can use copytruncate
to solve this problem, but filebeat does not work well with copytruncate. So it would be best if I could tell zap to write to the new log file.
Thanks
Zap does not have an inbuilt way to close a file and open a new file, but it can be done relatively easily by building a customer WriteSyncer
, and it can be done outside of zap. Here's a sample of how it would look (if it was specific to files, the same design could be used to build a WriteSyncer
that dynamically allows changing the underlying WriteSyncer
that's written to).
type ReopenableWriteSyncer struct { file string cur atomic.Value // *os.File } func NewReopenableWriteSyncer(file string) (*ReopenableWriteSyncer, error) {..} func (ws *ReopenableWriteSyncer) getFile() *os.File { return ws.cur.Load().(*os.File) } func (ws *ReopenableWriteSyncer) Reload() error { f, err := os.Open(ws.file) if err != nil { return err } ws.cur.Store(f) } # wrap all the WriteSyncer methods to use getFile # example with Sync func (ws *ReopenableWriteSyncer) Sync() error { return ws.getFile().Sync() }
The caller would need to write up the WriteSyncer
, Core
and Logger
, and then when they want to reopen the file (e.g., on SIGHUP), they could call the Reload
method.
Given that it can be built outside of zap, and we haven't seen many requests for it yet, I'm not sure if we should add it to the zap library directly right now.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK