1
1
package router
2
2
3
3
import (
4
+ "fmt"
5
+ "regexp"
4
6
"time"
5
7
6
8
"github.com/gin-contrib/cors"
@@ -22,7 +24,7 @@ import (
22
24
func Create (db * database.GormDatabase , vInfo * model.VersionInfo , conf * config.Configuration ) (* gin.Engine , func ()) {
23
25
g := gin .New ()
24
26
25
- g .Use (gin .Logger ( ), gin .Recovery (), error .Handler (), location .Default ())
27
+ g .Use (gin .LoggerWithFormatter ( logFormatter ), gin .Recovery (), error .Handler (), location .Default ())
26
28
g .NoRoute (error .NotFound ())
27
29
28
30
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
167
169
}
168
170
return g , streamHandler .Close
169
171
}
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