Logger skip sub paths

When skip path like "attachments/", the sub path "attachments/producthunt/1.jpg" log will be skipped
This commit is contained in:
liasica 2021-10-12 15:39:10 +08:00 committed by GitHub
parent 5929d52171
commit 38bd93dfb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import (
"io"
"net/http"
"os"
"strings"
"time"
"github.com/mattn/go-isatty"
@ -221,12 +222,16 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
}
var skip map[string]struct{}
var skipSub bool
if length := len(notlogged); length > 0 {
skip = make(map[string]struct{}, length)
for _, path := range notlogged {
skip[path] = struct{}{}
if strings.HasSuffix(path, "/") {
skipSub = true
}
}
}
@ -240,7 +245,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
c.Next()
// Log only when path is not being skipped
if _, ok := skip[path]; !ok {
if _, ok := skip[path]; !ok && (!skipSub || !willSkipLog(path, skip)) {
param := LogFormatterParams{
Request: c.Request,
isTerm: isTerm,
@ -268,3 +273,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
}