提交 8fc3104f authored 作者: mooncake9527's avatar mooncake9527

修改输出日志

上级 26f986a2
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity" "gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity"
"io" "io"
"net/http" "net/http"
"strings"
"time" "time"
ctxUtils "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils" ctxUtils "gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
...@@ -16,7 +17,8 @@ import ( ...@@ -16,7 +17,8 @@ import (
var ( var (
// Print body max length // Print body max length
defaultMaxLength = 300 defaultMaxLength = 300
defaultMediaMaxLength = 300
// default zap log // default zap log
defaultLogger, _ = zap.NewProduction() defaultLogger, _ = zap.NewProduction()
...@@ -37,19 +39,21 @@ type Option func(*options) ...@@ -37,19 +39,21 @@ type Option func(*options)
func defaultOptions() *options { func defaultOptions() *options {
return &options{ return &options{
maxLength: defaultMaxLength, maxLength: defaultMaxLength,
log: defaultLogger, mediaMaxLength: defaultMediaMaxLength,
ignoreRoutes: defaultIgnoreRoutes, log: defaultLogger,
traceIDFrom: 0, ignoreRoutes: defaultIgnoreRoutes,
traceIDFrom: 0,
} }
} }
type options struct { type options struct {
maxLength int maxLength int
appName string mediaMaxLength int
log *zap.Logger appName string
ignoreRoutes map[string]struct{} log *zap.Logger
traceIDFrom int // 0: ignore, 1: from context, 2: from header ignoreRoutes map[string]struct{}
traceIDFrom int // 0: ignore, 1: from context, 2: from header
} }
func (o *options) apply(opts ...Option) { func (o *options) apply(opts ...Option) {
...@@ -68,6 +72,15 @@ func WithMaxLen(maxLen int) Option { ...@@ -68,6 +72,15 @@ func WithMaxLen(maxLen int) Option {
} }
} }
func WithMediaMaxLen(maxLen int) Option {
return func(o *options) {
if maxLen < len(contentMark) {
panic("mediaMaxLength should be greater than or equal to 8")
}
o.mediaMaxLength = maxLen
}
}
// WithLog set log // WithLog set log
func WithLog(log *zap.Logger) Option { func WithLog(log *zap.Logger) Option {
return func(o *options) { return func(o *options) {
...@@ -220,6 +233,11 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -220,6 +233,11 @@ func Logging(opts ...Option) gin.HandlerFunc {
// processing requests // processing requests
c.Next() c.Next()
contentType := c.Writer.Header().Get("Content-Type")
isMedia := false
if strings.Contains(contentType, "image") {
isMedia = true
}
//print return message after processing //print return message after processing
fields = []zap.Field{ fields = []zap.Field{
zap.Int("code", c.Writer.Status()), zap.Int("code", c.Writer.Status()),
...@@ -229,7 +247,11 @@ func Logging(opts ...Option) gin.HandlerFunc { ...@@ -229,7 +247,11 @@ func Logging(opts ...Option) gin.HandlerFunc {
zap.String("cost", time.Since(start).String()), zap.String("cost", time.Since(start).String()),
zap.Int("size", newWriter.body.Len()), zap.Int("size", newWriter.body.Len()),
zap.Any("header", c.Writer.Header()), zap.Any("header", c.Writer.Header()),
zap.ByteString("body", getResponseBody(newWriter.body, o.maxLength)), }
if isMedia {
fields = append(fields, zap.ByteString("body", getResponseBody(newWriter.body, o.mediaMaxLength)))
} else {
fields = append(fields, zap.ByteString("body", getResponseBody(newWriter.body, o.maxLength)))
} }
if reqID != "" { if reqID != "" {
fields = append(fields, zap.String(ctxUtils.ContextTraceIDKey.String(), reqID)) fields = append(fields, zap.String(ctxUtils.ContextTraceIDKey.String(), reqID))
......
package xretry package retryUtils
import ( import (
"context" "context"
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论