2020-01-09 02:45:07 +01:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2020-01-14 00:47:35 +01:00
|
|
|
"fmt"
|
2020-01-09 02:45:07 +01:00
|
|
|
"net/http"
|
2020-01-14 00:47:35 +01:00
|
|
|
"runtime"
|
2020-01-09 02:45:07 +01:00
|
|
|
"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)
|
2020-01-10 19:11:24 +01:00
|
|
|
LevelTrace = Level(logrus.TraceLevel)
|
2020-01-09 02:45:07 +01:00
|
|
|
)
|
|
|
|
|
2020-05-13 22:49:55 +02:00
|
|
|
type contextKey string
|
|
|
|
|
|
|
|
const loggerCtxKey = contextKey("logger")
|
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
var (
|
|
|
|
currentLevel Level
|
|
|
|
defaultLogger = logrus.New()
|
2020-01-31 14:35:33 +01:00
|
|
|
logSourceLine = false
|
2020-01-09 02:45:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetLevel sets the global log level used by the simple logger.
|
|
|
|
func SetLevel(l Level) {
|
|
|
|
currentLevel = l
|
|
|
|
logrus.SetLevel(logrus.Level(l))
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:35:33 +01:00
|
|
|
func SetLevelString(l string) {
|
2020-01-14 00:47:35 +01:00
|
|
|
envLevel := strings.ToLower(l)
|
|
|
|
var level Level
|
|
|
|
switch envLevel {
|
|
|
|
case "critical":
|
|
|
|
level = LevelCritical
|
|
|
|
case "error":
|
|
|
|
level = LevelError
|
|
|
|
case "warn":
|
|
|
|
level = LevelWarn
|
|
|
|
case "debug":
|
|
|
|
level = LevelDebug
|
|
|
|
case "trace":
|
|
|
|
level = LevelTrace
|
|
|
|
default:
|
|
|
|
level = LevelInfo
|
|
|
|
}
|
|
|
|
SetLevel(level)
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:35:33 +01:00
|
|
|
func SetLogSourceLine(enabled bool) {
|
|
|
|
logSourceLine = enabled
|
|
|
|
}
|
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
func NewContext(ctx context.Context, keyValuePairs ...interface{}) context.Context {
|
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := addFields(createNewLogger(), keyValuePairs)
|
2020-05-13 22:49:55 +02:00
|
|
|
ctx = context.WithValue(ctx, loggerCtxKey, logger)
|
2020-01-09 02:45:07 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-01-10 19:11:24 +01:00
|
|
|
func Trace(args ...interface{}) {
|
|
|
|
if currentLevel < LevelTrace {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logger, msg := parseArgs(args)
|
|
|
|
logger.Trace(msg)
|
|
|
|
}
|
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
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)
|
|
|
|
}
|
2020-01-31 14:35:33 +01:00
|
|
|
if logSourceLine {
|
2020-01-14 00:47:35 +01:00
|
|
|
_, file, line, ok := runtime.Caller(2)
|
|
|
|
if !ok {
|
|
|
|
file = "???"
|
|
|
|
line = 0
|
|
|
|
}
|
|
|
|
//_, filename := path.Split(file)
|
|
|
|
//l = l.WithField("filename", filename).WithField("line", line)
|
|
|
|
l = l.WithField(" source", fmt.Sprintf("file://%s:%d", file, line))
|
|
|
|
}
|
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
switch msg := args[0].(type) {
|
|
|
|
case error:
|
|
|
|
return l, msg.Error()
|
|
|
|
case string:
|
|
|
|
return l, msg
|
|
|
|
}
|
2020-01-14 00:47:35 +01:00
|
|
|
|
2020-01-09 02:45:07 +01:00
|
|
|
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:
|
2020-11-22 22:55:23 +01:00
|
|
|
if i+1 >= len(keyValuePairs) {
|
|
|
|
logger = logger.WithField(name, "!!!!Invalid number of arguments in log call!!!!")
|
|
|
|
} else {
|
|
|
|
value := keyValuePairs[i+1]
|
|
|
|
logger = logger.WithField(name, value)
|
|
|
|
}
|
2020-01-09 02:45:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractLogger(ctx interface{}) (*logrus.Entry, error) {
|
|
|
|
switch ctx := ctx.(type) {
|
|
|
|
case *logrus.Entry:
|
|
|
|
return ctx, nil
|
|
|
|
case context.Context:
|
2020-05-13 22:49:55 +02:00
|
|
|
logger := ctx.Value(loggerCtxKey)
|
2020-01-09 02:45:07 +01:00
|
|
|
if logger != nil {
|
|
|
|
return logger.(*logrus.Entry), nil
|
|
|
|
}
|
2020-04-27 01:33:57 +02:00
|
|
|
return extractLogger(NewContext(ctx))
|
2020-01-09 02:45:07 +01:00
|
|
|
case *http.Request:
|
|
|
|
return extractLogger(ctx.Context())
|
|
|
|
}
|
|
|
|
return nil, errors.New("no logger found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func createNewLogger() *logrus.Entry {
|
2020-01-14 00:47:35 +01:00
|
|
|
//logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true, DisableTimestamp: false, FullTimestamp: true})
|
2020-01-09 02:45:07 +01:00
|
|
|
//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
|
|
|
|
}
|