提交 60456184 authored 作者: mooncake's avatar mooncake

update weblog

上级 783b3c2e
......@@ -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 {
......
......@@ -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.UserID.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:""` //请求
......
......@@ -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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论