增加日志方法
This commit is contained in:
116
log/log.go
Normal file
116
log/log.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/dromara/carbon/v2"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"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" // 短信
|
||||
LogTypeIP LogType = "ip" // ip信息
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func GFInfo(ctx context.Context, log *LogInfo) {
|
||||
handleLog(log)
|
||||
g.Log().Info(ctx, log)
|
||||
}
|
||||
|
||||
func GFWarning(ctx context.Context, log *LogInfo) {
|
||||
handleLog(log)
|
||||
g.Log().Warning(ctx, log)
|
||||
}
|
||||
|
||||
func GFError(ctx context.Context, log *LogInfo) {
|
||||
handleLog(log)
|
||||
g.Log().Error(ctx, log)
|
||||
}
|
||||
|
||||
func GFDebug(ctx context.Context, log *LogInfo) {
|
||||
handleLog(log)
|
||||
g.Log().Debug(ctx, log)
|
||||
}
|
||||
|
||||
func GFPanic(ctx context.Context, log *LogInfo) {
|
||||
handleLog(log)
|
||||
g.Log().Panic(ctx, log)
|
||||
}
|
||||
|
||||
func GFFatal(ctx context.Context, log *LogInfo) {
|
||||
handleLog(log)
|
||||
g.Log().Fatal(ctx, log)
|
||||
}
|
Reference in New Issue
Block a user