2025-05-08 23:03:08 +08:00

85 lines
2.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package common_structure
import (
"fmt"
"github.com/dromara/carbon/v2"
"go.uber.org/zap"
)
type LogType = string
const (
LogTypeOrder LogType = "order" // 订单
LogTypeProduct LogType = "product" // 商品
LogTypeMerchant LogType = "merchant" // 商家
LogTypeMerchantUser LogType = "merchant_user" // 商家用户
LogTypePay LogType = "pay" // 支付
LogTypeUser LogType = "user" // 用户
LogTypeGPS LogType = "gps" // gps
LogTypeSMS LogType = "sms" // 短信
LogTypeSystem LogType = "system" // 系统、日志、定时任务初始化、定时任务、mq初始化等
)
type LogInfo struct {
InfoType LogType `json:"info_type"` // 日志类型,按被操作对象划分
Operator *Operator `json:"operator,omitempty"` // 操作人,系统操作不用填写操作人,写日志类型就可以
ObjectorID int `json:"objector_id,omitempty"` // 被操作对象ID如果是gps短信等服务则为0
Title string `json:"title"` // 必填,标题,做什么事情
Param interface{} `json:"param,omitempty"` // 参数,请求参数,传指针
Result interface{} `json:"result,omitempty"` // 结果返回值传指针如果是报错传err
ErrStr string `json:"err_str,omitempty"` // 错误信息
At string `json:"at"` // 记录时间,不用传入,自动记录
}
type OperatorType = string
const (
AdminOperatorType OperatorType = "admin" // 管理员
MerchantOperatorType OperatorType = "merchant" // 商家
UserOperatorType OperatorType = "user" // 用户
)
type Operator struct {
ID int // 系统ID设置0
OperatorType OperatorType
}
func ZapDebug(logger *zap.Logger, log *LogInfo) {
handleLog(log)
logger.Debug("", zap.Any("log", log))
}
func ZapInfo(logger *zap.Logger, log *LogInfo) {
handleLog(log)
logger.Info("", zap.Any("log", log))
}
func ZapWarn(logger *zap.Logger, log *LogInfo) {
handleLog(log)
logger.Warn("", zap.Any("log", log))
}
func ZapError(logger *zap.Logger, log *LogInfo) {
handleLog(log)
logger.Error("", zap.Any("log", log))
}
func ZapPanic(logger *zap.Logger, log *LogInfo) {
handleLog(log)
logger.Panic("", zap.Any("log", log))
}
func ZapFatal(logger *zap.Logger, log *LogInfo) {
handleLog(log)
logger.Fatal("", 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
}
}