logging/backend.go

44 lines
676 B
Go
Raw Normal View History

2013-06-21 18:35:47 +02:00
package logging
import (
"io"
"os"
)
type Backend interface {
Write(*Record) error
SetLevel(Level)
SetFormatter(*Formatter)
}
type StdoutBackend struct {
l io.Writer
level Level
formatter *Formatter
}
func NewStdoutBackend() (b *StdoutBackend) {
b = &StdoutBackend{
l: os.Stdout,
formatter: &defaultFormatter,
}
return
}
func (b StdoutBackend) Write(r *Record) error {
if r.Level >= b.level {
//check back for auto dereferencing
text := (*b.formatter)(r)
b.l.Write([]byte(text))
}
return nil
}
func (b *StdoutBackend) SetLevel(l Level) {
b.level = l
}
func (b *StdoutBackend) SetFormatter(f *Formatter) {
b.formatter = f
}