mirror of
https://github.com/gin-gonic/gin.git
synced 2026-04-11 14:11:46 +08:00
chore(logger): allow skipping query string output (#4547)
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. Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
ba093d1947
commit
db309081bc
15
docs/doc.md
15
docs/doc.md
@ -22,6 +22,7 @@
|
|||||||
- [How to write log file](#how-to-write-log-file)
|
- [How to write log file](#how-to-write-log-file)
|
||||||
- [Custom Log Format](#custom-log-format)
|
- [Custom Log Format](#custom-log-format)
|
||||||
- [Controlling Log output coloring](#controlling-log-output-coloring)
|
- [Controlling Log output coloring](#controlling-log-output-coloring)
|
||||||
|
- [Avoid logging query strings](#avoid-loging-query-strings)
|
||||||
- [Model binding and validation](#model-binding-and-validation)
|
- [Model binding and validation](#model-binding-and-validation)
|
||||||
- [Custom Validators](#custom-validators)
|
- [Custom Validators](#custom-validators)
|
||||||
- [Only Bind Query String](#only-bind-query-string)
|
- [Only Bind Query String](#only-bind-query-string)
|
||||||
@ -592,6 +593,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
|
### 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).
|
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).
|
||||||
|
|||||||
@ -48,6 +48,11 @@ type LoggerConfig struct {
|
|||||||
// Optional.
|
// Optional.
|
||||||
SkipPaths []string
|
SkipPaths []string
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
// Skip is a Skipper that indicates which logs should not be written.
|
// Skip is a Skipper that indicates which logs should not be written.
|
||||||
// Optional.
|
// Optional.
|
||||||
Skip Skipper
|
Skip Skipper
|
||||||
@ -298,7 +303,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
|
|
||||||
param.BodySize = c.Writer.Size()
|
param.BodySize = c.Writer.Size()
|
||||||
|
|
||||||
if raw != "" {
|
if raw != "" && !conf.SkipQueryString {
|
||||||
path = path + "?" + raw
|
path = path + "?" + raw
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -471,3 +471,17 @@ func TestForceConsoleColor(t *testing.T) {
|
|||||||
// reset console color mode.
|
// reset console color mode.
|
||||||
consoleColorMode = autoColor
|
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")
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user