diff --git a/server.go b/server.go index afca1cd..856acd1 100644 --- a/server.go +++ b/server.go @@ -43,10 +43,12 @@ type ServerCtx struct { Request Request Response Response - // Unique id of the context. - // Used by ServerCtx.Logger(). + // Unique id of the request. ID uint64 + // Start time for the request processing. + Time time.Time + logger ctxLogger s *Server c remoteAddrer @@ -286,6 +288,8 @@ func (s *Server) serveConn(c io.ReadWriter, ctxP **ServerCtx) error { } break } + ctx.ID++ + ctx.Time = time.Now() s.Handler(ctx) shadow := atomic.LoadPointer(&ctx.shadow) if shadow != nil { @@ -308,7 +312,6 @@ func (s *Server) serveConn(c io.ReadWriter, ctxP **ServerCtx) error { break } } - ctx.ID++ } return err } diff --git a/server_test.go b/server_test.go index 6352d3d..a41980c 100644 --- a/server_test.go +++ b/server_test.go @@ -172,10 +172,10 @@ func TestServerLogger(t *testing.T) { verifyResponse(t, br, 200, "text/html", "requestURI=/foo1, body=\"\", remoteAddr=1.2.3.4:8765") verifyResponse(t, br, 200, "text/html", "requestURI=/foo2, body=\"abcde\", remoteAddr=1.2.3.4:8765") - expectedLogOut := `#0000000100000000 - 1.2.3.4:8765 - GET http://google.com/foo1 - begin -#0000000100000000 - 1.2.3.4:8765 - GET http://google.com/foo1 - end -#0000000100000001 - 1.2.3.4:8765 - POST http://aaa.com/foo2 - begin -#0000000100000001 - 1.2.3.4:8765 - POST http://aaa.com/foo2 - end + expectedLogOut := `#0000000100000001 - 1.2.3.4:8765 - GET http://google.com/foo1 - begin +#0000000100000001 - 1.2.3.4:8765 - GET http://google.com/foo1 - end +#0000000100000002 - 1.2.3.4:8765 - POST http://aaa.com/foo2 - begin +#0000000100000002 - 1.2.3.4:8765 - POST http://aaa.com/foo2 - end ` if cl.out != expectedLogOut { t.Fatalf("Unexpected logger output: %q. Expected %q", cl.out, expectedLogOut)