master v1.0.91
李光春 2 years ago
parent a7e10e9661
commit 699b1a6e5f

@ -4,7 +4,6 @@ import (
"context"
"errors"
"go.dtapp.net/dorm"
"go.dtapp.net/goip"
"go.dtapp.net/gorequest"
)
@ -16,7 +15,6 @@ type ApiClient struct {
gormClient *dorm.GormClient // 数据库驱动
mongoClient *dorm.MongoClient // 数据库驱动
zapLog *ZapLog // 日志服务
logDebug bool // 日志开关
config struct {
systemHostName string // 主机名
systemInsideIp string // 内网ip
@ -41,7 +39,6 @@ type ApiClient struct {
type ApiClientConfig struct {
GormClientFun dorm.GormClientTableFun // 日志配置
MongoClientFun dorm.MongoClientCollectionFun // 日志配置
Debug bool // 日志开关
ZapLog *ZapLog // 日志服务
CurrentIp string // 当前ip
}
@ -55,11 +52,6 @@ func NewApiClient(config *ApiClientConfig) (*ApiClient, error) {
c.zapLog = config.ZapLog
c.logDebug = config.Debug
if config.CurrentIp == "" {
config.CurrentIp = goip.GetOutsideIp(ctx)
}
if config.CurrentIp != "" && config.CurrentIp != "0.0.0.0" {
c.config.systemOutsideIp = config.CurrentIp
}

@ -87,25 +87,15 @@ func (c *ApiClient) gormMiddleware(ctx context.Context, request gorequest.Respon
ResponseTime: request.ResponseTime, //【返回】时间
SdkVersion: sdkVersion, //【程序】Sdk版本
}
if request.HeaderIsImg() {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.gormMiddleware.isimg]%s%s", data.RequestUri, request.ResponseHeader.Get("Content-Type"))
} else {
if !request.HeaderIsImg() {
if len(request.ResponseBody) > 0 {
data.ResponseBody = dorm.JsonEncodeNoError(dorm.JsonDecodeNoError(request.ResponseBody)) //【返回】数据
} else {
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.gormMiddleware.len]%s%s", data.RequestUri, request.ResponseBody)
}
}
}
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.gormMiddleware.data]%+v", data)
}
err := c.gormRecord(ctx, data)
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[golog.api.gormMiddleware]%s", err.Error())
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
}
}
@ -125,25 +115,15 @@ func (c *ApiClient) gormMiddlewareXml(ctx context.Context, request gorequest.Res
ResponseTime: request.ResponseTime, //【返回】时间
SdkVersion: sdkVersion, //【程序】Sdk版本
}
if request.HeaderIsImg() {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.gormMiddlewareXml.isimg]%s%s", data.RequestUri, request.ResponseHeader.Get("Content-Type"))
} else {
if !request.HeaderIsImg() {
if len(request.ResponseBody) > 0 {
data.ResponseBody = dorm.JsonEncodeNoError(request.ResponseBody) //【返回】内容
} else {
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.gormMiddlewareXml.len]%s%s", data.RequestUri, request.ResponseBody)
}
}
}
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.gormMiddlewareXml.data]%+v", data)
}
err := c.gormRecord(ctx, data)
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[golog.api.gormMiddlewareXml]%s", err.Error())
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
}
}
@ -163,24 +143,14 @@ func (c *ApiClient) gormMiddlewareCustom(ctx context.Context, api string, reques
ResponseTime: request.ResponseTime, //【返回】时间
SdkVersion: sdkVersion, //【程序】Sdk版本
}
if request.HeaderIsImg() {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.gormMiddlewareCustom.isimg]%s%s", data.RequestUri, request.ResponseHeader.Get("Content-Type"))
} else {
if !request.HeaderIsImg() {
if len(request.ResponseBody) > 0 {
data.ResponseBody = dorm.JsonEncodeNoError(dorm.JsonDecodeNoError(request.ResponseBody)) //【返回】数据
} else {
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.gormMiddlewareCustom.len]%s%s", data.RequestUri, request.ResponseBody)
}
}
}
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.gormMiddlewareCustom.data]%+v", data)
}
err := c.gormRecord(ctx, data)
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[golog.api.gormMiddlewareCustom]%s", err.Error())
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
}
}

