Close file descriptor in FileBackend before reopening it

This commit is contained in:
Bruno Carlin 2020-02-01 10:42:42 +01:00
parent 5c9b362285
commit 3bc019fd4e
2 changed files with 14 additions and 5 deletions

View file

@ -7,3 +7,5 @@
INFO -> Info, etc. INFO -> Info, etc.
- Uncompatible NoopBackend.Level() now returns DefaultLevel instead of Fatal - Uncompatible NoopBackend.Level() now returns DefaultLevel instead of Fatal
- Fix: creates new logger with level DefaultLevel instead of Debug - Fix: creates new logger with level DefaultLevel instead of Debug
- Fix: FileBackend now properly closes the file befor reopening it (fixes a
potential file descriptor leak)

View file

@ -89,12 +89,19 @@ func (b *FileBackend) Reopen() error {
return nil return nil
} }
filename_fd, err := os.OpenFile(b.filepath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) if c, ok := b.l.(io.Closer); ok {
if err != nil { if err := c.Close(); err != nil {
return errors.New(fmt.Sprintf("Cannot open log file %s (%s)", b.filepath, err.Error())) return err
} else { }
b.l = filename_fd
} }
fd, err := os.OpenFile(b.filepath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("Cannot open log file %s: %w", b.filepath, err)
}
b.l = fd
return nil return nil
} }