Feat: gin logger fileds

pull/134/head
zijiren233 2 years ago
parent e568218c14
commit a22d14386c

@ -2,9 +2,11 @@ package bootstrap
import (
"context"
"fmt"
"io"
"log"
"os"
"runtime"
"time"
"github.com/natefinch/lumberjack"
@ -25,6 +27,10 @@ func setLog(l *logrus.Logger) {
}
}
var logCallerIgnoreFuncs = map[string]struct{}{
"github.com/synctv-org/synctv/server/middlewares.Init.NewLog.func1": {},
}
func InitLog(ctx context.Context) (err error) {
setLog(logrus.StandardLogger())
forceColor := utils.ForceColor()
@ -61,6 +67,12 @@ func InitLog(ctx context.Context) (err error) {
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: time.DateTime,
CallerPrettyfier: func(f *runtime.Frame) (function string, file string) {
if _, ok := logCallerIgnoreFuncs[f.Function]; ok {
return "", ""
}
return f.Function, fmt.Sprintf("%s:%d", f.File, f.Line)
},
})
default:
if conf.Conf.Log.LogFormat != "text" {
@ -75,6 +87,12 @@ func InitLog(ctx context.Context) (err error) {
FullTimestamp: true,
TimestampFormat: time.DateTime,
QuoteEmptyFields: true,
CallerPrettyfier: func(f *runtime.Frame) (function string, file string) {
if _, ok := logCallerIgnoreFuncs[f.Function]; ok {
return "", ""
}
return f.Function, fmt.Sprintf("%s:%d", f.File, f.Line)
},
})
}
log.SetOutput(logrus.StandardLogger().Writer())

@ -12,9 +12,9 @@ import (
func Init(e *gin.Engine) {
w := log.StandardLogger().Writer()
e.
Use(gin.LoggerWithWriter(w), gin.RecoveryWithWriter(w)).
Use(NewCors()).
Use(NewLog(log.StandardLogger()))
Use(NewLog(log.StandardLogger())).
Use(gin.RecoveryWithWriter(w)).
Use(NewCors())
if conf.Conf.RateLimit.Enable {
d, err := time.ParseDuration(conf.Conf.RateLimit.Period)
if err != nil {

@ -1,14 +1,69 @@
package middlewares
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
func NewLog(l *logrus.Logger) gin.HandlerFunc {
return func(ctx *gin.Context) {
ctx.Set("log", &logrus.Entry{
return func(c *gin.Context) {
c.Set("log", &logrus.Entry{
Logger: l,
})
start := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery
c.Next()
param := gin.LogFormatterParams{
Request: c.Request,
Keys: c.Keys,
}
// Stop timer
param.TimeStamp = time.Now()
param.Latency = param.TimeStamp.Sub(start)
param.ClientIP = c.ClientIP()
param.Method = c.Request.Method
param.StatusCode = c.Writer.Status()
param.ErrorMessage = c.Errors.ByType(gin.ErrorTypePrivate).String()
param.BodySize = c.Writer.Size()
if raw != "" {
path = path + "?" + raw
}
param.Path = path
c.MustGet("log").(*logrus.Entry).Info(formatter(param))
}
}
var formatter = func(param gin.LogFormatterParams) string {
var statusColor, methodColor, resetColor string
if param.IsOutputColor() {
statusColor = param.StatusCodeColor()
methodColor = param.MethodColor()
resetColor = param.ResetColor()
}
if param.Latency > time.Minute {
param.Latency = param.Latency.Truncate(time.Second)
}
return fmt.Sprintf("[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %#v\n%s",
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
statusColor, param.StatusCode, resetColor,
param.Latency,
param.ClientIP,
methodColor, param.Method, resetColor,
param.Path,
param.ErrorMessage,
)
}

Loading…
Cancel
Save