mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 04:27:37 +03:00
Replaced Beego logging
This commit is contained in:
parent
6eda38a951
commit
84d69a4f41
28 changed files with 559 additions and 282 deletions
167
log/log.go
Normal file
167
log/log.go
Normal file
|
@ -0,0 +1,167 @@
|
|||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Level uint8
|
||||
|
||||
type LevelFunc = func(ctx interface{}, msg interface{}, keyValuePairs ...interface{})
|
||||
|
||||
const (
|
||||
LevelCritical = Level(logrus.FatalLevel)
|
||||
LevelError = Level(logrus.ErrorLevel)
|
||||
LevelWarn = Level(logrus.WarnLevel)
|
||||
LevelInfo = Level(logrus.InfoLevel)
|
||||
LevelDebug = Level(logrus.DebugLevel)
|
||||
)
|
||||
|
||||
var (
|
||||
currentLevel Level
|
||||
defaultLogger = logrus.New()
|
||||
)
|
||||
|
||||
// SetLevel sets the global log level used by the simple logger.
|
||||
func SetLevel(l Level) {
|
||||
currentLevel = l
|
||||
logrus.SetLevel(logrus.Level(l))
|
||||
}
|
||||
|
||||
func NewContext(ctx context.Context, keyValuePairs ...interface{}) context.Context {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
logger := addFields(createNewLogger(), keyValuePairs)
|
||||
ctx = context.WithValue(ctx, "logger", logger)
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
func SetDefaultLogger(l *logrus.Logger) {
|
||||
defaultLogger = l
|
||||
}
|
||||
|
||||
func CurrentLevel() Level {
|
||||
return currentLevel
|
||||
}
|
||||
|
||||
func Error(args ...interface{}) {
|
||||
if currentLevel < LevelError {
|
||||
return
|
||||
}
|
||||
logger, msg := parseArgs(args)
|
||||
logger.Error(msg)
|
||||
}
|
||||
|
||||
func Warn(args ...interface{}) {
|
||||
if currentLevel < LevelWarn {
|
||||
return
|
||||
}
|
||||
logger, msg := parseArgs(args)
|
||||
logger.Warn(msg)
|
||||
}
|
||||
|
||||
func Info(args ...interface{}) {
|
||||
if currentLevel < LevelInfo {
|
||||
return
|
||||
}
|
||||
logger, msg := parseArgs(args)
|
||||
logger.Info(msg)
|
||||
}
|
||||
|
||||
func Debug(args ...interface{}) {
|
||||
if currentLevel < LevelDebug {
|
||||
return
|
||||
}
|
||||
logger, msg := parseArgs(args)
|
||||
logger.Debug(msg)
|
||||
}
|
||||
|
||||
func parseArgs(args []interface{}) (*logrus.Entry, string) {
|
||||
var l *logrus.Entry
|
||||
var err error
|
||||
if args[0] == nil {
|
||||
l = createNewLogger()
|
||||
args = args[1:]
|
||||
} else {
|
||||
l, err = extractLogger(args[0])
|
||||
if err != nil {
|
||||
l = createNewLogger()
|
||||
} else {
|
||||
args = args[1:]
|
||||
}
|
||||
}
|
||||
if len(args) > 1 {
|
||||
kvPairs := args[1:]
|
||||
l = addFields(l, kvPairs)
|
||||
}
|
||||
switch msg := args[0].(type) {
|
||||
case error:
|
||||
return l, msg.Error()
|
||||
case string:
|
||||
return l, msg
|
||||
}
|
||||
return l, ""
|
||||
}
|
||||
|
||||
func addFields(logger *logrus.Entry, keyValuePairs []interface{}) *logrus.Entry {
|
||||
for i := 0; i < len(keyValuePairs); i += 2 {
|
||||
switch name := keyValuePairs[i].(type) {
|
||||
case error:
|
||||
logger = logger.WithField("error", name.Error())
|
||||
case string:
|
||||
value := keyValuePairs[i+1]
|
||||
logger = logger.WithField(name, value)
|
||||
}
|
||||
}
|
||||
return logger
|
||||
}
|
||||
|
||||
func extractLogger(ctx interface{}) (*logrus.Entry, error) {
|
||||
switch ctx := ctx.(type) {
|
||||
case *logrus.Entry:
|
||||
return ctx, nil
|
||||
case context.Context:
|
||||
logger := ctx.Value("logger")
|
||||
if logger != nil {
|
||||
return logger.(*logrus.Entry), nil
|
||||
}
|
||||
case *http.Request:
|
||||
return extractLogger(ctx.Context())
|
||||
}
|
||||
return nil, errors.New("no logger found")
|
||||
}
|
||||
|
||||
func createNewLogger() *logrus.Entry {
|
||||
//l.Formatter = &logrus.TextFormatter{ForceColors: true, DisableTimestamp: false, FullTimestamp: true}
|
||||
defaultLogger.Level = logrus.Level(currentLevel)
|
||||
logger := logrus.NewEntry(defaultLogger)
|
||||
logger.Level = logrus.Level(currentLevel)
|
||||
return logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
//logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true, DisableTimestamp: false, FullTimestamp: true})
|
||||
envLevel := strings.ToLower(os.Getenv("LOG_LEVEL"))
|
||||
var level Level
|
||||
switch envLevel {
|
||||
case "critical":
|
||||
level = LevelCritical
|
||||
case "error":
|
||||
level = LevelError
|
||||
case "warn":
|
||||
level = LevelWarn
|
||||
case "debug":
|
||||
level = LevelDebug
|
||||
default:
|
||||
level = LevelInfo
|
||||
}
|
||||
SetLevel(level)
|
||||
}
|
104
log/log_test.go
Normal file
104
log/log_test.go
Normal file
|
@ -0,0 +1,104 @@
|
|||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/sirupsen/logrus/hooks/test"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestLog(t *testing.T) {
|
||||
|
||||
Convey("Test Logger", t, func() {
|
||||
l, hook := test.NewNullLogger()
|
||||
SetLevel(LevelInfo)
|
||||
SetDefaultLogger(l)
|
||||
|
||||
Convey("Plain message", func() {
|
||||
Error("Simple Message")
|
||||
So(hook.LastEntry().Message, ShouldEqual, "Simple Message")
|
||||
So(hook.LastEntry().Data, ShouldBeEmpty)
|
||||
})
|
||||
|
||||
Convey("Passing nil as context", func() {
|
||||
Error(nil, "Simple Message")
|
||||
So(hook.LastEntry().Message, ShouldEqual, "Simple Message")
|
||||
So(hook.LastEntry().Data, ShouldBeEmpty)
|
||||
})
|
||||
|
||||
Convey("Message with two kv pairs", func() {
|
||||
Error("Simple Message", "key1", "value1", "key2", "value2")
|
||||
So(hook.LastEntry().Message, ShouldEqual, "Simple Message")
|
||||
So(hook.LastEntry().Data["key1"], ShouldEqual, "value1")
|
||||
So(hook.LastEntry().Data["key2"], ShouldEqual, "value2")
|
||||
So(hook.LastEntry().Data, ShouldHaveLength, 2)
|
||||
})
|
||||
|
||||
Convey("Only error", func() {
|
||||
Error(errors.New("error test"))
|
||||
So(hook.LastEntry().Message, ShouldEqual, "error test")
|
||||
So(hook.LastEntry().Data, ShouldBeEmpty)
|
||||
})
|
||||
|
||||
Convey("Error as last argument", func() {
|
||||
Error("Error scrobbling track", "id", 1, errors.New("some issue"))
|
||||
So(hook.LastEntry().Message, ShouldEqual, "Error scrobbling track")
|
||||
So(hook.LastEntry().Data["id"], ShouldEqual, 1)
|
||||
So(hook.LastEntry().Data["error"], ShouldEqual, "some issue")
|
||||
So(hook.LastEntry().Data, ShouldHaveLength, 2)
|
||||
})
|
||||
|
||||
Convey("Passing a request", func() {
|
||||
ctx := NewContext(nil, "foo", "bar")
|
||||
req := httptest.NewRequest("get", "/", nil).WithContext(ctx)
|
||||
|
||||
Error(req, "Simple Message", "key1", "value1")
|
||||
So(hook.LastEntry().Message, ShouldEqual, "Simple Message")
|
||||
So(hook.LastEntry().Data["foo"], ShouldEqual, "bar")
|
||||
So(hook.LastEntry().Data["key1"], ShouldEqual, "value1")
|
||||
So(hook.LastEntry().Data, ShouldHaveLength, 2)
|
||||
})
|
||||
|
||||
Convey("Skip if level is lower", func() {
|
||||
SetLevel(LevelError)
|
||||
Info("Simple Message")
|
||||
So(hook.LastEntry(), ShouldBeNil)
|
||||
})
|
||||
})
|
||||
|
||||
Convey("Test extractLogger", t, func() {
|
||||
Convey("It returns an error if the context is nil", func() {
|
||||
_, err := extractLogger(nil)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("It returns an error if the context is a string", func() {
|
||||
_, err := extractLogger("any msg")
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("It returns the logger from context if it has one", func() {
|
||||
logger := logrus.NewEntry(logrus.New())
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "logger", logger)
|
||||
|
||||
l, err := extractLogger(ctx)
|
||||
So(err, ShouldBeNil)
|
||||
So(l, ShouldEqual, logger)
|
||||
})
|
||||
|
||||
Convey("It returns the logger from request's context if it has one", func() {
|
||||
logger := logrus.NewEntry(logrus.New())
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "logger", logger)
|
||||
req := httptest.NewRequest("get", "/", nil).WithContext(ctx)
|
||||
l, err := extractLogger(req)
|
||||
So(err, ShouldBeNil)
|
||||
So(l, ShouldEqual, logger)
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue