2025-02-03 17:23:26 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-02-26 16:47:05 +00:00
|
|
|
"net/http"
|
2025-02-03 17:23:26 +00:00
|
|
|
|
|
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/logging"
|
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
2025-02-26 16:47:05 +00:00
|
|
|
kitlog "github.com/go-kit/log"
|
2025-02-03 17:23:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Logged wraps an endpoint and adds the error if the context supports it
|
|
|
|
|
func Logged(next endpoint.Endpoint) endpoint.Endpoint {
|
|
|
|
|
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
|
|
|
|
|
res, err := next(ctx, request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logging.WithErr(ctx, err)
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if errResp, ok := res.(interface{ Error() error }); ok {
|
|
|
|
|
err = errResp.Error()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logging.WithErr(ctx, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-26 16:47:05 +00:00
|
|
|
|
|
|
|
|
func LogRequestEnd(logger kitlog.Logger) func(context.Context, http.ResponseWriter) context.Context {
|
|
|
|
|
return func(ctx context.Context, w http.ResponseWriter) context.Context {
|
|
|
|
|
logCtx, ok := logging.FromContext(ctx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ctx
|
|
|
|
|
}
|
|
|
|
|
logCtx.Log(ctx, logger)
|
|
|
|
|
return ctx
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-01 16:02:24 +00:00
|
|
|
|
|
|
|
|
func LogResponseEndMiddleware(logger kitlog.Logger, next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
LogRequestEnd(logger)(r.Context(), w)
|
|
|
|
|
})
|
|
|
|
|
}
|