diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index f1ab240..7bb920b 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -1,13 +1,57 @@ package logger -import "log/slog" +import ( + "context" + "log/slog" + "os" -type logger struct { + "github.com/google/uuid" +) + +type LogLevel string + +type contextKey string + +const TraceIDKey contextKey = "trace_id" + +type Logger struct { *slog.Logger } -func NewLogger() *logger { - return &logger{ - Logger: slog.Default(), +func NewLogger(level LogLevel) *Logger { + return &Logger{ + Logger: slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + Level: slog.LevelInfo, // default log level + })), } } + +func GenerateTraceID() string { + return uuid.New().String() +} + +func WithTraceID(ctx context.Context) context.Context { + if ctx.Value(TraceIDKey) == nil { + return context.WithValue(ctx, TraceIDKey, GenerateTraceID()) + } + return ctx +} + +func GetTraceID(ctx context.Context) string { + if traceID, ok := ctx.Value(TraceIDKey).(string); ok { + return traceID + } + return "" +} + +func (l *Logger) Info(ctx context.Context, msg string, args ...any) { + traceID := GetTraceID(ctx) + if traceID != "" { + args = append(args, slog.String(string(TraceIDKey), traceID)) + } + l.Logger.Info(msg, args...) +} + +func (l *Logger) ErrorWithoutContext(msg string, args ...any) { + l.Logger.Error(msg, args...) +}