Skip to content

Commit

Permalink
fix #1260 change middleware.Logger's default output (#1336)
Browse files Browse the repository at this point in the history
* fix TestLoggerIPAddress reverse assertion

* change middleware.Logger default output

* remove nil field declaration
  • Loading branch information
nattawitc authored and vishr committed Jul 18, 2019
1 parent 405b221 commit 8cfaf50
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 4 additions & 2 deletions middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"io"
"os"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -74,7 +73,6 @@ var (
`"status":${status},"error":"${error}","latency":${latency},"latency_human":"${latency_human}"` +
`,"bytes_in":${bytes_in},"bytes_out":${bytes_out}}` + "\n",
CustomTimeFormat: "2006-01-02 15:04:05.00000",
Output: os.Stdout,
colorer: color.New(),
}
)
Expand Down Expand Up @@ -214,6 +212,10 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
return
}

if config.Output == nil {
_, err = c.Logger().Output().Write(buf.Bytes())
return
}
_, err = config.Output.Write(buf.Bytes())
return
}
Expand Down
6 changes: 3 additions & 3 deletions middleware/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ func TestLoggerIPAddress(t *testing.T) {
// With X-Real-IP
req.Header.Add(echo.HeaderXRealIP, ip)
h(c)
assert.Contains(t, ip, buf.String())
assert.Contains(t, buf.String(), ip)

// With X-Forwarded-For
buf.Reset()
req.Header.Del(echo.HeaderXRealIP)
req.Header.Add(echo.HeaderXForwardedFor, ip)
h(c)
assert.Contains(t, ip, buf.String())
assert.Contains(t, buf.String(), ip)

buf.Reset()
h(c)
assert.Contains(t, ip, buf.String())
assert.Contains(t, buf.String(), ip)
}

func TestLoggerTemplate(t *testing.T) {
Expand Down

0 comments on commit 8cfaf50

Please sign in to comment.