- update debug

master v1.0.60
李光春 2 years ago
parent ab40cf6a20
commit 5ad38c7138

@ -1,5 +1,5 @@
package golog
const (
Version = "1.0.59"
Version = "1.0.60"
)

@ -24,12 +24,12 @@ type GinClient struct {
mongoClient *dorm.MongoClient // 数据库驱动
ipService *goip.Client // ip服务
zapLog *ZapLog // 日志服务
logDebug bool // 日志开关
gormConfig struct {
tableName string // 表名
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
debug bool // 日志开关
}
mongoConfig struct {
databaseName string // 库名
@ -37,7 +37,6 @@ type GinClient struct {
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
debug bool // 日志开关
}
log struct {
gorm bool // 日志开关
@ -75,6 +74,8 @@ func NewGinClient(config *GinClientConfig) (*GinClient, error) {
c.zapLog = config.ZapLog
c.logDebug = config.Debug
gormClient, gormTableName := config.GormClientFun()
mongoClient, mongoDatabaseName, mongoCollectionName := config.MongoClientFun()
@ -93,8 +94,6 @@ func NewGinClient(config *GinClientConfig) (*GinClient, error) {
}
c.gormConfig.tableName = gormTableName
c.gormConfig.debug = config.Debug
c.ipService = config.IpService
err := c.gormAutoMigrate()
@ -124,8 +123,6 @@ func NewGinClient(config *GinClientConfig) (*GinClient, error) {
}
c.mongoConfig.collectionName = mongoCollectionName
c.mongoConfig.debug = config.Debug
c.ipService = config.IpService
c.mongoConfig.hostname = hostname
@ -240,16 +237,28 @@ func (c *GinClient) Middleware() gin.HandlerFunc {
// 记录
if c.log.gorm {
if dataJson {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.Middleware]准备使用{gormRecordJson}保存数据")
}
c.gormRecordJson(ginCtx, traceId, requestTime, jsonBody, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp)
} else {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.Middleware]准备使用{gormRecordXml}保存数据")
}
c.gormRecordXml(ginCtx, traceId, requestTime, xmlBody, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp)
}
}
// 记录
if c.log.mongo {
if dataJson {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.Middleware]准备使用{mongoRecordJson}保存数据")
}
c.mongoRecordJson(ginCtx, traceId, requestTime, jsonBody, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp)
} else {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.Middleware]准备使用{mongoRecordXml}保存数据")
}
c.mongoRecordXml(ginCtx, traceId, requestTime, xmlBody, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp)
}
}

@ -42,6 +42,8 @@ func NewGinGormClient(config *GinGormClientConfig) (*GinClient, error) {
c.zapLog = config.ZapLog
c.logDebug = config.Debug
client, tableName := config.GormClientFun()
if client == nil || client.Db == nil {
@ -55,8 +57,6 @@ func NewGinGormClient(config *GinGormClientConfig) (*GinClient, error) {
}
c.gormConfig.tableName = tableName
c.gormConfig.debug = config.Debug
c.ipService = config.IpService
err := c.gormAutoMigrate()
@ -136,6 +136,11 @@ func (c *GinClient) gormRecord(postgresqlLog ginPostgresqlLog) (err error) {
}
func (c *GinClient) gormRecordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody map[string]interface{}, responseCode int, responseBody string, startTime, endTime int64, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp string) {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.gormRecordJson]收到保存数据要求")
}
data := ginPostgresqlLog{
TraceId: traceId, //【系统】跟踪编号
RequestTime: requestTime, //【请求】时间
@ -179,6 +184,11 @@ func (c *GinClient) gormRecordJson(ginCtx *gin.Context, traceId string, requestT
}
func (c *GinClient) gormRecordXml(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody map[string]string, responseCode int, responseBody string, startTime, endTime int64, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp string) {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.gormRecordXml]收到保存数据要求")
}
data := ginPostgresqlLog{
TraceId: traceId, //【系统】跟踪编号
RequestTime: requestTime, //【请求】时间
@ -236,8 +246,8 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
// 获取
data, _ := ioutil.ReadAll(ginCtx.Request.Body)
if c.gormConfig.debug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.GormMiddleware] %s\n", data)
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.GormMiddleware]%s", data)
}
// 复用
@ -272,12 +282,12 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
xmlBody = goxml.XmlDecode(string(data))
}
if c.gormConfig.debug {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.GormMiddleware.len(jsonBody)] %v\n", len(jsonBody))
}
if err != nil {
if c.gormConfig.debug {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.GormMiddleware.json.Unmarshal] %s %s\n", jsonBody, err)
}
dataJson = false
@ -285,7 +295,7 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
}
}
if c.gormConfig.debug {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.GormMiddleware.xmlBody] %s\n", xmlBody)
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.GormMiddleware.jsonBody] %s\n", jsonBody)
}
@ -317,18 +327,24 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
var traceId = gotrace_id.GetGinTraceId(ginCtx)
if dataJson {
if c.gormConfig.debug {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.GormMiddleware.gormRecord.json.request_body] %s\n", jsonBody)
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.GormMiddleware.gormRecord.json.request_body] %s\n", dorm.JsonEncodeNoError(jsonBody))
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.GormMiddleware.gormRecord.json.request_body] %s\n", datatypes.JSON(dorm.JsonEncodeNoError(jsonBody)))
}
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.GormMiddleware]准备使用{gormRecordJson}保存数据")
}
c.gormRecordJson(ginCtx, traceId, requestTime, jsonBody, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp)
} else {
if c.gormConfig.debug {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.GormMiddleware.gormRecord.xml.request_body] %s\n", xmlBody)
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.GormMiddleware.gormRecord.xml.request_body] %s\n", dorm.JsonEncodeNoError(xmlBody))
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.GormMiddleware.gormRecord.xml.request_body] %s\n", datatypes.JSON(dorm.JsonEncodeNoError(xmlBody)))
}
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.GormMiddleware]准备使用{gormRecordXml}保存数据")
}
c.gormRecordXml(ginCtx, traceId, requestTime, xmlBody, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp)
}
}

