File tree Expand file tree Collapse file tree 2 files changed +46
-1
lines changed Expand file tree Collapse file tree 2 files changed +46
-1
lines changed 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 {
35
35
slog .F ("start" , start ),
36
36
)
37
37
38
- next .ServeHTTP (sw , r )
38
+ logContext := & RequestLoggerContext {}
39
+ defer func () {
40
+ logContext .WriteLog (r .Context (), "" , sw .Status )
41
+ }()
42
+
43
+ ctx := context .WithValue (r .Context (), logContextKey {}, logContext )
44
+
45
+ next .ServeHTTP (sw , r .WithContext (ctx ))
39
46
40
47
end := time .Now ()
41
48
@@ -74,3 +81,37 @@ func Logger(log slog.Logger) func(next http.Handler) http.Handler {
74
81
})
75
82
}
76
83
}
84
+
85
+ type RequestLoggerContext struct {
86
+ Fields map [string ]any
87
+
88
+ log * slog.Logger
89
+ written bool
90
+ }
91
+
92
+ func (c * RequestLoggerContext ) WriteLog (ctx context.Context , msg string , status int ) {
93
+ if c .written {
94
+ return
95
+ }
96
+ c .written = true
97
+ // append extra fields to the logger
98
+ for k , v := range c .Fields {
99
+ c .log .With (slog .F (k , v ))
100
+ }
101
+
102
+ if status >= http .StatusInternalServerError {
103
+ c .log .Error (ctx , msg )
104
+ } else {
105
+ c .log .Debug (ctx , msg )
106
+ }
107
+ }
108
+
109
+ type logContextKey struct {}
110
+
111
+ func FromContext (ctx context.Context ) * RequestLoggerContext {
112
+ val := ctx .Value (logContextKey {})
113
+ if logCtx , ok := val .(* RequestLoggerContext ); ok {
114
+ return logCtx
115
+ }
116
+ return nil
117
+ }
Original file line number Diff line number Diff line change @@ -554,6 +554,10 @@ func (f *logFollower) follow() {
554
554
return
555
555
}
556
556
557
+ // write the initial logs to the connection
558
+ httpmw .FromContext (f .ctx ).WriteLog (
559
+ f .ctx , "ProvisionerJobs log follower WS connection established" , http .StatusAccepted )
560
+
557
561
// no need to wait if the job is done
558
562
if f .complete {
559
563
return
You can’t perform that action at this time.
0 commit comments