add a Close() method to Backend
This commit is contained in:
parent
c6819aa61d
commit
79da88fda7
4 changed files with 45 additions and 8 deletions
29
backend.go
29
backend.go
|
@ -14,6 +14,7 @@ type Backend interface {
|
|||
SetLevel(Level)
|
||||
Level() Level
|
||||
Reopen() error
|
||||
Close() error
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -95,6 +96,22 @@ func (b *FileBackend) SetFormatter(f *Formatter) {
|
|||
// Reopen closes and reopens the file it writes to. It should be used after log
|
||||
// rotation
|
||||
func (b *FileBackend) Reopen() error {
|
||||
if err := b.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fd, err := os.OpenFile(b.filepath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) //nolint: gosec
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot open log file %s: %w", b.filepath, err)
|
||||
}
|
||||
|
||||
b.l = fd
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the underlying file used by the backend
|
||||
func (b *FileBackend) Close() error {
|
||||
if b.filepath == "" {
|
||||
return nil
|
||||
}
|
||||
|
@ -105,13 +122,6 @@ func (b *FileBackend) Reopen() error {
|
|||
}
|
||||
}
|
||||
|
||||
fd, err := os.OpenFile(b.filepath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) //nolint: gosec
|
||||
if err != nil {
|
||||
return fmt.Errorf("Cannot open log file %s: %w", b.filepath, err)
|
||||
}
|
||||
|
||||
b.l = fd
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -147,3 +157,8 @@ func (nb *NoopBackend) Level() Level {
|
|||
func (nb *NoopBackend) Reopen() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close is a noop
|
||||
func (nb *NoopBackend) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -78,6 +78,11 @@ func (sb *SyslogBackend) Reopen() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Close closes the connection to the syslog daemon
|
||||
func (sb *SyslogBackend) Close() error {
|
||||
return sb.w.Close()
|
||||
}
|
||||
|
||||
var facilities = map[string]syslog.Priority{
|
||||
"kern": syslog.LOG_KERN,
|
||||
"user": syslog.LOG_USER,
|
||||
|
|
|
@ -32,7 +32,6 @@ func (l *Logger) AddBackend(b Backend) {
|
|||
}
|
||||
|
||||
// SetBackend sets the backend list to the logger. Any existing backend will be lost.
|
||||
// FIXME must close file backends to avoid fd leaks
|
||||
func (l *Logger) SetBackend(b ...Backend) {
|
||||
l.backends = b
|
||||
}
|
||||
|
|
18
logging.go
18
logging.go
|
@ -1,3 +1,21 @@
|
|||
// Package logging provides a multi-backend leveled logging facility.
|
||||
/*
|
||||
|
||||
Backends
|
||||
|
||||
package logging defines the following builtin backends:
|
||||
* NoopBackend, which discards all messages
|
||||
* FileBackend, which redirects logs to an io.Writer object. It has constructors
|
||||
for files, stdout and stderr
|
||||
* SyslogBackend, only available in unix-like systems, writes log entryies to a
|
||||
syslog daemon.
|
||||
|
||||
A backend can safely be used by multiple loggers.
|
||||
|
||||
It is the caller's responsability to call Close on backends when they are not
|
||||
used anymore to free their resources.
|
||||
|
||||
*/
|
||||
package logging
|
||||
|
||||
import (
|
||||
|
|
Loading…
Reference in a new issue