@ -45,6 +45,8 @@ func NewGinMongoClient(config *GinMongoClientConfig) (*GinClient, error) {
c.zapLog = config.ZapLog
c.logDebug = config.Debug
client, databaseName, collectionName := config.MongoClientFun()
if client == nil || client.Db == nil {
@ -63,8 +65,6 @@ func NewGinMongoClient(config *GinMongoClientConfig) (*GinClient, error) {
}
c.mongoConfig.collectionName = collectionName
c.mongoConfig.debug = config.Debug
c.ipService = config.IpService
hostname, _ := os.Hostname()
@ -220,6 +220,11 @@ func (c *GinClient) mongoRecord(mongoLog ginMongoLog) (err error) {
}
func (c *GinClient) mongoRecordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody map[string]interface{}, responseCode int, responseBody string, startTime, endTime int64, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp string) {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.mongoRecordJson]收到保存数据要求")
}
data := ginMongoLog{
TraceId: traceId, //【系统】跟踪编号
RequestTime: primitive.NewDateTimeFromTime(requestTime), //【请求】时间
@ -261,6 +266,11 @@ func (c *GinClient) mongoRecordJson(ginCtx *gin.Context, traceId string, request
}
func (c *GinClient) mongoRecordXml(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody map[string]string, responseCode int, responseBody string, startTime, endTime int64, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp string) {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.mongoRecordXml]收到保存数据要求")
}
data := ginMongoLog{
TraceId: traceId, //【系统】跟踪编号
RequestTime: primitive.NewDateTimeFromTime(requestTime), //【请求】时间
@ -317,7 +327,7 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
// 获取
data, _ := ioutil.ReadAll(ginCtx.Request.Body)
if c.mongoConfig.debug {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.MongoMiddleware] %s\n", data)
}
@ -353,12 +363,12 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
xmlBody = goxml.XmlDecode(string(data))
}
if c.mongoConfig.debug {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.MongoMiddleware.len(jsonBody)] %v\n", len(jsonBody))
}
if err != nil {
if c.mongoConfig.debug {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.gin.MongoMiddleware.json.Unmarshal] %s %s\n", jsonBody, err)
}
dataJson = false
@ -366,7 +376,7 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
}
}
if c.mongoConfig.debug {
if c.logDebug {
c.zapLog.WithLogger().Sugar().Infof("[golog.MongoMiddleware.xmlBody] %s\n", xmlBody)
c.zapLog.WithLogger().Sugar().Infof("[golog.MongoMiddleware.jsonBody] %s\n", jsonBody)
}
@ -398,14 +408,20 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
var traceId = gotrace_id.GetGinTraceId(ginCtx)
if dataJson {
if c.mongoConfig.debug {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.MongoMiddleware.mongoRecord.json.request_body] %s\n", jsonBody)
}
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.MongoMiddleware]准备使用{mongoRecordJson}保存数据")
}
c.mongoRecordJson(ginCtx, traceId, requestTime, jsonBody, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp)
} else {
if c.mongoConfig.debug {
if c.logDebug {
c.zapLog.WithTraceIdStr(traceId).Sugar().Infof("[golog.gin.MongoMiddleware.mongoRecord.xml.request_body] %s\n", xmlBody)
}
if c.logDebug {
c.zapLog.WithLogger().Sugar().Info("[golog.gin.MongoMiddleware]准备使用{mongoRecordXml}保存数据")
}
c.mongoRecordXml(ginCtx, traceId, requestTime, xmlBody, responseCode, responseBody, startTime, endTime, clientIp, requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp)
}
}

Loading…
Cancel
Save