/
voltage
/
tcheck
Обзор
Документация
Войти
/
voltage
/
tcheck
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/pkg/logger/handler.go
38 строк
1 KB
Владимир
Add pkg/logger
02 июн 2026, 10:17
02 июн 2026, 10:17
325e969
Код
Авторство
О чём код?
package logger import ( "context" "log/slog" ) type ctxKey string const RequestIDKey ctxKey = "request_id" type ContextHandler struct { slog.Handler } // Handle перехватывает запись лога и внедряет поля из контекста func (h *ContextHandler) Handle(ctx context.Context, r slog.Record) error { if ctx == nil { return h.Handler.Handle(ctx, r) } // Проверяем наличие request_id в контексте if reqID, ok := ctx.Value(RequestIDKey).(string); ok && reqID != "" { r.AddAttrs(slog.String("request_id", reqID)) } return h.Handler.Handle(ctx, r) } // WithAttrs переопределяем для сохранения обертки func (h *ContextHandler) WithAttrs(attrs []slog.Attr) slog.Handler { return &ContextHandler{Handler: h.Handler.WithAttrs(attrs)} } // WithGroup переопределяем для сохранения обертки func (h *ContextHandler) WithGroup(name string) slog.Handler { return &ContextHandler{Handler: h.Handler.WithGroup(name)} }