Skip to content

Commit 5feb4e4

Browse files
committed
fixed log formatting
1 parent 7edb8c3 commit 5feb4e4

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

coderd/httpmw/logger.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
3838
logContext := &RequestLoggerContext{
3939
log: &httplog,
4040
written: false,
41+
message: r.Method,
4142
}
4243
defer func() {
43-
logContext.WriteLog(r.Context(), "", sw.Status)
44+
logContext.WriteLog(r.Context(), sw.Status)
4445
}()
4546

4647
ctx := context.WithValue(r.Context(), logContextKey{}, logContext)
@@ -88,23 +89,24 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
8889
type RequestLoggerContext struct {
8990
log *slog.Logger
9091
written bool
92+
message string
9193
}
9294

9395
func (c *RequestLoggerContext) WithFields(fields ...slog.Field) {
9496
newLogger := c.log.With(fields...)
9597
c.log = &newLogger
9698
}
9799

98-
func (c *RequestLoggerContext) WriteLog(ctx context.Context, msg string, status int) {
100+
func (c *RequestLoggerContext) WriteLog(ctx context.Context, status int) {
99101
if c.written {
100102
return
101103
}
102104
c.written = true
103105

104106
if status >= http.StatusInternalServerError {
105-
c.log.Error(ctx, msg)
107+
c.log.Error(ctx, c.message, "status_code", status)
106108
} else {
107-
c.log.Debug(ctx, msg)
109+
c.log.Debug(ctx, c.message, "status_code", status)
108110
}
109111
}
110112

coderd/provisionerjobs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ func (f *logFollower) follow() {
556556

557557
// write the initial logs to the connection
558558
httpmw.FromContext(f.ctx).WriteLog(
559-
f.ctx, "ProvisionerJobs log follower WS connection established", http.StatusAccepted)
559+
f.ctx, http.StatusAccepted)
560560

561561
// no need to wait if the job is done
562562
if f.complete {

0 commit comments

Comments
 (0)