logging/backend.go

150 lines
3.1 KiB
Go
Raw Normal View History

2013-06-21 18:35:47 +02:00
package logging
import (
2013-06-24 17:37:46 +02:00
"fmt"
2013-06-21 18:35:47 +02:00
"io"
"os"
)
2020-02-01 17:30:09 +01:00
// Backend is the interface that specifies the methods that a backend must
// implement
2013-06-21 18:35:47 +02:00
type Backend interface {
Write(*Record) error
SetFormatter(*Formatter)
2013-06-24 17:37:46 +02:00
SetLevel(Level)
Level() Level
2014-12-04 20:25:49 +01:00
Reopen() error
2013-06-21 18:35:47 +02:00
}
2013-06-24 17:37:46 +02:00
//
// Backend to write in file-like objects
//
2020-02-01 17:30:09 +01:00
// FileBackend is a backend that writes to a file.
2013-06-24 17:37:46 +02:00
type FileBackend struct {
2013-06-21 18:35:47 +02:00
l io.Writer
formatter *Formatter
2013-06-24 17:37:46 +02:00
level Level
2014-12-04 20:25:49 +01:00
filepath string
2013-06-21 18:35:47 +02:00
}
2020-02-01 17:30:09 +01:00
// NewStdoutBackend creates a new backend to write the logs on the standard
// output
2013-06-24 17:37:46 +02:00
func NewStdoutBackend() (b *FileBackend) {
b = &FileBackend{
2013-06-21 18:35:47 +02:00
l: os.Stdout,
formatter: &defaultFormatter,
}
return
}
2020-02-01 17:30:09 +01:00
// NewStderrBackend creates a new backend to write the logs on the error output
2013-06-24 17:37:46 +02:00
func NewStderrBackend() (b *FileBackend) {
b = &FileBackend{
l: os.Stderr,
formatter: &defaultFormatter,
}
return
}
2020-02-01 17:30:09 +01:00
// NewFileBackend creates a new backend to write the logs in a given file
func NewFileBackend(filename string) (*FileBackend, error) {
fd, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) //nolint: gosec
2013-06-24 17:37:46 +02:00
if err != nil {
2020-02-01 17:30:09 +01:00
return nil, fmt.Errorf("Cannot open log file %s: %w", filename, err)
2013-06-24 17:37:46 +02:00
}
2020-02-01 17:30:09 +01:00
b := &FileBackend{
l: fd,
2013-06-24 17:37:46 +02:00
formatter: &defaultFormatter,
2014-12-04 20:25:49 +01:00
filepath: filename,
2013-06-24 17:37:46 +02:00
}
2020-02-01 17:30:09 +01:00
return b, nil
2013-06-24 17:37:46 +02:00
}
2020-02-01 17:30:09 +01:00
// NewIoBackend creates a new backend to write the logs in a given io.Writer
func NewIoBackend(buf io.Writer) (b *FileBackend) {
return &FileBackend{
l: buf,
formatter: &defaultFormatter,
}
}
2013-06-24 17:37:46 +02:00
func (b FileBackend) Write(r *Record) error {
text := (*b.formatter)(r)
_, err := io.WriteString(b.l, text)
return err
2013-06-21 18:35:47 +02:00
}
2020-02-01 17:30:09 +01:00
// SetLevel changes the log level of the backend
2013-06-24 17:37:46 +02:00
func (b *FileBackend) SetLevel(l Level) {
2013-06-21 18:35:47 +02:00
b.level = l
}
2020-02-01 17:30:09 +01:00
// Level returns the log level set for this backend
2013-06-24 17:37:46 +02:00
func (b *FileBackend) Level() Level {
return b.level
}
2020-02-01 17:30:09 +01:00
// SetFormatter defines the formatter for this backend
2013-06-24 17:37:46 +02:00
func (b *FileBackend) SetFormatter(f *Formatter) {
2013-06-21 18:35:47 +02:00
b.formatter = f
}
2020-02-01 17:30:09 +01:00
// Reopen closes and reopens the file it writes to. It should be used after log
// rotation
2014-12-04 20:25:49 +01:00
func (b *FileBackend) Reopen() error {
if b.filepath == "" {
return nil
}
if c, ok := b.l.(io.Closer); ok {
if err := c.Close(); err != nil {
return err
}
}
2020-02-01 17:30:09 +01:00
fd, err := os.OpenFile(b.filepath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) //nolint: gosec
2014-12-04 20:25:49 +01:00
if err != nil {
return fmt.Errorf("Cannot open log file %s: %w", b.filepath, err)
2014-12-04 20:25:49 +01:00
}
b.l = fd
2014-12-04 20:25:49 +01:00
return nil
}
2014-10-22 12:35:14 +02:00
//
// Noop Backend
//
2020-02-01 17:30:09 +01:00
// NoopBackend does nothing and discards all log entries without writing them anywhere
2014-10-22 12:35:14 +02:00
type NoopBackend struct{}
2020-02-01 17:30:09 +01:00
// NewNoopBackend creates a noop backend
2014-10-22 12:35:14 +02:00
func NewNoopBackend() (Backend, error) {
return &NoopBackend{}, nil
}
2020-02-01 17:30:09 +01:00
// Write is a noop
2014-10-22 12:35:14 +02:00
func (nb *NoopBackend) Write(r *Record) error {
return nil
}
2020-02-01 17:30:09 +01:00
// SetFormatter is a noop
2014-10-22 12:35:14 +02:00
func (nb *NoopBackend) SetFormatter(f *Formatter) {}
2020-02-01 17:30:09 +01:00
// SetLevel is a noop
2014-10-22 12:35:14 +02:00
func (nb *NoopBackend) SetLevel(level Level) {}
2020-02-01 17:30:09 +01:00
// Level always returns DefeultLevel
2014-10-22 12:35:14 +02:00
func (nb *NoopBackend) Level() Level {
return DefaultLevel
2014-10-22 12:35:14 +02:00
}
2014-12-04 20:25:49 +01:00
2020-02-01 17:30:09 +01:00
// Reopen is a noop
2014-12-04 20:25:49 +01:00
func (nb *NoopBackend) Reopen() error {
return nil
}