@ -103,25 +103,15 @@ func (c *ApiClient) mongoMiddleware(ctx context.Context, request gorequest.Respo
ResponseTime: dorm.NewBsonTimeFromTime(request.ResponseTime), //【返回】时间
SdkVersion: sdkVersion, //【程序】Sdk版本
}
if request.HeaderIsImg() {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.mongoMiddleware.type]%s %s", data.RequestUri, request.ResponseHeader.Get("Content-Type"))
} else {
if !request.HeaderIsImg() {
if len(request.ResponseBody) > 0 {
data.ResponseBody = dorm.JsonDecodeNoError(request.ResponseBody) //【返回】内容
} else {
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.mongoMiddleware.len]%s %s", data.RequestUri, request.ResponseBody)
}
}
}
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.mongoMiddleware.data]%+v", data)
}
err := c.mongoRecord(ctx, data)
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[golog.api.mongoMiddleware]%s", err.Error())
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
}
}
@ -142,25 +132,15 @@ func (c *ApiClient) mongoMiddlewareXml(ctx context.Context, request gorequest.Re
ResponseTime: dorm.NewBsonTimeFromTime(request.ResponseTime), //【返回】时间
SdkVersion: sdkVersion, //【程序】Sdk版本
}
if request.HeaderIsImg() {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.mongoMiddlewareXml.type]%s %s", data.RequestUri, request.ResponseHeader.Get("Content-Type"))
} else {
if !request.HeaderIsImg() {
if len(request.ResponseBody) > 0 {
data.ResponseBody = dorm.XmlDecodeNoError(request.ResponseBody) //【返回】内容
} else {
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.mongoMiddlewareXml]%s %s", data.RequestUri, request.ResponseBody)
}
}
}
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.mongoMiddlewareXml.data]%+v", data)
}
err := c.mongoRecord(ctx, data)
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[golog.api.mongoMiddlewareXml]%s", err.Error())
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
}
}
@ -181,24 +161,14 @@ func (c *ApiClient) mongoMiddlewareCustom(ctx context.Context, api string, reque
ResponseTime: dorm.NewBsonTimeFromTime(request.ResponseTime), //【返回】时间
SdkVersion: sdkVersion, //【程序】Sdk版本
}
if request.HeaderIsImg() {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.mongoMiddlewareCustom.type]%s %s", data.RequestUri, request.ResponseHeader.Get("Content-Type"))
} else {
if !request.HeaderIsImg() {
if len(request.ResponseBody) > 0 {
data.ResponseBody = dorm.JsonDecodeNoError(request.ResponseBody) //【返回】内容
} else {
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.mongoMiddlewareCustom]%s %s", data.RequestUri, request.ResponseBody)
}
}
}
if c.logDebug {
c.zapLog.WithTraceId(ctx).Sugar().Infof("[golog.api.mongoMiddlewareCustom.data]%+v", data)
}
err := c.mongoRecord(ctx, data)
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[golog.api.mongoMiddlewareCustom]%s", err.Error())
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
}
}

@ -1,5 +1,5 @@
package golog
const (
Version = "1.0.90"
Version = "1.0.91"
)

@ -23,7 +23,6 @@ type GinClient struct {
mongoClient *dorm.MongoClient // 数据库驱动
ipService *goip.Client // ip服务
zapLog *ZapLog // 日志服务
logDebug bool // 日志开关
config struct {
systemHostName string // 主机名
systemInsideIp string // 内网ip
@ -49,7 +48,6 @@ type GinClientConfig struct {
IpService *goip.Client // ip服务
GormClientFun dorm.GormClientTableFun // 日志配置
MongoClientFun dorm.MongoClientCollectionFun // 日志配置
Debug bool // 日志开关
ZapLog *ZapLog // 日志服务
}
@ -62,8 +60,6 @@ func NewGinClient(config *GinClientConfig) (*GinClient, error) {
c.zapLog = config.ZapLog
c.logDebug = config.Debug
c.ipService = config.IpService
// 配置信息
@ -206,27 +202,15 @@ func (c *GinClient) Middleware() gin.HandlerFunc {
// 记录
if c.gormConfig.stats {
if dataJson {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.Middleware]准备使用{gormRecordJson}保存数据:%s", data)
}
c.gormRecordJson(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp, requestClientIpLocationLatitude, requestClientIpLocationLongitude)
} else {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.Middleware]准备使用{gormRecordXml}保存数据:%s", data)
}
c.gormRecordXml(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp, requestClientIpLocationLatitude, requestClientIpLocationLongitude)
}
}
if c.mongoConfig.stats {
if dataJson {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.Middleware]准备使用{mongoRecordJson}保存数据:%s", data)
}
c.mongoRecordJson(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp, requestClientIpLocationLatitude, requestClientIpLocationLongitude)
} else {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.Middleware]准备使用{mongoRecordXml}保存数据:%s", data)
}
c.mongoRecordXml(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp, requestClientIpLocationLatitude, requestClientIpLocationLongitude)
}
}

