Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
6bf054e1e8 | ||
![]() |
a48afa14b7 | ||
![]() |
3b335785a4 | ||
![]() |
d311a8368a | ||
![]() |
7b077e25b4 |
1
go.mod
1
go.mod
@@ -5,6 +5,7 @@ go 1.18.0
|
|||||||
require (
|
require (
|
||||||
github.com/dromara/carbon/v2 v2.6.4
|
github.com/dromara/carbon/v2 v2.6.4
|
||||||
go.uber.org/zap v1.27.0
|
go.uber.org/zap v1.27.0
|
||||||
|
github.com/gogf/gf/v2 v2.7.2
|
||||||
)
|
)
|
||||||
|
|
||||||
require go.uber.org/multierr v1.10.0 // indirect
|
require go.uber.org/multierr v1.10.0 // indirect
|
||||||
|
1
go.sum
1
go.sum
@@ -1,6 +1,7 @@
|
|||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/dromara/carbon/v2 v2.6.4 h1:cpIansyiEIEed3OlEIqo1IXj86qu0x6pf/E2keL2wYo=
|
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/dromara/carbon/v2 v2.6.4/go.mod h1:Baj3A1uBBctJmpZWJd6/+WWnmIuY2pobR6IOpB6xigc=
|
||||||
|
github.com/gogf/gf/v2 v2.7.2/go.mod h1:EBXneAg/wes86rfeh68XC0a2JBNQylmT7Sp6/8Axk88=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
|
@@ -1,9 +1,10 @@
|
|||||||
package common_structure
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/dromara/carbon/v2"
|
"github.com/dromara/carbon/v2"
|
||||||
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,26 +19,27 @@ const (
|
|||||||
LogTypeUser LogType = "user" // 用户
|
LogTypeUser LogType = "user" // 用户
|
||||||
LogTypeGPS LogType = "gps" // gps
|
LogTypeGPS LogType = "gps" // gps
|
||||||
LogTypeSMS LogType = "sms" // 短信
|
LogTypeSMS LogType = "sms" // 短信
|
||||||
|
LogTypeIP LogType = "ip" // ip信息
|
||||||
LogTypeSystem LogType = "system" // 系统、日志、定时任务初始化、定时任务、mq初始化等
|
LogTypeSystem LogType = "system" // 系统、日志、定时任务初始化、定时任务、mq初始化等
|
||||||
)
|
)
|
||||||
|
|
||||||
type LogInfo struct {
|
type LogInfo struct {
|
||||||
InfoType LogType `json:"info_type"` // 日志类型,按被操作对象划分
|
LogType LogType `json:"log_type"` // 日志类型,按被操作对象划分
|
||||||
Operator *Operator `json:"operator,omitempty"` // 操作人,系统操作不用填写操作人,写日志类型就可以
|
Operator *Operator `json:"operator,omitempty"` // 操作人,系统操作不用填写操作人,写日志类型就可以
|
||||||
ObjectorID int `json:"objector_id,omitempty"` // 被操作对象ID,如果是gps,短信等服务则为0
|
ObjectorID int `json:"objector_id,omitempty"` // 被操作对象ID,如果是gps,短信等服务则为0
|
||||||
Title string `json:"title"` // 必填,标题,做什么事情
|
Title string `json:"title"` // 必填,标题,做什么事情
|
||||||
Param interface{} `json:"param,omitempty"` // 参数,请求参数,传指针
|
Param interface{} `json:"param,omitempty"` // 参数,请求参数,传指针
|
||||||
Result interface{} `json:"result,omitempty"` // 结果,返回值,传指针,如果是报错,传err
|
Result interface{} `json:"result,omitempty"` // 结果,返回值,传指针,如果是报错,传err
|
||||||
ErrStr string `json:"err_str,omitempty"` // 错误信息
|
ErrStr string `json:"err_str,omitempty"` // 错误信息,不用传入,自动记录
|
||||||
At string `json:"at"` // 记录时间,不用传入,自动记录
|
At string `json:"at"` // 记录时间,不用传入,自动记录
|
||||||
}
|
}
|
||||||
|
|
||||||
type OperatorType = string
|
type OperatorType = string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AdminOperatorType OperatorType = "admin" // 管理员
|
OperatorTypeAdmin OperatorType = "admin" // 管理员
|
||||||
MerchantOperatorType OperatorType = "merchant" // 商家
|
OperatorTypeMerchant OperatorType = "merchant" // 商家
|
||||||
UserOperatorType OperatorType = "user" // 用户
|
OperatorTypeUser OperatorType = "user" // 用户
|
||||||
)
|
)
|
||||||
|
|
||||||
type Operator struct {
|
type Operator struct {
|
||||||
@@ -82,3 +84,53 @@ func handleLog(log *LogInfo) {
|
|||||||
log.Result = nil
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LogInfo) GFCommonHandelResult(ctx context.Context, result interface{}, err error) {
|
||||||
|
if err != nil {
|
||||||
|
l.Result = err
|
||||||
|
GFError(ctx, l)
|
||||||
|
} else {
|
||||||
|
l.Result = result
|
||||||
|
GFInfo(ctx, 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)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user