Skip to content

logger: allow skipping query string output #4019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [How to write log file](#how-to-write-log-file)
- [Custom Log Format](#custom-log-format)
- [Controlling Log output coloring](#controlling-log-output-coloring)
- [Avoid logging query strings](#avoid-loging-query-strings)
- [Model binding and validation](#model-binding-and-validation)
- [Custom Validators](#custom-validators)
- [Only Bind Query String](#only-bind-query-string)
Expand Down Expand Up @@ -589,6 +590,20 @@ func main() {
}
```

### Avoid logging query strings

```go
func main() {
router := gin.New()

// SkipQueryString indicates that the logger should not log the query string.
// For example, /path?q=1 will be logged as /path
loggerConfig := gin.LoggerConfig{SkipQueryString: true}

router.Use(gin.LoggerWithConfig(loggerConfig))
}
```

### Model binding and validation

To bind a request body into a type, use model binding. We currently support binding of JSON, XML, YAML, TOML and standard form values (foo=bar&boo=baz).
Expand Down
7 changes: 6 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ type LoggerConfig struct {
// Optional. Default value is gin.DefaultWriter.
Output io.Writer

// SkipQueryString indicates that query strings should not be written
// for cases such as when API keys are passed via query strings.
// Optional. Default value is false.
SkipQueryString bool

// SkipPaths is an url path array which logs are not written.
// Optional.
SkipPaths []string
Expand Down Expand Up @@ -270,7 +275,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {

param.BodySize = c.Writer.Size()

if raw != "" {
if raw != "" && !conf.SkipQueryString {
path = path + "?" + raw
}

Expand Down
14 changes: 14 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,17 @@ func TestForceConsoleColor(t *testing.T) {
// reset console color mode.
consoleColorMode = autoColor
}

func TestLoggerWithConfigSkipQueryString(t *testing.T) {
buffer := new(strings.Builder)
router := New()
router.Use(LoggerWithConfig(LoggerConfig{
Output: buffer,
SkipQueryString: true,
}))
router.GET("/logged", func(c *Context) { c.Status(http.StatusOK) })

PerformRequest(router, "GET", "/logged?a=21")
assert.Contains(t, buffer.String(), "200")
assert.NotContains(t, buffer.String(), "a=21")
}