<commit_before>package middlewares

import (
	"net/http"

	"math/rand"
	"time"

	"context"

	"github.com/uber-go/zap"
	"github.com/urfave/negroni"
)

type ZapLoggerMiddleware struct {
	logger zap.Logger
}

func NewZapLoggerMiddleware(logger zap.Logger) *ZapLoggerMiddleware {
	return &ZapLoggerMiddleware{logger: logger}
}

func (middleware *ZapLoggerMiddleware) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request, next http.HandlerFunc) {
	startTime := time.Now()

	requestLogger := middleware.logger.With(zap.Int64("request-id", rand.Int63()))

	requestLogger.Info(
		"HTTP Request started",
		zap.String("host", request.Host),
		zap.String("method", request.Method),
		zap.String("path", request.URL.Path),
	)

	negroniResponseWriter, ok := responseWriter.(negroni.ResponseWriter)
	if !ok {
		negroniResponseWriter = negroni.NewResponseWriter(responseWriter)
	}

	next(responseWriter, request.WithContext(context.WithValue(nil, "logger", requestLogger)))

	fields := []zap.Field{
		zap.String("host", request.Host),
		zap.String("method", request.Method),
		zap.String("path", request.URL.Path),
		zap.Int("status-code", negroniResponseWriter.Status()),
		zap.Int("body-size", negroniResponseWriter.Size()),
		zap.Duration("duration", time.Since(startTime)),
	}
	if negroniResponseWriter.Status() >= 300 && negroniResponseWriter.Status() < 400 {
		fields = append(fields, zap.String("Location", negroniResponseWriter.Header().Get("Location")))
	}
	requestLogger.Info("HTTP Request completed", fields...)
}<commit_msg>Use negroni response writer to get body size in log message<commit_after>package middlewares

import (
	"net/http"

	"math/rand"
	"time"

	"context"

	"github.com/uber-go/zap"
	"github.com/urfave/negroni"
)

type ZapLoggerMiddleware struct {
	logger zap.Logger
}

func NewZapLoggerMiddleware(logger zap.Logger) *ZapLoggerMiddleware {
	return &ZapLoggerMiddleware{logger: logger}
}

func (middleware *ZapLoggerMiddleware) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request, next http.HandlerFunc) {
	startTime := time.Now()

	requestLogger := middleware.logger.With(zap.Int64("request-id", rand.Int63()))

	requestLogger.Info(
		"HTTP Request started",
		zap.String("host", request.Host),
		zap.String("method", request.Method),
		zap.String("path", request.URL.Path),
	)

	negroniResponseWriter, ok := responseWriter.(negroni.ResponseWriter)
	if !ok {
		negroniResponseWriter = negroni.NewResponseWriter(responseWriter)
	}

	next(negroniResponseWriter, request.WithContext(context.WithValue(nil, "logger", requestLogger)))

	fields := []zap.Field{
		zap.String("host", request.Host),
		zap.String("method", request.Method),
		zap.String("path", request.URL.Path),
		zap.Int("status-code", negroniResponseWriter.Status()),
		zap.Int("body-size", negroniResponseWriter.Size()),
		zap.Duration("duration", time.Since(startTime)),
	}
	if negroniResponseWriter.Status() >= 300 && negroniResponseWriter.Status() < 400 {
		fields = append(fields, zap.String("Location", negroniResponseWriter.Header().Get("Location")))
	}
	requestLogger.Info("HTTP Request completed", fields...)
}
{'fraction_non_alphanumeric': 0.15030380556443876, 'fraction_numerical': 0.0063959066197633516, 'mean_word_length': 5.93569844789357, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1342511', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1036, 'n_words': 176}