backends can be reopened

This commit is contained in:
Bruno Carlin 2014-12-04 20:25:49 +01:00
parent 42ebeb58f4
commit e6b3497b2b

View file

@ -14,6 +14,7 @@ type Backend interface {
SetFormatter(*Formatter) SetFormatter(*Formatter)
SetLevel(Level) SetLevel(Level)
Level() Level Level() Level
Reopen() error
} }
// //
@ -24,6 +25,7 @@ type FileBackend struct {
l io.Writer l io.Writer
formatter *Formatter formatter *Formatter
level Level level Level
filepath string
} }
// Creates a new backend to write the logs on the standard output // Creates a new backend to write the logs on the standard output
@ -53,6 +55,7 @@ func NewFileBackend(filename string) (b *FileBackend, e error) {
b = &FileBackend{ b = &FileBackend{
l: filename_fd, l: filename_fd,
formatter: &defaultFormatter, formatter: &defaultFormatter,
filepath: filename,
} }
return return
} }
@ -83,6 +86,20 @@ func (b *FileBackend) SetFormatter(f *Formatter) {
b.formatter = f b.formatter = f
} }
func (b *FileBackend) Reopen() error {
if b.filepath == "" {
return nil
}
filename_fd, err := os.OpenFile(b.filepath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return errors.New(fmt.Sprintf("Cannot open log file %s (%s)", b.filepath, err.Error()))
} else {
b.l = filename_fd
}
return nil
}
// //
// Syslog Backend // Syslog Backend
// //
@ -140,6 +157,10 @@ func (sb *SyslogBackend) Level() Level {
return sb.level return sb.level
} }
func (sb *SyslogBackend) Reopen() error {
return nil
}
var facilities = map[string]syslog.Priority{ var facilities = map[string]syslog.Priority{
"kern": syslog.LOG_KERN, "kern": syslog.LOG_KERN,
"user": syslog.LOG_USER, "user": syslog.LOG_USER,
@ -192,3 +213,7 @@ func (nb *NoopBackend) SetLevel(level Level) {}
func (nb *NoopBackend) Level() Level { func (nb *NoopBackend) Level() Level {
return FATAL return FATAL
} }
func (nb *NoopBackend) Reopen() error {
return nil
}