Compare commits

..

3 Commits

Author SHA1 Message Date
yuguojian
2b080ef98e 更新日志 2025-05-08 18:07:25 +08:00
yuguojian
52c21d0814 更新日志 2025-05-08 18:07:13 +08:00
yuguojian
1915dc0a2a 更新日志 2025-05-08 16:40:03 +08:00
3 changed files with 52 additions and 10 deletions

7
go.mod
View File

@@ -1,3 +1,10 @@
module git.ssgfgtfy.com/public/common_structure module git.ssgfgtfy.com/public/common_structure
go 1.18.0 go 1.18.0
require (
github.com/dromara/carbon/v2 v2.6.4 // indirect
github.com/golang-module/carbon/v2 v2.5.9 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.27.0 // indirect
)

8
go.sum Normal file
View File

@@ -0,0 +1,8 @@
github.com/dromara/carbon/v2 v2.6.4 h1:cpIansyiEIEed3OlEIqo1IXj86qu0x6pf/E2keL2wYo=
github.com/dromara/carbon/v2 v2.6.4/go.mod h1:Baj3A1uBBctJmpZWJd6/+WWnmIuY2pobR6IOpB6xigc=
github.com/golang-module/carbon/v2 v2.5.9 h1:QKotfr6/Fkk5RGIPyEfItaR5guF37QH2YyKCbn1EFxM=
github.com/golang-module/carbon/v2 v2.5.9/go.mod h1:yAYXIP0OIq7ykDosUWpZpEyTGuvBXnSjGMQ/EaNagYQ=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=

47
log.go
View File

@@ -1,6 +1,11 @@
package common_structure package common_structure
import "time" import (
"fmt"
"github.com/dromara/carbon/v2"
"go.uber.org/zap"
)
type LogType = string type LogType = string
@@ -17,20 +22,19 @@ const (
) )
type LogInfo struct { type LogInfo struct {
InfoType LogType `json:"info_type"` // 日志类型,按被操作对象划分 InfoType LogType `json:"info_type"` // 日志类型,按被操作对象划分
Operator Operator `json:"operator"` // 操作人 Operator *Operator `json:"operator,omitempty"` // 操作人,系统操作不用填写操作人,写日志类型就可以
ObjectorID int `json:"objector_id"` // 被操作对象ID如果是gps短信等服务则为0 ObjectorID int `json:"objector_id,omitempty"` // 被操作对象ID如果是gps短信等服务则为0
Title string `json:"title"` // 标题,做什么事情 Title string `json:"title"` // 必填,标题,做什么事情
Param interface{} `json:"param"` // 参数,请求参数,函数、方法就是参数 Param interface{} `json:"param,omitempty"` // 参数,请求参数,传指针
Result interface{} `json:"result"` // 结果,返回值 Result interface{} `json:"result,omitempty"` // 结果,返回值传指针如果是报错传err
ErrStr string `json:"err_str"` // 错误信息json转义error没有自动存储需要手动转义 ErrStr string `json:"err_str,omitempty"` // 错误信息
At time.Time `json:"at"` // 记录时间 At string `json:"at"` // 记录时间,不用传入,自动记录
} }
type OperatorType = string type OperatorType = string
const ( const (
SystemOperatorType OperatorType = "system" // 系统
AdminOperatorType OperatorType = "admin" // 管理员 AdminOperatorType OperatorType = "admin" // 管理员
MerchantOperatorType OperatorType = "merchant" // 商家 MerchantOperatorType OperatorType = "merchant" // 商家
UserOperatorType OperatorType = "user" // 用户 UserOperatorType OperatorType = "user" // 用户
@@ -40,3 +44,26 @@ type Operator struct {
ID int // 系统ID设置0 ID int // 系统ID设置0
OperatorType OperatorType OperatorType OperatorType
} }
func ZapPanic(logger *zap.Logger, log *LogInfo) {
handleLog(log)
logger.Panic("", zap.Any("log", log))
}
func ZapInfo(logger *zap.Logger, log *LogInfo) {
handleLog(log)
logger.Info("", zap.Any("log", log))
}
func ZapError(logger *zap.Logger, log *LogInfo) {
handleLog(log)
logger.Error("", zap.Any("log", log))
}
func handleLog(log *LogInfo) {
log.At = carbon.Now().ToDateTimeString()
if _, ok := log.Result.(error); ok {
log.ErrStr = fmt.Sprintf("%+v", log.Result)
log.Result = nil
}
}