Skip to content

Commit

Permalink
improve readme and doc and comments in test
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Nov 21, 2023
1 parent 2f539f2 commit af2430a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ JSON formatted logs can also be converted back to text later/after capture and s

The `log.Colors` can be used by callers and they'll be empty string when not in color mode, and the ansi escape codes otherwise.

# HTTP request/response logging

`LogAndCall()` combines `LogRequest` and `LogResponse` for a light middleware recording what happens during serving of a request (both incoming and outgoing attributes).

For instance (most attributes elided for brievety, also logs client cert and TLSInfo if applicable)
```json
{"level":"info","msg":"test-log-and-call2","method":"GET","url":{"Path":"/tea"},"status":418,"size":5,"microsec":100042}
```

# Config

You can either use `fortio.org/cli` or `fortio.org/scli` (or `dflags`) for configuration using flags (or dynamic flags and config map) or use the environment variables:
Expand Down
9 changes: 6 additions & 3 deletions http_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,14 @@ func (rr *ResponseRecorder) WriteHeader(code int) {
rr.StatusCode = code
}

// LogResponse logs the response code, byte size and duration of the request.
// additional key:value pairs can be passed as extraAttributes.
// LogAndCall logs the incoming request and the response code, byte size and duration
// of the request.
//
// If Config.CombineRequestAndResponse or the LOGGER_COMBINE_REQUEST_AND_RESPONSE
// environment variable is true. then a single log entry is done combining request and
// response information, including catching for panic
// response information, including catching for panic.
//
// Additional key:value pairs can be passed as extraAttributes.
//
//nolint:revive // name is fine.
func LogAndCall(msg string, hf http.HandlerFunc, extraAttributes ...KeyVal) http.HandlerFunc {
Expand Down
4 changes: 2 additions & 2 deletions http_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestLogAndCall(t *testing.T) {
Config.LogFileAndLine = false
Config.JSON = true
Config.NoTimestamp = true
Config.CombineRequestAndResponse = false // single line test
Config.CombineRequestAndResponse = false // Separate request and response logging
var b bytes.Buffer
w := bufio.NewWriter(&b)
SetOutput(w)
Expand All @@ -96,7 +96,7 @@ func TestLogAndCall(t *testing.T) {
}
hr.URL = &url.URL{Path: "/tea"}
b.Reset()
Config.CombineRequestAndResponse = true // single line test
Config.CombineRequestAndResponse = true // Combined logging test
LogAndCall("test-log-and-call2", testHandler).ServeHTTP(hw, hr)
w.Flush()
actual = b.String()
Expand Down

0 comments on commit af2430a

Please sign in to comment.