@ -71,10 +71,6 @@ func (c *GinClient) gormRecord(data ginPostgresqlLog) (err error) {
func (c *GinClient) gormRecordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp string, requestClientIpLocationLatitude, requestClientIpLocationLongitude float64) {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.gormRecordJson]收到保存数据要求:%s", c.gormConfig.tableName)
}
data := ginPostgresqlLog{
TraceId: traceId, //【系统】跟踪编号
RequestTime: requestTime, //【请求】时间
@ -90,8 +86,8 @@ func (c *GinClient) gormRecordJson(ginCtx *gin.Context, traceId string, requestT
RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份
RequestIpCity: requestClientIpCity, //【请求】请求客户端城市
RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商
RequestIpLatitude: requestClientIpLocationLatitude, // 【请求】请求客户端纬度
RequestIpLongitude: requestClientIpLocationLongitude, // 【请求】请求客户端经度
RequestIpLatitude: requestClientIpLocationLatitude, //【请求】请求客户端纬度
RequestIpLongitude: requestClientIpLocationLongitude, //【请求】请求客户端经度
RequestHeader: dorm.JsonEncodeNoError(ginCtx.Request.Header), //【请求】请求头
ResponseTime: gotime.Current().Time, //【返回】时间
ResponseCode: responseCode, //【返回】状态码
@ -106,30 +102,16 @@ func (c *GinClient) gormRecordJson(ginCtx *gin.Context, traceId string, requestT
if len(requestBody) > 0 {
data.RequestBody = dorm.JsonEncodeNoError(requestBody) //【请求】请求主体
} else {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.gormRecordJson.len]%s%s", data.RequestUri, requestBody)
}
}
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.gormRecordJson.data]%+v", data)
}
err := c.gormRecord(data)
if err != nil {
c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("[golog.gin.gormRecordJson]%s", err)
c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("[golog.gin.gormRecordJson.string]%s", requestBody)
c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("[golog.gin.gormRecordJson.JsonEncodeNoError.string]%s", dorm.JsonEncodeNoError(requestBody))
c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("保存失败:%s", err.Error())
}
}
func (c *GinClient) gormRecordXml(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, clientIp, requestClientIpCountry, requestClientIpProvince, requestClientIpCity, requestClientIpIsp string, requestClientIpLocationLatitude, requestClientIpLocationLongitude float64) {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.gormRecordXml]收到保存数据要求:%s", c.gormConfig.tableName)
}
data := ginPostgresqlLog{
TraceId: traceId, //【系统】跟踪编号
RequestTime: requestTime, //【请求】时间
@ -145,8 +127,8 @@ func (c *GinClient) gormRecordXml(ginCtx *gin.Context, traceId string, requestTi
RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份
RequestIpCity: requestClientIpCity, //【请求】请求客户端城市
RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商
RequestIpLatitude: requestClientIpLocationLatitude, // 【请求】请求客户端纬度
RequestIpLongitude: requestClientIpLocationLongitude, // 【请求】请求客户端经度
RequestIpLatitude: requestClientIpLocationLatitude, //【请求】请求客户端纬度
RequestIpLongitude: requestClientIpLocationLongitude, //【请求】请求客户端经度
RequestHeader: dorm.JsonEncodeNoError(ginCtx.Request.Header), //【请求】请求头
ResponseTime: gotime.Current().Time, //【返回】时间
ResponseCode: responseCode, //【返回】状态码
@ -161,22 +143,11 @@ func (c *GinClient) gormRecordXml(ginCtx *gin.Context, traceId string, requestTi
if len(requestBody) > 0 {
data.RequestBody = dorm.XmlEncodeNoError(dorm.XmlDecodeNoError(requestBody)) //【请求】请求内容
} else {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.gormRecordXml.len]%s%s", data.RequestUri, requestBody)
}
}
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.gormRecordXml.data]%+v", data)
}
err := c.gormRecord(data)
if err != nil {
c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("[golog.gin.gormRecordXml]%s", err)
c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("[golog.gin.gormRecordXml.string]%s", requestBody)
c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("[golog.gin.gormRecordXml.XmlDecodeNoError.string]%s", dorm.XmlDecodeNoError(requestBody))
c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("[golog.gin.gormRecordXml.XmlEncodeNoError.string]%s", dorm.XmlEncodeNoError(dorm.XmlDecodeNoError(requestBody)))
c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("保存失败:%s", err.Error())
}
}

