- Notifications
You must be signed in to change notification settings - Fork1.2k
feat: log long-lived connections acceptance#17219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
0e397edc384706686f4db7edb8c35feb4e47500ef566e2c3e4e5f36eb7822b3a7724302a1dbe35a0fa0530bb53d89e987e16491167ea2dd6338fd202708286ca3c4d3dfa2f4f845ff0f1f7acfb58aa7362c63b32d268a3c8f550c581e1527b9e2b61deb1a4109e46d09b16cf92e331a37e9f083b82776f3d44727cb118a9754cee6d28f2bd57a49d5cb130deFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
…rovisionerJobs logs
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -35,7 +35,14 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler { | ||
| slog.F("start", start), | ||
| ) | ||
| logContext := &RequestLoggerContext{} | ||
| defer func() { | ||
| logContext.WriteLog(r.Context(), "", sw.Status) | ||
| }() | ||
| ctx := context.WithValue(r.Context(), logContextKey{}, logContext) | ||
| next.ServeHTTP(sw, r.WithContext(ctx)) | ||
ibetitsmike marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| end := time.Now() | ||
| @@ -74,3 +81,37 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler { | ||
| }) | ||
| } | ||
| } | ||
| type RequestLoggerContext struct { | ||
| Fields map[string]any | ||
ibetitsmike marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| log *slog.Logger | ||
| written bool | ||
| } | ||
| func (c *RequestLoggerContext) WriteLog(ctx context.Context, msg string, status int) { | ||
ibetitsmike marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if c.written { | ||
| return | ||
| } | ||
| c.written = true | ||
| // append extra fields to the logger | ||
| for k, v := range c.Fields { | ||
| c.log.With(slog.F(k, v)) | ||
| } | ||
| if status >= http.StatusInternalServerError { | ||
| c.log.Error(ctx, msg) | ||
| } else { | ||
| c.log.Debug(ctx, msg) | ||
| } | ||
| } | ||
| type logContextKey struct{} | ||
| func FromContext(ctx context.Context) *RequestLoggerContext { | ||
ibetitsmike marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| val := ctx.Value(logContextKey{}) | ||
| if logCtx, ok := val.(*RequestLoggerContext); ok { | ||
| return logCtx | ||
| } | ||
| return nil | ||
| } | ||