@@ -51,7 +51,7 @@ const (
51
51
)
52
52
53
53
// AccessLogApacheMiddleware produces the access log following a format inspired by Apache
54
- // mod_log_config. It depends on TimerMiddleware and RecorderMiddleware that must be in the wrapped
54
+ // mod_log_config. It depends on TimerMiddleware and RecorderMiddleware that should be in the wrapped
55
55
// middlewares. It also uses request.Env["REMOTE_USER"].(string) set by the auth middlewares.
56
56
type AccessLogApacheMiddleware struct {
57
57
@@ -105,8 +105,8 @@ var apacheAdapter = strings.NewReplacer(
105
105
"%r" , "{{.R.Method}} {{.R.URL.RequestURI}} {{.R.Proto}}" ,
106
106
"%s" , "{{.StatusCode}}" ,
107
107
"%S" , "\033 [{{.StatusCode | statusCodeColor}}m{{.StatusCode}}" ,
108
- "%t" , "{{.StartTime. Format \" 02/Jan/2006:15:04:05 -0700\" }}" ,
109
- "%T" , "{{.ResponseTime. Seconds | printf \" %.3f\" }}" ,
108
+ "%t" , "{{if .StartTime}}{{.StartTime. Format \" 02/Jan/2006:15:04:05 -0700\" }}{{end }}" ,
109
+ "%T" , "{{if .ResponseTime}}{{.ResponseTime. Seconds | printf \" %.3f\" }}{{end }}" ,
110
110
"%u" , "{{.RemoteUser | dashIfEmptyStr}}" ,
111
111
"%{User-Agent}i" , "{{.R.UserAgent | dashIfEmptyStr}}" ,
112
112
"%{Referer}i" , "{{.R.Referer | dashIfEmptyStr}}" ,
@@ -185,7 +185,10 @@ func (u *accessLogUtil) ApacheQueryString() string {
185
185
186
186
// When the request entered the timer middleware.
187
187
func (u * accessLogUtil ) StartTime () * time.Time {
188
- return u .R .Env ["START_TIME" ].(* time.Time )
188
+ if u .R .Env ["START_TIME" ] != nil {
189
+ return u .R .Env ["START_TIME" ].(* time.Time )
190
+ }
191
+ return nil
189
192
}
190
193
191
194
// If remoteAddr is set then return is without the port number, apache log style.
@@ -200,12 +203,18 @@ func (u *accessLogUtil) ApacheRemoteAddr() string {
200
203
201
204
// As recorded by the recorder middleware.
202
205
func (u * accessLogUtil ) StatusCode () int {
203
- return u .R .Env ["STATUS_CODE" ].(int )
206
+ if u .R .Env ["STATUS_CODE" ] != nil {
207
+ return u .R .Env ["STATUS_CODE" ].(int )
208
+ }
209
+ return 0
204
210
}
205
211
206
212
// As mesured by the timer middleware.
207
213
func (u * accessLogUtil ) ResponseTime () * time.Duration {
208
- return u .R .Env ["ELAPSED_TIME" ].(* time.Duration )
214
+ if u .R .Env ["ELAPSED_TIME" ] != nil {
215
+ return u .R .Env ["ELAPSED_TIME" ].(* time.Duration )
216
+ }
217
+ return nil
209
218
}
210
219
211
220
// Process id.
@@ -215,5 +224,8 @@ func (u *accessLogUtil) Pid() int {
215
224
216
225
// As recorded by the recorder middleware.
217
226
func (u * accessLogUtil ) BytesWritten () int64 {
218
- return u .R .Env ["BYTES_WRITTEN" ].(int64 )
227
+ if u .R .Env ["BYTES_WRITTEN" ] != nil {
228
+ return u .R .Env ["BYTES_WRITTEN" ].(int64 )
229
+ }
230
+ return 0
219
231
}
0 commit comments