Skip to content

Commit 7edb8c3

Browse files
committed
refactored fields extension on the logger
1 parent 686f4db commit 7edb8c3

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

coderd/httpmw/logger.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
3636
)
3737

3838
logContext := &RequestLoggerContext{
39-
Fields: map[string]any{},
4039
log: &httplog,
4140
written: false,
4241
}
@@ -87,21 +86,20 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
8786
}
8887

8988
type RequestLoggerContext struct {
90-
Fields map[string]any
91-
9289
log *slog.Logger
9390
written bool
9491
}
9592

93+
func (c *RequestLoggerContext) WithFields(fields ...slog.Field) {
94+
newLogger := c.log.With(fields...)
95+
c.log = &newLogger
96+
}
97+
9698
func (c *RequestLoggerContext) WriteLog(ctx context.Context, msg string, status int) {
9799
if c.written {
98100
return
99101
}
100102
c.written = true
101-
// append extra fields to the logger
102-
for k, v := range c.Fields {
103-
c.log.With(slog.F(k, v))
104-
}
105103

106104
if status >= http.StatusInternalServerError {
107105
c.log.Error(ctx, msg)

0 commit comments

Comments
 (0)