Skip to content

Commit

Permalink
update logger default shouldLogReqBody/shouldLogRespBody again
Browse files Browse the repository at this point in the history
  • Loading branch information
bughou committed Nov 30, 2021
1 parent d14e772 commit fd7af05
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A golang http router with regexp and document generation support.
[![Build Status](https://github.com/lovego/goa/actions/workflows/go.yml/badge.svg)](https://github.com/lovego/goa/actions/workflows/go.yml)
[![Coverage Status](https://coveralls.io/repos/github/lovego/goa/badge.svg?branch=master&1)](https://coveralls.io/github/lovego/goa)
[![Go Report Card](https://goreportcard.com/badge/github.com/lovego/goa)](https://goreportcard.com/report/github.com/lovego/goa)
[![Documentation](https://pkg.go.dev/badge/github.com/lovego/goa)](https://pkg.go.dev/github.com/lovego/goa@v0.2.9)
[![Documentation](https://pkg.go.dev/badge/github.com/lovego/goa)](https://pkg.go.dev/github.com/lovego/goa@v0.3.0)


## Usage
Expand Down
19 changes: 11 additions & 8 deletions middlewares/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,25 @@ func defaultPanicHandler(c *goa.Context) {
}

func shouldLogReqBody(c *goa.Context) bool {
if v := c.Request.Header.Get("Content-Type"); v != "" {
mediaType, _, _ := mime.ParseMediaType(v)
// multipart is often use by file uploading.
// big multipart request often is file uploading, so ignore it.
if body, _ := c.RequestBody(); len(body) > 1024 {
mediaType, _, _ := mime.ParseMediaType(c.Request.Header.Get("Content-Type"))
if strings.HasPrefix(mediaType, "multipart/") {
return false
}
}

return true
}

func shouldLogRespBody(c *goa.Context) bool {
method := c.Request.Method
if method == http.MethodGet || method == http.MethodPost &&
strings.Contains(c.Request.URL.Path, "query") &&
strings.Contains(c.Request.URL.Path, "search") {
return false
// big reading request often is of low value for debugging, so ignore it.
if len(c.ResponseBody()) > 1024 {
if c.Request.Method == http.MethodGet || c.Request.Method == http.MethodPost &&
strings.Contains(c.Request.URL.Path, "query") &&
strings.Contains(c.Request.URL.Path, "search") {
return false
}
}
return true
}
Expand Down

0 comments on commit fd7af05

Please sign in to comment.