15357fd3c0
yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarbacreate and moove into new directories for BegushiyBashkir and yalarba
46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
// pkg/middleware/logger.go
|
|
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"api_bb/pkg/logger"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// Logger middleware для логирования HTTP запросов
|
|
func ZapLogger(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
|
|
// Получаем request ID
|
|
reqID := middleware.GetReqID(r.Context())
|
|
|
|
// Создаем логгер с контекстом запроса
|
|
requestLogger := logger.Get().With(
|
|
zap.String("method", r.Method),
|
|
zap.String("path", r.URL.Path),
|
|
zap.String("remote_addr", r.RemoteAddr),
|
|
zap.String("user_agent", r.UserAgent()),
|
|
zap.String("request_id", reqID),
|
|
)
|
|
|
|
// Обертываем ResponseWriter для получения статуса
|
|
wrappedWriter := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
|
|
|
|
// Обрабатываем запрос
|
|
next.ServeHTTP(wrappedWriter, r)
|
|
|
|
// Логируем результат
|
|
duration := time.Since(start)
|
|
|
|
requestLogger.Info("request completed",
|
|
zap.Int("status", wrappedWriter.Status()),
|
|
zap.Int("bytes", wrappedWriter.BytesWritten()),
|
|
zap.Duration("duration", duration),
|
|
)
|
|
})
|
|
} |