Skip to content

Commit

Permalink
api: add user-agent to access logs
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa committed Aug 30, 2018
1 parent b142d02 commit c12875d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api/middleware.go
Expand Up @@ -275,7 +275,7 @@ func (l *loggerMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, ne
if r.TLS != nil {
scheme = "https"
}
l.logger.Printf("%s %s %s %s %d in %0.6fms%s", nowFormatted, scheme, r.Method, r.URL.Path, statusCode, float64(duration)/float64(time.Millisecond), requestID)
l.logger.Printf("%s %s %s %s %d %q in %0.6fms%s", nowFormatted, scheme, r.Method, r.URL.Path, statusCode, r.UserAgent(), float64(duration)/float64(time.Millisecond), requestID)
}

type contentHijackMiddleware struct {
Expand Down
9 changes: 5 additions & 4 deletions api/middleware_test.go
Expand Up @@ -497,6 +497,7 @@ func (s *S) TestLoggerMiddleware(c *check.C) {
recorder := httptest.NewRecorder()
request, err := http.NewRequest("PUT", "/my/path", nil)
c.Assert(err, check.IsNil)
request.Header.Set("User-Agent", "ardata 1.1")
h, handlerLog := doHandler()
handlerLog.sleep = 100 * time.Millisecond
handlerLog.response = http.StatusOK
Expand All @@ -507,7 +508,7 @@ func (s *S) TestLoggerMiddleware(c *check.C) {
middle.ServeHTTP(negroni.NewResponseWriter(recorder), request, h)
c.Assert(handlerLog.called, check.Equals, true)
timePart := time.Now().Format(time.RFC3339Nano)[:19]
c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 in 1\d{2}\.\d+ms`+"\n", timePart))
c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 "ardata 1.1" in 1\d{2}\.\d+ms`+"\n", timePart))
}

func (s *S) TestLoggerMiddlewareWithoutStatusCode(c *check.C) {
Expand All @@ -524,7 +525,7 @@ func (s *S) TestLoggerMiddlewareWithoutStatusCode(c *check.C) {
middle.ServeHTTP(negroni.NewResponseWriter(recorder), request, h)
c.Assert(handlerLog.called, check.Equals, true)
timePart := time.Now().Format(time.RFC3339Nano)[:19]
c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 in 1\d{2}\.\d+ms`+"\n", timePart))
c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 "" in 1\d{2}\.\d+ms`+"\n", timePart))
}

func (s *S) TestLoggerMiddlewareWithRequestID(c *check.C) {
Expand All @@ -544,7 +545,7 @@ func (s *S) TestLoggerMiddlewareWithRequestID(c *check.C) {
middle.ServeHTTP(negroni.NewResponseWriter(recorder), request, h)
c.Assert(handlerLog.called, check.Equals, true)
timePart := time.Now().Format(time.RFC3339Nano)[:19]
c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 in 1\d{2}\.\d+ms \[Request-ID: my-rid\]`+"\n", timePart))
c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 "" in 1\d{2}\.\d+ms \[Request-ID: my-rid\]`+"\n", timePart))
}

func (s *S) TestLoggerMiddlewareHTTPS(c *check.C) {
Expand All @@ -567,7 +568,7 @@ func (s *S) TestLoggerMiddlewareHTTPS(c *check.C) {
c.Assert(rsp.StatusCode, check.Equals, http.StatusOK)
c.Assert(handlerLog.called, check.Equals, true)
timePart := time.Now().Format(time.RFC3339Nano)[:19]
c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? https PUT /my/path 200 in \d{1}\.\d+ms`+"\n", timePart))
c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? https PUT /my/path 200 "Go-http-client/1.1" in \d{1}\.\d+ms`+"\n", timePart))
}

func (s *S) TestContentHijackerMiddleware(c *check.C) {
Expand Down

0 comments on commit c12875d

Please sign in to comment.