Skip to content
Myr

chore(logger): allow skipping query string output (#4547)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This is useful for APIs that might have sensitive information in the query string, such as API keys. This patch does not change the default behavior of the code unless the new `SkipQueryString` config option is passed in. The "skip" term is a bit of a misnomer here, as this doesn't actually skip that log, but modifies the output. I'm open to suggestions for a more appropriate name.

authored by Jacob McSwain · committed by GitHub · parent ba093d1 · Browse files · 3 files+35 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

3 files+35 added−1 removed

  • other:+35 added−1 removed

3 files+35 added−1 removed

modified:docs/doc.md+15 added
20 unmodified lines
21 - [Custom Recovery behavior](#custom-recovery-behavior)22 - [How to write log file](#how-to-write-log-file)23 - [Custom Log Format](#custom-log-format)24 - [Controlling Log output coloring](#controlling-log-output-coloring)25 - [Avoid logging query strings](#avoid-loging-query-strings)26 - [Model binding and validation](#model-binding-and-validation)27 - [Custom Validators](#custom-validators)28 - [Only Bind Query String](#only-bind-query-string)29 - [Bind Query String or Post Data](#bind-query-string-or-post-data)
562 unmodified lines
592 router.Run(":8080")593}594```595596### Avoid logging query strings597598```go599func main() {600 router := gin.New()601 602 // SkipQueryString indicates that the logger should not log the query string.603 // For example, /path?q=1 will be logged as /path604 loggerConfig := gin.LoggerConfig{SkipQueryString: true}605 606 router.Use(gin.LoggerWithConfig(loggerConfig))607}608```609610### Model binding and validation611612To 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).613
2023 unmodified lines
modified:logger_test.go+14 added
469 unmodified lines
470471 // reset console color mode.472 consoleColorMode = autoColor473}474475func TestLoggerWithConfigSkipQueryString(t *testing.T) {476 buffer := new(strings.Builder)477 router := New()478 router.Use(LoggerWithConfig(LoggerConfig{479 Output: buffer,480 SkipQueryString: true,481 }))482 router.GET("/logged", func(c *Context) { c.Status(http.StatusOK) })483484 PerformRequest(router, "GET", "/logged?a=21")485 assert.Contains(t, buffer.String(), "200")486 assert.NotContains(t, buffer.String(), "a=21")487}
modified:logger.go+6 added-1 removed
46 unmodified lines
47 // SkipPaths is a URL path array which logs are not written.48 // Optional.49 SkipPaths []string5051 // SkipQueryString indicates that query strings should not be written52 // for cases such as when API keys are passed via query strings.53 // Optional. Default value is false.54 SkipQueryString bool5556 // Skip is a Skipper that indicates which logs should not be written.57 // Optional.58 Skip Skipper59}
242 unmodified lines
302 param.ErrorMessage = c.Errors.ByType(ErrorTypePrivate).String()303304 param.BodySize = c.Writer.Size()305301 if raw != "" {306 if raw != "" && !conf.SkipQueryString {307 path = path + "?" + raw308 }309310 param.Path = path
4 unmodified lines