Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
60456184
提交
60456184
authored
10月 16, 2025
作者:
mooncake
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update weblog
上级
783b3c2e
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
54 行增加
和
26 行删除
+54
-26
ctxUtils.go
utils/ctxUtils/ctxUtils.go
+41
-0
webLogUtils.go
utils/webLogUtils/webLogUtils.go
+11
-26
weblog.sql
utils/webLogUtils/weblog.sql
+2
-0
没有找到文件。
utils/ctxUtils/ctxUtils.go
浏览文件 @
60456184
...
...
@@ -31,6 +31,7 @@ const (
KeyUID
=
"uid"
KeyUType
=
"uType"
KeyCompanyID
=
"companyID"
KeyCompanyName
=
"companyName"
KeyShopID
=
"shopID"
KeyUName
=
"uname"
KeyToken
=
"token"
...
...
@@ -212,6 +213,26 @@ func GetGinUID(c context.Context) xsf.ID {
return
xsf
.
ID
(
0
)
}
func
GetGinUName
(
c
context
.
Context
)
string
{
if
val
:=
c
.
Value
(
KeyUName
);
val
!=
nil
{
if
v
,
ok
:=
val
.
(
string
);
ok
{
return
v
}
return
cast
.
ToString
(
val
)
}
return
""
}
func
GetGinCompanyName
(
c
context
.
Context
)
string
{
if
val
:=
c
.
Value
(
KeyCompanyName
);
val
!=
nil
{
if
v
,
ok
:=
val
.
(
string
);
ok
{
return
v
}
return
cast
.
ToString
(
val
)
}
return
""
}
func
GetCtxUID
(
c
context
.
Context
)
xsf
.
ID
{
if
val
:=
c
.
Value
(
KeyUID
);
val
!=
nil
{
if
v
,
ok
:=
val
.
(
xsf
.
ID
);
ok
{
...
...
@@ -223,6 +244,26 @@ func GetCtxUID(c context.Context) xsf.ID {
return
xsf
.
ID
(
0
)
}
func
GetCtxCompanyName
(
c
context
.
Context
)
string
{
if
val
:=
c
.
Value
(
KeyCompanyName
);
val
!=
nil
{
if
v
,
ok
:=
val
.
(
string
);
ok
{
return
v
}
return
cast
.
ToString
(
val
)
}
return
""
}
func
GetCtxUName
(
c
context
.
Context
)
string
{
if
val
:=
c
.
Value
(
KeyUName
);
val
!=
nil
{
if
v
,
ok
:=
val
.
(
string
);
ok
{
return
v
}
return
cast
.
ToString
(
val
)
}
return
""
}
func
GetGinUType
(
c
*
gin
.
Context
)
int
{
if
val
:=
c
.
Value
(
KeyUType
);
val
!=
nil
{
if
v
,
ok
:=
val
.
(
int
);
ok
{
...
...
utils/webLogUtils/webLogUtils.go
浏览文件 @
60456184
...
...
@@ -3,7 +3,6 @@ package webLogUtils
import
(
"container/list"
"context"
"fmt"
"strings"
"time"
...
...
@@ -56,18 +55,15 @@ func WebLogInterceptor() func(c *gin.Context) {
ctx
:=
ctxUtils
.
WrapCtx
(
c
)
xApi
,
ok
:=
api
.
XApi
.
M
[
URL
]
if
ok
{
companyID
:=
ctxUtils
.
GetCtxCompanyID
(
c
)
uid
:=
ctxUtils
.
GetGinUID
(
c
)
uname
:=
ctxUtils
.
GetGinUserName
(
c
)
uType
:=
getUserType
(
c
)
req
=
entity
.
GetCopyReq
(
c
)
if
uid
>
0
{
defer
func
()
{
AddAsync
(
ctx
,
""
,
fmt
.
Sprintf
(
"%d-%d-%s %s"
,
companyID
,
uType
,
uname
,
xApi
.
Summary
)
,
req
,
rsp
)
AddAsync
(
ctx
,
xApi
.
Summary
,
req
,
rsp
)
}()
}
else
{
defer
func
()
{
AddAsync
(
ctx
,
""
,
xApi
.
Summary
,
req
,
rsp
)
AddAsync
(
ctx
,
xApi
.
Summary
,
req
,
rsp
)
}()
}
}
...
...
@@ -78,21 +74,6 @@ func WebLogInterceptor() func(c *gin.Context) {
}
}
func
GetUserID
(
ctx
context
.
Context
)
xsf
.
ID
{
val
:=
ctx
.
Value
(
ctxUtils
.
KeyUID
)
if
val
!=
nil
{
if
id
,
ok
:=
val
.
(
xsf
.
ID
);
ok
{
return
id
}
if
v
,
ok
:=
val
.
(
string
);
ok
{
vid
,
_
:=
xsf
.
ParseString
(
v
)
return
vid
}
return
xsf
.
ParseInt64
(
cast
.
ToInt64
(
val
))
}
return
xsf
.
ParseInt64
(
0
)
}
var
merger
*
merge
.
FanIn
func
Init
()
{
...
...
@@ -153,7 +134,7 @@ func Init() {
if
err
:=
retryUtils
.
Retry
(
insertFn
,
retryUtils
.
RetryTimes
(
10
),
retryUtils
.
RetryWithLinearBackoff
(
32
*
time
.
Minute
));
err
!=
nil
{
logger
.
Error
(
"[webLog] insert fail"
,
logger
.
String
(
"err"
,
err
.
Error
()),
ctxUtils
.
CtxTraceIDField
(
ctx
))
for
_
,
or
:=
range
ors
{
logger
.
Error
(
"[webLog] insert fail detail"
,
logger
.
String
(
"user_id"
,
or
.
U
ser
ID
.
String
()),
logger
.
String
(
"operate"
,
or
.
Operate
),
ctxUtils
.
CtxTraceIDField
(
ctx
))
logger
.
Error
(
"[webLog] insert fail detail"
,
logger
.
String
(
"user_id"
,
or
.
UID
.
String
()),
logger
.
String
(
"operate"
,
or
.
Operate
),
ctxUtils
.
CtxTraceIDField
(
ctx
))
}
}
if
debug
{
...
...
@@ -168,11 +149,13 @@ func Init() {
webLogDaoInstance
.
db
=
database
.
DB
(
schema
)
}
func
AddAsync
(
ctx
context
.
Context
,
traceNo
,
operate
string
,
req
*
entity
.
CopyHttpReq
,
rsp
*
entity
.
CopyHttpRsp
)
{
func
AddAsync
(
ctx
context
.
Context
,
operate
string
,
req
*
entity
.
CopyHttpReq
,
rsp
*
entity
.
CopyHttpRsp
)
{
wl
:=
&
WebLog
{}
wl
.
ID
=
xsf
.
GenerateID
()
wl
.
CompanyName
=
ctxUtils
.
GetGinCompanyName
(
ctx
)
wl
.
CompanyID
=
ctxUtils
.
GetCtxCompanyID
(
ctx
)
wl
.
UserID
=
ctxUtils
.
GetCtxUID
(
ctx
)
wl
.
UID
=
ctxUtils
.
GetCtxUID
(
ctx
)
wl
.
UName
=
ctxUtils
.
GetCtxUName
(
ctx
)
wl
.
TraceID
=
ctxUtils
.
GetCtxTid
(
ctx
)
wl
.
Operate
=
operate
wl
.
CreatedAt
=
xtime
.
Now
()
...
...
@@ -211,8 +194,10 @@ func getUserType(c *gin.Context) int {
type
WebLog
struct
{
ID
xsf
.
ID
`json:"id" form:"id" swaggertype:"string" gorm:"column:id;type:bigint(20) unsigned;primaryKey;autoIncrement;comment:主键" example:"1000"`
//主键
CompanyID
xsf
.
ID
`json:"companyID" form:"companyID" swaggertype:"string" gorm:"column:company_id;type:bigint(20) unsigned;comment:商户ID" example:"1008"`
//商户ID
UserID
xsf
.
ID
`json:"userID" form:"userID" swaggertype:"string" gorm:"column:user_id;type:bigint(20) unsigned;comment:用户ID" example:"2008"`
//用户ID
CompanyID
xsf
.
ID
`json:"companyID" form:"companyID" swaggertype:"string" gorm:"column:company_id;type:bigint(20) unsigned;comment:公司ID" example:"1008"`
//公司ID
UID
xsf
.
ID
`json:"uID" form:"uID" swaggertype:"string" gorm:"column:u_id;type:bigint(20) unsigned;comment:用户ID" example:"2008"`
//用户ID
CompanyName
string
`json:"companyName" form:"companyName" gorm:"column:company_name;type:varchar(128);comment:公司名称" example:"tomcat"`
//公司名称
UName
string
`json:"uName" form:"uName" gorm:"column:u_name;type:varchar(128);comment:名称" example:"tomcat"`
//名称
Operate
string
`json:"operate" form:"operate" gorm:"column:operate;type:varchar(128);comment:操作" example:"用户登出"`
//操作
TraceID
string
`json:"traceID" form:"traceID" gorm:"column:trace_id;type:varchar(64);comment:溯源id" example:"TRACE_ID12345"`
//溯源id
Req
*
string
`json:"req" form:"req" gorm:"column:req;type:json;comment:请求" example:""`
//请求
...
...
utils/webLogUtils/weblog.sql
浏览文件 @
60456184
...
...
@@ -4,6 +4,8 @@ CREATE TABLE `web_log` (
`id`
bigint
(
20
)
unsigned
NOT
NULL
AUTO_INCREMENT
COMMENT
'ID eg[10001]'
,
`company_id`
bigint
(
20
)
DEFAULT
NULL
COMMENT
'公司ID eg[20001]'
,
`user_id`
bigint
(
20
)
DEFAULT
NULL
COMMENT
'用户ID eg[30001]'
,
`company_name`
varchar
(
32
)
COLLATE
utf8mb4_unicode_ci
DEFAULT
NULL
DEFAULT
''
COMMENT
'公司名称 eg[万桩]'
,
`u_name`
varchar
(
32
)
COLLATE
utf8mb4_unicode_ci
DEFAULT
NULL
DEFAULT
''
COMMENT
'名称 eg[tomcat]'
,
`operate`
varchar
(
32
)
COLLATE
utf8mb4_unicode_ci
DEFAULT
NULL
DEFAULT
''
COMMENT
'操作 eg[用户登出]'
,
`trace_id`
varchar
(
64
)
COLLATE
utf8mb4_unicode_ci
DEFAULT
NULL
DEFAULT
''
COMMENT
'溯源id eg[TRACE_ID12345]'
,
`req`
json
DEFAULT
NULL
COMMENT
'请求 eg[]'
,
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论