@ -178,10 +178,6 @@ func (c *GinClient) mongoRecordJson(ginCtx *gin.Context, traceId string, request
var ctx = gotrace_id.SetGinTraceIdContext(context.Background(), ginCtx)
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.mongoRecordJson]收到保存数据要求:%s,%s", c.mongoConfig.databaseName, c.mongoConfig.collectionName)
}
data := ginMongoLog{
TraceId: traceId, //【记录】跟踪编号
LogTime: primitive.NewDateTimeFromTime(requestTime), //【记录】时间
@ -212,10 +208,6 @@ func (c *GinClient) mongoRecordJson(ginCtx *gin.Context, traceId string, request
if len(requestBody) > 0 {
data.RequestBody = dorm.JsonDecodeNoError(requestBody) //【请求】请求主体
} else {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.mongoRecordJson.len]%s%s", data.RequestUri, requestBody)
}
}
if requestClientIpLocationLatitude != 0 && requestClientIpLocationLongitude != 0 {
@ -225,13 +217,9 @@ func (c *GinClient) mongoRecordJson(ginCtx *gin.Context, traceId string, request
}
}
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.mongoRecordJson.data]%+v", data)
}
err := c.mongoRecord(ctx, data)
if err != nil {
c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("[golog.gin.mongoRecordJson]%s", err)
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
}
}
@ -239,10 +227,6 @@ func (c *GinClient) mongoRecordXml(ginCtx *gin.Context, traceId string, requestT
var ctx = gotrace_id.SetGinTraceIdContext(context.Background(), ginCtx)
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.mongoRecordXml]收到保存数据要求:%s,%s", c.mongoConfig.databaseName, c.mongoConfig.collectionName)
}
data := ginMongoLog{
TraceId: traceId, //【记录】跟踪编号
LogTime: primitive.NewDateTimeFromTime(requestTime), //【记录】时间
@ -273,10 +257,6 @@ func (c *GinClient) mongoRecordXml(ginCtx *gin.Context, traceId string, requestT
if len(requestBody) > 0 {
data.RequestBody = dorm.XmlDecodeNoError(requestBody) //【请求】请求主体
} else {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.mongoRecordXml.len]%s%s", data.RequestUri, requestBody)
}
}
if requestClientIpLocationLatitude != 0 && requestClientIpLocationLongitude != 0 {
@ -286,12 +266,8 @@ func (c *GinClient) mongoRecordXml(ginCtx *gin.Context, traceId string, requestT
}
}
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.mongoRecordXml.data]%+v", data)
}
err := c.mongoRecord(ctx, data)
if err != nil {
c.zapLog.WithTraceIdStr(traceId).Sugar().Errorf("[golog.gin.mongoRecordXml]%s", err)
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存失败:%s", err.Error())
}
}

@ -6,7 +6,7 @@ require (
github.com/gin-gonic/gin v1.8.1
github.com/natefinch/lumberjack v2.0.0+incompatible
go.dtapp.net/dorm v1.0.42
go.dtapp.net/goip v1.0.36
go.dtapp.net/goip v1.0.38
go.dtapp.net/gorequest v1.0.31
go.dtapp.net/gotime v1.0.5
go.dtapp.net/gotrace_id v1.0.6

@ -497,8 +497,8 @@ github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxt
github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0=
go.dtapp.net/dorm v1.0.42 h1:bugzTYBr5goLDf9s8vft6xG/fAiinLm1jo+9uFD7JRM=
go.dtapp.net/dorm v1.0.42/go.mod h1:LCy6tqg7uClOhMb8zgF9x9mTFoVd9Ud40g9a2Z6bbwM=
go.dtapp.net/goip v1.0.36 h1:+wexFCMnP3f+6jPYXjBLMyjnP+DfQrslWvXifndxkdc=
go.dtapp.net/goip v1.0.36/go.mod h1:9/Oo1HVM4EVUsvAebdV6CaBAK4S6qQMQWT3LcJfH6jM=
go.dtapp.net/goip v1.0.38 h1:WHIqXV0qWUM9XDtRaMIMyCKWyd9dWfSvSRdDr7vF7xU=
go.dtapp.net/goip v1.0.38/go.mod h1:N2YFFr2OO+5VQwMqyKtg7c4MVrDJOoog/QmIvYUfi1c=
go.dtapp.net/gorandom v1.0.1 h1:IWfMClh1ECPvyUjlqD7MwLq4mZdUusD1qAwAdsvEJBs=
go.dtapp.net/gorandom v1.0.1/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8=
go.dtapp.net/gorequest v1.0.31 h1:r/OoU5Y00TbJjkQtpvwjsb/pllqO0UQQjFRY1veZYZc=

Loading…
Cancel
Save