mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
Merge 7a541e3822b3d5dfdf832e4c0d9c604f5fdb840b into a481ee2897af1e368de5c919fbeb21b89aa26fc7
This commit is contained in:
commit
cb31c7ebff
17
logger.go
17
logger.go
@ -9,6 +9,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mattn/go-isatty"
|
"github.com/mattn/go-isatty"
|
||||||
@ -223,12 +224,16 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var skip map[string]struct{}
|
var skip map[string]struct{}
|
||||||
|
var skipSub bool
|
||||||
|
|
||||||
if length := len(notlogged); length > 0 {
|
if length := len(notlogged); length > 0 {
|
||||||
skip = make(map[string]struct{}, length)
|
skip = make(map[string]struct{}, length)
|
||||||
|
|
||||||
for _, path := range notlogged {
|
for _, path := range notlogged {
|
||||||
skip[path] = struct{}{}
|
skip[path] = struct{}{}
|
||||||
|
if strings.HasSuffix(path, "/") {
|
||||||
|
skipSub = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,7 +247,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
// Log only when path is not being skipped
|
// Log only when path is not being skipped
|
||||||
if _, ok := skip[path]; !ok {
|
if _, ok := skip[path]; !ok && (!skipSub || !willSkipLog(path, skip)) {
|
||||||
param := LogFormatterParams{
|
param := LogFormatterParams{
|
||||||
Request: c.Request,
|
Request: c.Request,
|
||||||
isTerm: isTerm,
|
isTerm: isTerm,
|
||||||
@ -270,3 +275,13 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// willSkipLog if skip path is "/attachments/", url like "/attachments/producthunt/*" will be skipped
|
||||||
|
func willSkipLog(path string, skip map[string]struct{}) bool {
|
||||||
|
for p := range skip {
|
||||||
|
if strings.HasPrefix(path, p[:len(p)-1]) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user