You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/middleware/logger.md
+124-4Lines changed: 124 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,13 @@ Echo has 2 different logger middlewares:
11
11
- Older string template based logger [`Logger`](https://github.com/labstack/echo/blob/master/middleware/logger.go) - easy to start with but has limited capabilities
12
12
- Newer customizable function based logger [`RequestLogger`](https://github.com/labstack/echo/blob/master/middleware/request_logger.go) - allows developer fully to customize what is logged and how it is logged. Suitable for usage with 3rd party logger libraries.
RequestLogger middleware allows developer fully to customize what is logged and how it is logged and is more suitable
112
119
for usage with 3rd party (structured logging) libraries.
113
-
114
-
See [`RequestLoggerConfig`](https://github.com/labstack/echo/blob/master/middleware/request_logger.go) structure fields for values that logger knows to extract.
120
+
```go
121
+
typeRequestLoggerConfigstruct {
122
+
// Skipper defines a function to skip middleware.
123
+
SkipperSkipper
124
+
125
+
// BeforeNextFunc defines a function that is called before next middleware or handler is called in chain.
126
+
BeforeNextFuncfunc(c echo.Context)
127
+
// LogValuesFunc defines a function that is called with values extracted by logger from request/response.
128
+
// Mandatory.
129
+
LogValuesFuncfunc(c echo.Context, v RequestLoggerValues) error
130
+
131
+
// HandleError instructs logger to call global error handler when next middleware/handler returns an error.
132
+
// This is useful when you have custom error handler that can decide to use different status codes.
133
+
//
134
+
// A side-effect of calling global error handler is that now Response has been committed and sent to the client
135
+
// and middlewares up in chain can not change Response status code or response body.
136
+
HandleErrorbool
137
+
138
+
// LogLatency instructs logger to record duration it took to execute rest of the handler chain (next(c) call).
139
+
LogLatencybool
140
+
// LogProtocol instructs logger to extract request protocol (i.e. `HTTP/1.1` or `HTTP/2`)
141
+
LogProtocolbool
142
+
// LogRemoteIP instructs logger to extract request remote IP. See `echo.Context.RealIP()` for implementation details.
143
+
LogRemoteIPbool
144
+
// LogHost instructs logger to extract request host value (i.e. `example.com`)
145
+
LogHostbool
146
+
// LogMethod instructs logger to extract request method value (i.e. `GET` etc)
147
+
LogMethodbool
148
+
// LogURI instructs logger to extract request URI (i.e. `/list?lang=en&page=1`)
149
+
LogURIbool
150
+
// LogURIPath instructs logger to extract request URI path part (i.e. `/list`)
151
+
LogURIPathbool
152
+
// LogRoutePath instructs logger to extract route path part to which request was matched to (i.e. `/user/:id`)
153
+
LogRoutePathbool
154
+
// LogRequestID instructs logger to extract request ID from request `X-Request-ID` header or response if request did not have value.
155
+
LogRequestIDbool
156
+
// LogReferer instructs logger to extract request referer values.
157
+
LogRefererbool
158
+
// LogUserAgent instructs logger to extract request user agent values.
159
+
LogUserAgentbool
160
+
// LogStatus instructs logger to extract response status code. If handler chain returns an echo.HTTPError,
161
+
// the status code is extracted from the echo.HTTPError returned
162
+
LogStatusbool
163
+
// LogError instructs logger to extract error returned from executed handler chain.
164
+
LogErrorbool
165
+
// LogContentLength instructs logger to extract content length header value. Note: this value could be different from
166
+
// actual request body size as it could be spoofed etc.
167
+
LogContentLengthbool
168
+
// LogResponseSize instructs logger to extract response content length value. Note: when used with Gzip middleware
169
+
// this value may not be always correct.
170
+
LogResponseSizebool
171
+
// LogHeaders instructs logger to extract given list of headers from request. Note: request can contain more than
172
+
// one header with same value so slice of values is been logger for each given header.
173
+
//
174
+
// Note: header values are converted to canonical form with http.CanonicalHeaderKey as this how request parser converts header
175
+
// names to. For example, the canonical key for "accept-encoding" is "Accept-Encoding".
176
+
LogHeaders []string
177
+
// LogQueryParams instructs logger to extract given list of query parameters from request URI. Note: request can
178
+
// contain more than one query parameter with same name so slice of values is been logger for each given query param name.
179
+
LogQueryParams []string
180
+
// LogFormValues instructs logger to extract given list of form values from request body+URI. Note: request can
181
+
// contain more than one form value with same name so slice of values is been logger for each given form value name.
#### 1. Solution for "panic: missing LogValuesFunc callback function for request logger middleware"
314
+
This panic arises when the `LogValuesFunc` callback function, which is mandatory for the request logger middleware configuration, is left unset.
315
+
316
+
To address this, you must define a suitable function that adheres to the `LogValuesFunc` specifications and then assign it within the middleware configuration. Consider the following straightforward illustration:
When investigating logging-related glitches, if you notice that certain parameters like `v.URI` and `v.Status` within the `LogValuesFunc` function produce empty outputs, your focus should shift to validating the relevant configuration elements. Specifically, check whether the corresponding items (such as `LogStatus`, `LogURI`, etc.) in `e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{...}))` have been erroneously set to `false` or failed to activate properly due to miscellaneous factors. Ensure these configuration particulars are accurately configured so that the pertinent request and response data can be precisely logged.
0 commit comments