2024-08-22 08:49:05 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
2024-08-23 12:17:35 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/xvzc/SpoofDPI/util"
|
2024-08-22 08:49:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
scopeFieldName = "scope"
|
|
|
|
traceIdFieldName = "trace_id"
|
|
|
|
)
|
|
|
|
|
|
|
|
var logger zerolog.Logger
|
|
|
|
|
|
|
|
func GetCtxLogger(ctx context.Context) zerolog.Logger {
|
|
|
|
return logger.With().Ctx(ctx).Logger()
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitLogger(cfg *util.Config) {
|
|
|
|
partsOrder := []string{
|
|
|
|
zerolog.LevelFieldName,
|
2024-08-28 22:50:08 +00:00
|
|
|
zerolog.TimestampFieldName,
|
2024-08-22 08:49:05 +00:00
|
|
|
traceIdFieldName,
|
|
|
|
scopeFieldName,
|
|
|
|
zerolog.MessageFieldName,
|
|
|
|
}
|
|
|
|
|
|
|
|
consoleWriter := zerolog.ConsoleWriter{
|
|
|
|
Out: os.Stdout,
|
|
|
|
TimeFormat: time.RFC3339,
|
|
|
|
PartsOrder: partsOrder,
|
|
|
|
FormatPrepare: func(m map[string]any) error {
|
|
|
|
formatFieldValue[string](m, "%s", traceIdFieldName)
|
|
|
|
formatFieldValue[string](m, "[%s]", scopeFieldName)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
FieldsExclude: []string{traceIdFieldName, scopeFieldName},
|
|
|
|
}
|
|
|
|
|
|
|
|
logger = zerolog.New(consoleWriter).Hook(ctxHook{})
|
2024-08-23 12:17:35 +00:00
|
|
|
if cfg.Debug {
|
2024-08-22 08:49:05 +00:00
|
|
|
logger = logger.Level(zerolog.DebugLevel)
|
|
|
|
} else {
|
|
|
|
logger = logger.Level(zerolog.InfoLevel)
|
|
|
|
}
|
|
|
|
logger = logger.With().Timestamp().Logger()
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatFieldValue[T any](vs map[string]any, format string, field string) {
|
|
|
|
if v, ok := vs[field].(T); ok {
|
|
|
|
vs[field] = fmt.Sprintf(format, v)
|
|
|
|
} else {
|
|
|
|
vs[field] = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ctxHook struct{}
|
|
|
|
|
|
|
|
func (h ctxHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
|
|
|
|
if scope, ok := util.GetScopeFromCtx(e.GetCtx()); ok {
|
|
|
|
e.Str(scopeFieldName, scope)
|
|
|
|
}
|
|
|
|
if traceId, ok := util.GetTraceIdFromCtx(e.GetCtx()); ok {
|
|
|
|
e.Str(traceIdFieldName, traceId)
|
|
|
|
}
|
|
|
|
}
|