159 lines
4.5 KiB
Go
159 lines
4.5 KiB
Go
package log
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"github.com/gogf/gf/v2/os/glog"
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
type LogType = string
|
||
|
||
const (
|
||
LogTypeOrder LogType = "order" // 订单
|
||
LogTypeProduct LogType = "product" // 商品
|
||
LogTypeMerchant LogType = "merchant" // 商家
|
||
LogTypeMerchantRedPack LogType = "merchant_red_pack" // 商家红包
|
||
LogTypePay LogType = "pay" // 支付
|
||
LogTypeUser LogType = "user" // 用户
|
||
LogTypeUserRedPack LogType = "user_red_pack" // 用户红包
|
||
LogTypeGPS LogType = "gps" // gps
|
||
LogTypeSMS LogType = "sms" // 短信
|
||
LogTypeIP LogType = "ip" // ip信息
|
||
LogTypeAdmin LogType = "admin" // 管理员操作,例如登录、退出、修改密码、修改用户信息、修改角色信息、审核等
|
||
LogTypeSystem LogType = "system" // 系统、日志、定时任务初始化、定时任务、mq初始化等
|
||
LogTypeSystemRedPack LogType = "system_red_pack" // 系统红包
|
||
LogTypeActivity LogType = "activity" // 活动
|
||
LogTypeJJT LogType = "jjt" // 交易所
|
||
)
|
||
|
||
type OperatorType = string
|
||
|
||
const (
|
||
OperatorTypeAdmin OperatorType = "admin" // 管理员
|
||
OperatorTypeMerchant OperatorType = "merchant" // 商家
|
||
OperatorTypeUser OperatorType = "user" // 用户
|
||
)
|
||
|
||
type Operator struct {
|
||
ID int `json:"id"` // 系统ID设置0
|
||
OperatorType OperatorType `json:"operator_type"`
|
||
}
|
||
|
||
type LogInfo struct {
|
||
LogType LogType `json:"log_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
|
||
}
|
||
|
||
type TempGLog struct {
|
||
Time string `json:"Time"`
|
||
TraceId string `json:"TraceId"`
|
||
Level string `json:"Level"`
|
||
Content string `json:"Content"`
|
||
}
|
||
|
||
type GLog struct {
|
||
Time string `json:"Time"`
|
||
TraceId string `json:"TraceId"`
|
||
Level string `json:"Level"`
|
||
Content *LogInfo `json:"Content"`
|
||
}
|
||
|
||
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) {
|
||
// 处理 Param
|
||
log.Param = processValue(log.Param)
|
||
// 处理 Result
|
||
log.Result = processValue(log.Result)
|
||
}
|
||
|
||
func GFInfo(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||
HandleLog(log)
|
||
g.Info(ctx, log)
|
||
}
|
||
|
||
func GFWarning(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||
HandleLog(log)
|
||
g.Warning(ctx, log)
|
||
}
|
||
|
||
func GFError(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||
HandleLog(log)
|
||
g.Error(ctx, log)
|
||
}
|
||
|
||
func GFDebug(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||
HandleLog(log)
|
||
g.Debug(ctx, log)
|
||
}
|
||
|
||
func GFPanic(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||
HandleLog(log)
|
||
g.Panic(ctx, log)
|
||
}
|
||
|
||
func GFFatal(ctx context.Context, g *glog.Logger, log *LogInfo) {
|
||
HandleLog(log)
|
||
g.Fatal(ctx, log)
|
||
}
|
||
|
||
func (l *LogInfo) GFCommonHandelResult(ctx context.Context, g *glog.Logger, result interface{}, err error) {
|
||
if err != nil {
|
||
l.Result = err
|
||
GFError(ctx, g, l)
|
||
} else {
|
||
l.Result = result
|
||
GFInfo(ctx, g, l)
|
||
}
|
||
}
|
||
|
||
func (l *LogInfo) ZapCommonHandelResult(logger *zap.Logger, result interface{}, err error) {
|
||
if err != nil {
|
||
l.Result = err
|
||
ZapError(logger, l)
|
||
} else {
|
||
l.Result = result
|
||
ZapInfo(logger, l)
|
||
}
|
||
}
|
||
|
||
func processValue(value interface{}) interface{} {
|
||
if value == nil {
|
||
return ""
|
||
}
|
||
return fmt.Sprintf("%+v", value)
|
||
}
|