40 lines
620 B
Go
40 lines
620 B
Go
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 {
|
|
text := (*b.formatter)(r)
|
|
_, err := io.WriteString(b.l, text)
|
|
return err
|
|
}
|
|
|
|
func (b *StdoutBackend) SetLevel(l Level) {
|
|
b.level = l
|
|
}
|
|
|
|
func (b *StdoutBackend) SetFormatter(f *Formatter) {
|
|
b.formatter = f
|
|
}
|