Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
ffb9dd6112 | ||
![]() |
b8bd72806f | ||
![]() |
2b080ef98e | ||
![]() |
52c21d0814 |
7
go.mod
7
go.mod
@@ -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
|
||||||
|
go.uber.org/zap v1.27.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require go.uber.org/multierr v1.10.0 // indirect
|
||||||
|
11
go.sum
Normal file
11
go.sum
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
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/go.mod h1:Baj3A1uBBctJmpZWJd6/+WWnmIuY2pobR6IOpB6xigc=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
|
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=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
51
log.go
51
log.go
@@ -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
|
||||||
|
|
||||||
@@ -22,9 +27,9 @@ type LogInfo struct {
|
|||||||
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"` // 结果,返回值,传指针
|
Result interface{} `json:"result,omitempty"` // 结果,返回值,传指针,如果是报错,传err
|
||||||
ErrStr string `json:"err_str,omitempty"` // 错误信息,json转义error没有自动存储,需要手动转义
|
ErrStr string `json:"err_str,omitempty"` // 错误信息
|
||||||
At time.Time `json:"at"` // 记录时间
|
At string `json:"at"` // 记录时间,不用传入,自动记录
|
||||||
}
|
}
|
||||||
|
|
||||||
type OperatorType = string
|
type OperatorType = string
|
||||||
@@ -39,3 +44,41 @@ type Operator struct {
|
|||||||
ID int // 系统ID设置0
|
ID int // 系统ID设置0
|
||||||
OperatorType OperatorType
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user