2013-06-21 18:35:47 +02:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-18 13:03:24 +02:00
|
|
|
"log"
|
2013-06-21 18:35:47 +02:00
|
|
|
"os"
|
2016-04-28 14:17:23 +02:00
|
|
|
"sync"
|
2013-06-21 18:35:47 +02:00
|
|
|
)
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Logger is a facility that writes logs to one or more backands (files,
|
|
|
|
// stdout/stderr, syslog, etc.) which can be configured independently
|
|
|
|
//
|
|
|
|
// Loggers are concurrent-safe.
|
2013-06-21 18:35:47 +02:00
|
|
|
type Logger struct {
|
2016-04-28 14:17:23 +02:00
|
|
|
sync.Mutex
|
|
|
|
|
2014-08-18 13:03:24 +02:00
|
|
|
name string
|
2013-06-21 18:35:47 +02:00
|
|
|
backends []Backend
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// NewLogger initializes a new Logger with no backend and with the default log level.
|
2014-08-18 13:03:24 +02:00
|
|
|
func NewLogger(name string) (l *Logger) {
|
2013-06-21 18:35:47 +02:00
|
|
|
l = &Logger{
|
2020-02-01 17:30:09 +01:00
|
|
|
name: name,
|
2013-06-21 18:35:47 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// AddBackend add a new Backend to the logger. All set backends are kept.
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) AddBackend(b Backend) {
|
2020-05-17 14:34:56 +02:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
2013-06-21 18:35:47 +02:00
|
|
|
l.backends = append(l.backends, b)
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// SetBackend sets the backend list to the logger. Any existing backend will be lost.
|
2014-08-18 13:03:24 +02:00
|
|
|
func (l *Logger) SetBackend(b ...Backend) {
|
2020-05-17 14:34:56 +02:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
2014-08-18 13:03:24 +02:00
|
|
|
l.backends = b
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// SetLevel changes the log level of all regisered backends to the given level.
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) SetLevel(level Level) {
|
|
|
|
for _, backend := range l.backends {
|
|
|
|
backend.SetLevel(level)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 13:03:24 +02:00
|
|
|
type buffer struct {
|
|
|
|
level Level
|
|
|
|
logger *Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *buffer) Write(p []byte) (n int, err error) {
|
|
|
|
b.logger.Log(b.level, string(p))
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// AsStdLog encapsulate the logger in an instance of lof.Logger from the
|
|
|
|
// standard library and returns it.
|
|
|
|
//
|
|
|
|
// It is there for interoperability reasons.
|
2014-08-18 13:03:24 +02:00
|
|
|
func (l *Logger) AsStdLog(level Level) *log.Logger {
|
|
|
|
stdLogger := log.New(&buffer{logger: l, level: level}, "", 0)
|
|
|
|
|
|
|
|
return stdLogger
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Log sends a record containing the message `m` to the registered backends
|
|
|
|
// whose level is at least `level`
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Log(level Level, m string) {
|
2016-04-28 14:17:23 +02:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
2014-08-18 13:03:24 +02:00
|
|
|
r := NewRecord(l.name, level, m)
|
2013-06-21 18:35:47 +02:00
|
|
|
for _, backend := range l.backends {
|
2013-06-24 17:35:43 +02:00
|
|
|
if r.Level >= backend.Level() {
|
2020-02-01 17:30:09 +01:00
|
|
|
_ = backend.Write(r)
|
2013-06-24 17:35:43 +02:00
|
|
|
}
|
2013-06-21 18:35:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Debug logs a message with the Debug level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Debug(text string) {
|
2020-02-01 17:30:09 +01:00
|
|
|
l.Log(Debug, text)
|
2013-06-21 18:35:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Debugf formats the message with given args and logs the result with the
|
|
|
|
// Debug level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Debugf(text string, args ...interface{}) {
|
|
|
|
l.Debug(fmt.Sprintf(text, args...))
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Info logs a message with the Info level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Info(text string) {
|
2020-02-01 17:30:09 +01:00
|
|
|
l.Log(Info, text)
|
2013-06-21 18:35:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Infof formats the message with given args and logs the result with the
|
|
|
|
// Info level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Infof(text string, args ...interface{}) {
|
|
|
|
l.Info(fmt.Sprintf(text, args...))
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Warning logs a message with the Warning level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Warning(text string) {
|
2020-02-01 17:30:09 +01:00
|
|
|
l.Log(Warning, text)
|
2013-06-21 18:35:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Warningf formats the message with given args and logs the result with the
|
|
|
|
// Warning level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Warningf(text string, args ...interface{}) {
|
|
|
|
l.Warning(fmt.Sprintf(text, args...))
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Error logs a message with the Error level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Error(text string) {
|
2020-02-01 17:30:09 +01:00
|
|
|
l.Log(Error, text)
|
2013-06-21 18:35:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Errorf formats the message with given args and logs the result with the
|
|
|
|
// Error level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Errorf(text string, args ...interface{}) {
|
|
|
|
l.Error(fmt.Sprintf(text, args...))
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Critical logs a message with the Critical level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Critical(text string) {
|
2020-02-01 17:30:09 +01:00
|
|
|
l.Log(Critical, text)
|
2013-06-21 18:35:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Criticalf formats the message with given args and logs the result with the
|
|
|
|
// Critical level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Criticalf(text string, args ...interface{}) {
|
|
|
|
l.Critical(fmt.Sprintf(text, args...))
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Fatal logs a message with the Fatal level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Fatal(text string) {
|
2020-02-01 17:30:09 +01:00
|
|
|
l.Log(Debug, text)
|
2013-06-21 18:35:47 +02:00
|
|
|
os.Exit(100)
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:30:09 +01:00
|
|
|
// Fatalf formats the message with given args and logs the result with the
|
|
|
|
// Fatal level
|
2013-06-21 18:35:47 +02:00
|
|
|
func (l *Logger) Fatalf(text string, args ...interface{}) {
|
|
|
|
l.Fatal(fmt.Sprintf(text, args...))
|
|
|
|
}
|