MCPcopy
hub / github.com/labstack/echo / RequestLogger

Function RequestLogger

middleware/request_logger.go:395–446  ·  view source on GitHub ↗

RequestLogger creates Request Logger middleware with Echo default settings that uses Context.Logger() as logger.

()

Source from the content-addressed store, hash-verified

393
394// RequestLogger creates Request Logger middleware with Echo default settings that uses Context.Logger() as logger.
395func RequestLogger() echo.MiddlewareFunc {
396 return RequestLoggerWithConfig(RequestLoggerConfig{
397 LogLatency: true,
398 LogRemoteIP: true,
399 LogHost: true,
400 LogMethod: true,
401 LogURI: true,
402 LogRequestID: true,
403 LogUserAgent: true,
404 LogStatus: true,
405 LogContentLength: true,
406 LogResponseSize: true,
407 // forwards error to the global error handler, so it can decide appropriate status code.
408 // NB: side-effect of that is - request is now "committed" written to the client. Middlewares up in chain can not
409 // change Response status code or response body.
410 HandleError: true,
411 LogValuesFunc: func(c *echo.Context, v RequestLoggerValues) error {
412 logger := c.Logger()
413 if v.Error == nil {
414 logger.LogAttrs(context.Background(), slog.LevelInfo, "REQUEST",
415 slog.String("method", v.Method),
416 slog.String("uri", v.URI),
417 slog.Int("status", v.Status),
418 slog.Duration("latency", v.Latency),
419 slog.String("host", v.Host),
420 slog.String("bytes_in", v.ContentLength),
421 slog.Int64("bytes_out", v.ResponseSize),
422 slog.String("user_agent", v.UserAgent),
423 slog.String("remote_ip", v.RemoteIP),
424 slog.String("request_id", v.RequestID),
425 )
426 return nil
427 }
428
429 logger.LogAttrs(context.Background(), slog.LevelError, "REQUEST_ERROR",
430 slog.String("method", v.Method),
431 slog.String("uri", v.URI),
432 slog.Int("status", v.Status),
433 slog.Duration("latency", v.Latency),
434 slog.String("host", v.Host),
435 slog.String("bytes_in", v.ContentLength),
436 slog.Int64("bytes_out", v.ResponseSize),
437 slog.String("user_agent", v.UserAgent),
438 slog.String("remote_ip", v.RemoteIP),
439 slog.String("request_id", v.RequestID),
440
441 slog.String("error", v.Error.Error()),
442 )
443 return nil
444 },
445 })
446}

Callers 3

TestRequestLoggerOKFunction · 0.85
TestRequestLoggerErrorFunction · 0.85
TestTestRequestLoggerFunction · 0.85

Calls 7

RequestLoggerWithConfigFunction · 0.85
LoggerMethod · 0.80
IntMethod · 0.80
DurationMethod · 0.80
Int64Method · 0.80
StringMethod · 0.45
ErrorMethod · 0.45

Tested by 3

TestRequestLoggerOKFunction · 0.68
TestRequestLoggerErrorFunction · 0.68
TestTestRequestLoggerFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…