Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
5198625a
提交
5198625a
authored
6月 27, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
request cost
上级
fa27152e
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
9 行增加
和
24 行删除
+9
-24
logging.go
gin/middleware/logging.go
+8
-13
requstid.go
gin/middleware/requstid.go
+1
-11
没有找到文件。
gin/middleware/logging.go
浏览文件 @
5198625a
...
...
@@ -2,6 +2,7 @@ package middleware
import
(
"bytes"
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/gin/entity"
"io"
"net/http"
...
...
@@ -46,6 +47,7 @@ func defaultOptions() *options {
type
options
struct
{
maxLength
int
appName
string
log
*
zap
.
Logger
ignoreRoutes
map
[
string
]
struct
{}
traceIDFrom
int
// 0: ignore, 1: from context, 2: from header
...
...
@@ -75,6 +77,11 @@ func WithLog(log *zap.Logger) Option {
}
}
}
func
WithAppName
(
appName
string
)
Option
{
return
func
(
o
*
options
)
{
o
.
appName
=
appName
}
}
// WithIgnoreRoutes no logger content routes
func
WithIgnoreRoutes
(
routes
...
string
)
Option
{
...
...
@@ -191,14 +198,6 @@ func Logging(opts ...Option) gin.HandlerFunc {
copyHttpReq
.
Body
=
buf
.
String
()
c
.
Set
(
ctxUtil
.
KeyReqBody
,
copyHttpReq
)
//if c.Request.Method == http.MethodGet && buf.Len() == 0 {
// paramMap, err := uriUtils.URLValuePairsToMap(c.Request.URL.RawQuery)
// if err == nil {
// val, _ := json.Marshal(paramMap)
// c.Set(ctxUtil.KeyReqBody, val)
// }
//}
reqID
:=
""
if
o
.
traceIDFrom
==
1
{
if
v
,
isExist
:=
c
.
Get
(
ctxUtil
.
ContextTraceIDKey
);
isExist
{
...
...
@@ -225,7 +224,7 @@ func Logging(opts ...Option) gin.HandlerFunc {
// processing requests
c
.
Next
()
//c.Set(ctxUtil.KeyRspBody, newWriter.body.Bytes(
))
c
.
Writer
.
Header
()
.
Add
(
ctxUtil
.
KeyCost
,
fmt
.
Sprintf
(
"%s-%d"
,
o
.
appName
,
time
.
Since
(
start
)
.
Milliseconds
()
))
// print return message after processing
fields
=
[]
zap
.
Field
{
...
...
@@ -241,10 +240,6 @@ func Logging(opts ...Option) gin.HandlerFunc {
if
reqID
!=
""
{
fields
=
append
(
fields
,
zap
.
String
(
ctxUtil
.
ContextTraceIDKey
,
reqID
))
}
//copyHttpRsp := CopyHttpRsp{}
//copyHttpRsp.Headers = c.Writer.Header()
//copyHttpRsp.Body = newWriter.body.Bytes()
//c.Set(ctxUtil.KeyRspBody, jsonUtils.ToJsonBytes(copyHttpRsp))
o
.
log
.
Info
(
">>>>>>>>>rsp"
,
fields
...
)
}
...
...
gin/middleware/requstid.go
浏览文件 @
5198625a
...
...
@@ -2,7 +2,6 @@ package middleware
import
(
"context"
"fmt"
ctxUtil
"gitlab.wanzhuangkj.com/tush/xpkg/gin/ctxUtils"
"net/http"
"strconv"
...
...
@@ -52,11 +51,6 @@ func WithContextRequestIDKey(key string) RequestIDOption {
o
.
contextRequestIDKey
=
key
}
}
func
WithAppName
(
key
string
)
RequestIDOption
{
return
func
(
o
*
requestIDOptions
)
{
o
.
appName
=
key
}
}
// WithHeaderRequestIDKey set header request id key, minimum length of 4
func
WithHeaderRequestIDKey
(
key
string
)
RequestIDOption
{
...
...
@@ -84,9 +78,7 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc {
o
.
setRequestIDKey
()
return
func
(
c
*
gin
.
Context
)
{
// Check for incoming header, use it if exists
requestID
:=
c
.
Request
.
Header
.
Get
(
ctxUtil
.
HeaderXRequestIDKey
)
//costs := c.Request.Header.Values(ctxUtil.KeyCost)
// Create request id
if
requestID
==
""
{
...
...
@@ -99,11 +91,9 @@ func RequestID(opts ...RequestIDOption) gin.HandlerFunc {
// Set X-Request-Id header
c
.
Writer
.
Header
()
.
Set
(
ctxUtil
.
HeaderXRequestIDKey
,
requestID
)
startTime
:=
time
.
Now
()
c
.
Next
()
//costs = append(costs, fmt.Sprintf("%s-%d", o.appName, time.Since(startTime).Milliseconds()))
c
.
Writer
.
Header
()
.
Set
(
ctxUtil
.
HeaderXTimestampKey
,
strconv
.
FormatInt
(
time
.
Now
()
.
Unix
(),
10
))
c
.
Writer
.
Header
()
.
Add
(
ctxUtil
.
KeyCost
,
fmt
.
Sprintf
(
"%s-%d"
,
o
.
appName
,
time
.
Since
(
startTime
)
.
Milliseconds
()))
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论