2022-12-31 10:22:38 +01:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
2024-03-16 19:05:21 +01:00
|
|
|
"context"
|
2022-12-31 10:22:38 +01:00
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
2023-05-29 20:03:01 +02:00
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-12-31 10:22:38 +01:00
|
|
|
)
|
|
|
|
|
2024-03-16 19:05:21 +01:00
|
|
|
type Fields = logrus.Fields
|
|
|
|
|
|
|
|
type FieldLogger interface {
|
|
|
|
logrus.FieldLogger
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
|
|
|
var (
|
2024-03-16 19:05:21 +01:00
|
|
|
log logrus.FieldLogger
|
|
|
|
once sync.Once
|
2022-12-31 10:22:38 +01:00
|
|
|
)
|
|
|
|
|
2024-03-16 19:05:21 +01:00
|
|
|
// Init initialize global instance of logging library.
|
|
|
|
func Init(fields Fields) FieldLogger {
|
|
|
|
once.Do(func() {
|
|
|
|
logger := logrus.New()
|
|
|
|
|
|
|
|
logger.SetFormatter(&logrus.JSONFormatter{
|
|
|
|
FieldMap: logrus.FieldMap{
|
|
|
|
logrus.FieldKeyTime: "timestamp",
|
|
|
|
logrus.FieldKeyLevel: "level",
|
|
|
|
logrus.FieldKeyMsg: "message",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.SetLevel(logrus.InfoLevel)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2024-03-16 19:05:21 +01:00
|
|
|
log = logger.WithFields(fields)
|
2022-12-31 10:22:38 +01:00
|
|
|
})
|
2024-03-16 19:05:21 +01:00
|
|
|
return log
|
|
|
|
}
|
|
|
|
|
|
|
|
type ctxKey int
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2024-03-16 19:05:21 +01:00
|
|
|
const (
|
|
|
|
loggerKey ctxKey = iota
|
|
|
|
)
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2024-03-16 19:05:21 +01:00
|
|
|
func AddToContext(ctx context.Context, log FieldLogger) context.Context {
|
|
|
|
return context.WithValue(ctx, loggerKey, log)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
2024-03-16 19:05:21 +01:00
|
|
|
func FromContext(ctx context.Context) FieldLogger {
|
|
|
|
log, ok := ctx.Value(loggerKey).(FieldLogger)
|
|
|
|
if ok {
|
|
|
|
return log
|
|
|
|
}
|
|
|
|
return log
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2024-03-16 19:05:21 +01:00
|
|
|
func WithFields(fields Fields) FieldLogger {
|
|
|
|
return log.WithFields(fields)
|
|
|
|
}
|
2022-12-31 10:22:38 +01:00
|
|
|
|
2024-03-16 19:05:21 +01:00
|
|
|
func WithField(key string, value any) FieldLogger {
|
|
|
|
return log.WithField(key, value)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Info(args ...interface{}) {
|
2024-03-16 19:05:21 +01:00
|
|
|
log.Info(args...)
|
2023-05-29 20:03:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Infof(format string, args ...interface{}) {
|
2024-03-16 19:05:21 +01:00
|
|
|
log.Infof(format, args...)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Error(args ...interface{}) {
|
2024-03-16 19:05:21 +01:00
|
|
|
log.Error(args...)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 15:35:44 +02:00
|
|
|
func Errorf(format string, args ...interface{}) {
|
2024-03-16 19:05:21 +01:00
|
|
|
log.Errorf(format, args...)
|
2023-07-05 15:35:44 +02:00
|
|
|
}
|
|
|
|
|
2022-12-31 10:22:38 +01:00
|
|
|
func Warning(args ...interface{}) {
|
2024-03-16 19:05:21 +01:00
|
|
|
log.Warning(args...)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Fatal(args ...interface{}) {
|
2024-03-16 19:05:21 +01:00
|
|
|
log.Fatal(args...)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
2024-03-16 15:41:30 +01:00
|
|
|
func Fatalf(format string, args ...interface{}) {
|
2024-03-16 19:05:21 +01:00
|
|
|
log.Fatalf(format, args...)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func LogCommand(command interface{}) {
|
|
|
|
context, _ := json.Marshal(command)
|
|
|
|
|
|
|
|
commandName := reflect.TypeOf(command).Elem().Name()
|
|
|
|
|
2023-01-30 14:46:59 +01:00
|
|
|
var commandAsFields logrus.Fields
|
|
|
|
json.Unmarshal(context, &commandAsFields)
|
|
|
|
|
2024-03-16 19:05:21 +01:00
|
|
|
log.
|
2023-01-30 14:46:59 +01:00
|
|
|
WithFields(logrus.Fields{
|
|
|
|
"command_name": commandName,
|
|
|
|
"command": commandAsFields,
|
|
|
|
}).Info("Start command " + commandName)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func LogCommandFailed(command interface{}, err error) {
|
|
|
|
commandName := reflect.TypeOf(command).Elem().Name()
|
2024-03-16 19:05:21 +01:00
|
|
|
log.
|
2023-01-30 14:46:59 +01:00
|
|
|
WithFields(logrus.Fields{
|
|
|
|
"reason": err.Error(),
|
|
|
|
}).Info("Command failed" + commandName)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|