Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
476975d0
提交
476975d0
authored
7月 16, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
打印请求响应报文时,可以限制输出日志的长度
上级
8fc3104f
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
69 行增加
和
2 行删除
+69
-2
ctx.go
gin/ctxUtils/ctx.go
+7
-0
logging.go
gin/middleware/logging.go
+6
-1
contentType.go
httpcli/httpContentType/contentType.go
+15
-0
httpcli.go
httpcli/httpcli.go
+41
-1
没有找到文件。
gin/ctxUtils/ctx.go
浏览文件 @
476975d0
...
...
@@ -40,6 +40,7 @@ const (
KeyUName
CtxKey
=
"uname"
KeyToken
CtxKey
=
"token"
KeyCost
CtxKey
=
"X-Request-Cost"
KeyRspBodyMax
CtxKey
=
"rsp-body-max"
)
var
(
...
...
@@ -55,6 +56,12 @@ func Set(c *gin.Context, key CtxKey, val any) {
c
.
Set
(
key
.
String
(),
val
)
}
func
SetRspBodyMax
(
ctx
context
.
Context
,
max
int
)
{
if
c
,
_
:=
ToGinCtx
(
ctx
);
c
!=
nil
{
Set
(
c
,
KeyRspBodyMax
,
max
)
}
}
func
Get
[
T
any
](
c
*
gin
.
Context
,
key
CtxKey
)
(
T
,
bool
)
{
if
val
,
ok
:=
c
.
Get
(
key
.
String
());
ok
{
if
v
,
ok1
:=
val
.
(
T
);
ok1
{
...
...
gin/middleware/logging.go
浏览文件 @
476975d0
...
...
@@ -233,6 +233,7 @@ func Logging(opts ...Option) gin.HandlerFunc {
// processing requests
c
.
Next
()
rspBodyMax
:=
c
.
GetInt
(
ctxUtils
.
KeyRspBodyMax
.
String
())
contentType
:=
c
.
Writer
.
Header
()
.
Get
(
"Content-Type"
)
isMedia
:=
false
if
strings
.
Contains
(
contentType
,
"image"
)
{
...
...
@@ -251,7 +252,11 @@ func Logging(opts ...Option) gin.HandlerFunc {
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
rspBodyMax
>
0
{
fields
=
append
(
fields
,
zap
.
ByteString
(
"body"
,
getResponseBody
(
newWriter
.
body
,
rspBodyMax
)))
}
else
{
fields
=
append
(
fields
,
zap
.
ByteString
(
"body"
,
getResponseBody
(
newWriter
.
body
,
o
.
maxLength
)))
}
}
if
reqID
!=
""
{
fields
=
append
(
fields
,
zap
.
String
(
ctxUtils
.
ContextTraceIDKey
.
String
(),
reqID
))
...
...
httpcli/httpContentType/contentType.go
0 → 100644
浏览文件 @
476975d0
package
httpContentType
import
(
"net/http"
"strings"
)
func
IsMedia
(
header
http
.
Header
)
bool
{
ct
:=
header
.
Get
(
"Content-Type"
)
isMedia
:=
false
if
strings
.
Contains
(
ct
,
"image"
)
{
isMedia
=
true
}
return
isMedia
}
httpcli/httpcli.go
浏览文件 @
476975d0
...
...
@@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/entity"
"gitlab.wanzhuangkj.com/tush/xpkg/httpcli/httpContentType"
"io"
"net/http"
"net/url"
...
...
@@ -297,15 +298,54 @@ func (x *Request) send(ctx context.Context) {
x
.
pushDo
(
ctx
)
}
body
:=
x
.
RespBodyString
()
if
httpContentType
.
IsMedia
(
x
.
response
.
Header
)
{
body
=
string
(
getResponseBody
(
x
.
Response
()
.
BodyBuf
(),
16
))
}
logger
.
Info
(
"[httpCli] rsp"
,
logger
.
String
(
"cost"
,
time
.
Since
(
st
)
.
String
()),
logger
.
Int
(
"statusCode"
,
x
.
response
.
StatusCode
),
logger
.
String
(
"status"
,
x
.
response
.
Status
),
logger
.
Any
(
"header"
,
x
.
Response
()
.
Header
),
logger
.
String
(
"body"
,
x
.
RespBodyString
()
),
logger
.
String
(
"body"
,
body
),
ctxUtils
.
CtxTraceIDField
(
ctx
))
}
// If there is sensitive information in the body, you can use WithIgnoreRoutes set the route to ignore logging
func
getResponseBody
(
buf
*
bytes
.
Buffer
,
maxLen
int
)
[]
byte
{
l
:=
buf
.
Len
()
if
l
==
0
{
return
[]
byte
(
""
)
}
else
if
l
>
maxLen
{
l
=
maxLen
}
body
:=
make
([]
byte
,
l
)
n
,
_
:=
buf
.
Read
(
body
)
if
n
==
0
{
return
emptyBody
}
else
if
n
<
maxLen
{
if
isLastByteNewline
(
body
)
{
return
body
[
:
n
-
1
]
}
return
body
[
:
n
]
}
return
append
(
body
[
:
maxLen
-
len
(
contentMark
)],
contentMark
...
)
}
var
(
emptyBody
=
[]
byte
(
""
)
contentMark
=
[]
byte
(
" ...... "
)
)
func
isLastByteNewline
(
data
[]
byte
)
bool
{
if
len
(
data
)
==
0
{
return
false
}
return
data
[
len
(
data
)
-
1
]
==
'\n'
}
func
(
x
*
Request
)
pushDo
(
ctx
context
.
Context
)
{
client
:=
x
.
cli
if
client
==
nil
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论