mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 17:42:14 +08:00
Merge branch 'master' into fix-1700
This commit is contained in:
commit
13357b854f
35
logger.go
35
logger.go
@ -35,9 +35,9 @@ type LoggerConfig struct {
|
|||||||
// Optional. Default value is gin.DefaultWriter.
|
// Optional. Default value is gin.DefaultWriter.
|
||||||
Output io.Writer
|
Output io.Writer
|
||||||
|
|
||||||
// SkipPathes is a url path array which logs are not written.
|
// SkipPaths is a url path array which logs are not written.
|
||||||
// Optional.
|
// Optional.
|
||||||
SkipPathes []string
|
SkipPaths []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter
|
// LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter
|
||||||
@ -45,15 +45,24 @@ type LogFormatter func(params LogFormatterParams) string
|
|||||||
|
|
||||||
// LogFormatterParams is the structure any formatter will be handed when time to log comes
|
// LogFormatterParams is the structure any formatter will be handed when time to log comes
|
||||||
type LogFormatterParams struct {
|
type LogFormatterParams struct {
|
||||||
Request *http.Request
|
Request *http.Request
|
||||||
TimeStamp time.Time
|
|
||||||
StatusCode int
|
// TimeStamp shows the time after the server returns a response.
|
||||||
Latency time.Duration
|
TimeStamp time.Time
|
||||||
ClientIP string
|
// StatusCode is HTTP response code.
|
||||||
Method string
|
StatusCode int
|
||||||
Path string
|
// Latency is how much time the server cost to process a certain request.
|
||||||
|
Latency time.Duration
|
||||||
|
// ClientIP equals Context's ClientIP method.
|
||||||
|
ClientIP string
|
||||||
|
// Method is the HTTP method given to the request.
|
||||||
|
Method string
|
||||||
|
// Path is a path the client requests.
|
||||||
|
Path string
|
||||||
|
// ErrorMessage is set if error has occurred in processing the request.
|
||||||
ErrorMessage string
|
ErrorMessage string
|
||||||
IsTerm bool
|
// IsTerm shows whether does gin's output descriptor refers to a terminal.
|
||||||
|
IsTerm bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultLogFormatter is the default log format function Logger middleware uses.
|
// defaultLogFormatter is the default log format function Logger middleware uses.
|
||||||
@ -114,8 +123,8 @@ func LoggerWithFormatter(f LogFormatter) HandlerFunc {
|
|||||||
// Example: os.Stdout, a file opened in write mode, a socket...
|
// Example: os.Stdout, a file opened in write mode, a socket...
|
||||||
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
|
||||||
return LoggerWithConfig(LoggerConfig{
|
return LoggerWithConfig(LoggerConfig{
|
||||||
Output: out,
|
Output: out,
|
||||||
SkipPathes: notlogged,
|
SkipPaths: notlogged,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +140,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
out = DefaultWriter
|
out = DefaultWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
notlogged := conf.SkipPathes
|
notlogged := conf.SkipPaths
|
||||||
|
|
||||||
isTerm := true
|
isTerm := true
|
||||||
|
|
||||||
|
@ -320,8 +320,8 @@ func TestLoggerWithConfigSkippingPaths(t *testing.T) {
|
|||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
router := New()
|
router := New()
|
||||||
router.Use(LoggerWithConfig(LoggerConfig{
|
router.Use(LoggerWithConfig(LoggerConfig{
|
||||||
Output: buffer,
|
Output: buffer,
|
||||||
SkipPathes: []string{"/skipped"},
|
SkipPaths: []string{"/skipped"},
|
||||||
}))
|
}))
|
||||||
router.GET("/logged", func(c *Context) {})
|
router.GET("/logged", func(c *Context) {})
|
||||||
router.GET("/skipped", func(c *Context) {})
|
router.GET("/skipped", func(c *Context) {})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user