11package router
22
33import (
4+ "fmt"
5+ "regexp"
46 "time"
57
68 "github.com/gin-contrib/cors"
@@ -22,7 +24,7 @@ import (
2224func Create (db * database.GormDatabase , vInfo * model.VersionInfo , conf * config.Configuration ) (* gin.Engine , func ()) {
2325 g := gin .New ()
2426
25- g .Use (gin .Logger ( ), gin .Recovery (), error .Handler (), location .Default ())
27+ g .Use (gin .LoggerWithFormatter ( logFormatter ), gin .Recovery (), error .Handler (), location .Default ())
2628 g .NoRoute (error .NotFound ())
2729
2830 streamHandler := stream .New (time .Duration (conf .Server .Stream .PingPeriodSeconds )* time .Second , 15 * time .Second , conf .Server .Stream .AllowedOrigins )
@@ -167,3 +169,28 @@ func Create(db *database.GormDatabase, vInfo *model.VersionInfo, conf *config.Co
167169 }
168170 return g , streamHandler .Close
169171}
172+
173+ var tokenRegexp = regexp .MustCompile ("token=[^&]+" )
174+
175+ func logFormatter (param gin.LogFormatterParams ) string {
176+ var statusColor , methodColor , resetColor string
177+ if param .IsOutputColor () {
178+ statusColor = param .StatusCodeColor ()
179+ methodColor = param .MethodColor ()
180+ resetColor = param .ResetColor ()
181+ }
182+
183+ if param .Latency > time .Minute {
184+ param .Latency = param .Latency - param .Latency % time .Second
185+ }
186+ path := tokenRegexp .ReplaceAllString (param .Path , "token=[masked]" )
187+ return fmt .Sprintf ("[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n %s" ,
188+ param .TimeStamp .Format ("2006/01/02 - 15:04:05" ),
189+ statusColor , param .StatusCode , resetColor ,
190+ param .Latency ,
191+ param .ClientIP ,
192+ methodColor , param .Method , resetColor ,
193+ path ,
194+ param .ErrorMessage ,
195+ )
196+ }
0 commit comments