2fas-server/internal/common/logging/logger.go

136 lines
2.8 KiB
Go
Raw Normal View History

2022-12-31 10:22:38 +01:00
package logging
import (
"encoding/json"
"reflect"
"sync"
2023-05-29 20:03:01 +02:00
"github.com/sirupsen/logrus"
2022-12-31 10:22:38 +01:00
)
2023-07-05 15:35:44 +02:00
// TODO: do not log reuse on every request.
2022-12-31 10:22:38 +01:00
type Fields map[string]interface{}
var (
customLogger = New()
defaultFields = logrus.Fields{}
defaultFieldsMutex = sync.RWMutex{}
)
func New() *logrus.Logger {
logger := logrus.New()
logger.SetFormatter(&logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyLevel: "level",
logrus.FieldKeyMsg: "message",
},
})
logger.SetLevel(logrus.InfoLevel)
return logger
}
func WithDefaultField(key, value string) *logrus.Logger {
defaultFieldsMutex.Lock()
defer defaultFieldsMutex.Unlock()
defaultFields[key] = value
return customLogger
}
func Info(args ...interface{}) {
2023-02-06 23:00:30 +01:00
defaultFieldsMutex.Lock()
defer defaultFieldsMutex.Unlock()
2023-05-29 20:03:01 +02:00
customLogger.WithFields(defaultFields).Info(args...)
}
func Infof(format string, args ...interface{}) {
defaultFieldsMutex.Lock()
defer defaultFieldsMutex.Unlock()
customLogger.WithFields(defaultFields).Infof(format, args...)
2022-12-31 10:22:38 +01:00
}
func Error(args ...interface{}) {
2023-02-06 23:00:30 +01:00
defaultFieldsMutex.Lock()
defer defaultFieldsMutex.Unlock()
2023-05-29 20:03:01 +02:00
customLogger.WithFields(defaultFields).Error(args...)
2022-12-31 10:22:38 +01:00
}
2023-07-05 15:35:44 +02:00
func Errorf(format string, args ...interface{}) {
defaultFieldsMutex.Lock()
defer defaultFieldsMutex.Unlock()
customLogger.WithFields(defaultFields).Errorf(format, args...)
}
2022-12-31 10:22:38 +01:00
func Warning(args ...interface{}) {
2023-02-06 23:00:30 +01:00
defaultFieldsMutex.Lock()
defer defaultFieldsMutex.Unlock()
2022-12-31 10:22:38 +01:00
2023-05-29 20:03:01 +02:00
customLogger.WithFields(defaultFields).Warning(args...)
2022-12-31 10:22:38 +01:00
}
func Fatal(args ...interface{}) {
2023-02-06 23:00:30 +01:00
defaultFieldsMutex.Lock()
defer defaultFieldsMutex.Unlock()
2023-05-29 20:03:01 +02:00
customLogger.WithFields(defaultFields).Fatal(args...)
2022-12-31 10:22:38 +01:00
}
func WithField(key string, value interface{}) *logrus.Entry {
2023-02-06 23:00:30 +01:00
defaultFieldsMutex.Lock()
defer defaultFieldsMutex.Unlock()
2022-12-31 10:22:38 +01:00
return customLogger.
2023-02-06 23:00:30 +01:00
WithFields(defaultFields).
WithField(key, value)
2022-12-31 10:22:38 +01:00
}
func WithFields(fields Fields) *logrus.Entry {
2023-02-06 23:00:30 +01:00
defaultFieldsMutex.Lock()
defer defaultFieldsMutex.Unlock()
2022-12-31 10:22:38 +01:00
return customLogger.
WithFields(logrus.Fields(fields)).
WithFields(defaultFields)
}
func LogCommand(command interface{}) {
2023-02-06 23:00:30 +01:00
defaultFieldsMutex.Lock()
defer defaultFieldsMutex.Unlock()
2022-12-31 10:22:38 +01:00
context, _ := json.Marshal(command)
commandName := reflect.TypeOf(command).Elem().Name()
var commandAsFields logrus.Fields
json.Unmarshal(context, &commandAsFields)
customLogger.
WithFields(defaultFields).
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) {
2023-02-06 23:00:30 +01:00
defaultFieldsMutex.Lock()
defer defaultFieldsMutex.Unlock()
2022-12-31 10:22:38 +01:00
commandName := reflect.TypeOf(command).Elem().Name()
customLogger.
WithFields(defaultFields).
WithFields(logrus.Fields{
"reason": err.Error(),
}).Info("Command failed" + commandName)
2022-12-31 10:22:38 +01:00
}