From 78c77ff6ad74cc81dcd9f3581aec4f33abb04d61 Mon Sep 17 00:00:00 2001 From: dtapps Date: Tue, 26 Dec 2023 11:05:43 +0800 Subject: [PATCH] - update gin_gorm --- README.md | 4 +- api_gorm.go | 18 +++--- api_gorm_config.go | 28 ++++---- api_gorm_record.go | 42 ++++++------ api_slog_config.go | 14 ++-- api_slog_record.go | 18 +++--- gin_gorm.go | 158 +++++++++++++++++++++++++++++++++++++++++++++ gin_gorm_config.go | 29 +++++++++ gin_gorm_record.go | 108 +++++++++++++++++++++++++++++++ gin_slog.go | 18 +++--- gin_slog_cofing.go | 14 ++-- gin_slog_record.go | 45 ++----------- version.go | 2 +- 13 files changed, 378 insertions(+), 120 deletions(-) create mode 100644 gin_gorm.go create mode 100644 gin_gorm_config.go create mode 100644 gin_gorm_record.go diff --git a/README.md b/README.md index 3f8aace..05db4a3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@

-Golang Log +Golang

[comment]: <> (go) @@ -11,5 +11,5 @@ #### 安装 ```shell -go get -v -u go.dtapp.net/golog@v1.0.104 +go get -v -u go.dtapp.net/golog@v1.0.105 ``` diff --git a/api_gorm.go b/api_gorm.go index 18be085..559d2af 100644 --- a/api_gorm.go +++ b/api_gorm.go @@ -68,22 +68,22 @@ func NewApiGorm(ctx context.Context, systemOutsideIp string, gormClient *dorm.Go } // Middleware 中间件 -func (gl *ApiGorm) Middleware(ctx context.Context, request gorequest.Response) { - if gl.gormConfig.stats { - gl.gormMiddleware(ctx, request) +func (ag *ApiGorm) Middleware(ctx context.Context, request gorequest.Response) { + if ag.gormConfig.stats { + ag.gormMiddleware(ctx, request) } } // MiddlewareXml 中间件 -func (gl *ApiGorm) MiddlewareXml(ctx context.Context, request gorequest.Response) { - if gl.gormConfig.stats { - gl.gormMiddlewareXml(ctx, request) +func (ag *ApiGorm) MiddlewareXml(ctx context.Context, request gorequest.Response) { + if ag.gormConfig.stats { + ag.gormMiddlewareXml(ctx, request) } } // MiddlewareCustom 中间件 -func (gl *ApiGorm) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response) { - if gl.gormConfig.stats { - gl.gormMiddlewareCustom(ctx, api, request) +func (ag *ApiGorm) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response) { + if ag.gormConfig.stats { + ag.gormMiddlewareCustom(ctx, api, request) } } diff --git a/api_gorm_config.go b/api_gorm_config.go index a138d5b..b01e421 100644 --- a/api_gorm_config.go +++ b/api_gorm_config.go @@ -6,24 +6,24 @@ import ( "runtime" ) -func (gl *ApiGorm) setConfig(ctx context.Context, systemOutsideIp string) { +func (ag *ApiGorm) setConfig(ctx context.Context, systemOutsideIp string) { info := getSystem() - gl.config.systemHostname = info.SystemHostname - gl.config.systemOs = info.SystemOs - gl.config.systemVersion = info.SystemVersion - gl.config.systemKernel = info.SystemKernel - gl.config.systemKernelVersion = info.SystemKernelVersion - gl.config.systemBootTime = info.SystemBootTime - gl.config.cpuCores = info.CpuCores - gl.config.cpuModelName = info.CpuModelName - gl.config.cpuMhz = info.CpuMhz + ag.config.systemHostname = info.SystemHostname + ag.config.systemOs = info.SystemOs + ag.config.systemVersion = info.SystemVersion + ag.config.systemKernel = info.SystemKernel + ag.config.systemKernelVersion = info.SystemKernelVersion + ag.config.systemBootTime = info.SystemBootTime + ag.config.cpuCores = info.CpuCores + ag.config.cpuModelName = info.CpuModelName + ag.config.cpuMhz = info.CpuMhz - gl.config.systemInsideIp = gorequest.GetInsideIp(ctx) - gl.config.systemOutsideIp = systemOutsideIp + ag.config.systemInsideIp = gorequest.GetInsideIp(ctx) + ag.config.systemOutsideIp = systemOutsideIp - gl.config.goVersion = runtime.Version() - gl.config.sdkVersion = Version + ag.config.goVersion = runtime.Version() + ag.config.sdkVersion = Version } diff --git a/api_gorm_record.go b/api_gorm_record.go index 5992a07..215d211 100644 --- a/api_gorm_record.go +++ b/api_gorm_record.go @@ -38,30 +38,30 @@ type apiPostgresqlLog struct { } // 创建模型 -func (gl *ApiGorm) gormAutoMigrate(ctx context.Context) { - err := gl.gormClient.GetDb().Table(gl.gormConfig.tableName).AutoMigrate(&apiPostgresqlLog{}) +func (ag *ApiGorm) gormAutoMigrate(ctx context.Context) { + err := ag.gormClient.GetDb().Table(ag.gormConfig.tableName).AutoMigrate(&apiPostgresqlLog{}) if err != nil { log.Printf("创建模型:%s", err) } } // 记录日志 -func (gl *ApiGorm) gormRecord(ctx context.Context, data apiPostgresqlLog) { +func (ag *ApiGorm) gormRecord(ctx context.Context, data apiPostgresqlLog) { if utf8.ValidString(data.ResponseBody) == false { data.ResponseBody = "" } - data.SystemHostName = gl.config.systemHostname //【系统】主机名 - data.SystemInsideIp = gl.config.systemInsideIp //【系统】内网ip - data.GoVersion = gl.config.goVersion //【程序】Go版本 - data.SdkVersion = gl.config.systemVersion //【程序】Sdk版本 + data.SystemHostName = ag.config.systemHostname //【系统】主机名 + data.SystemInsideIp = ag.config.systemInsideIp //【系统】内网ip + data.GoVersion = ag.config.goVersion //【程序】Go版本 + data.SdkVersion = ag.config.systemVersion //【程序】Sdk版本 data.TraceID = gotrace_id.GetTraceIdContext(ctx) //【记录】跟踪编号 - data.RequestIp = gl.config.systemOutsideIp //【请求】请求Ip - data.SystemOs = gl.config.systemOs //【系统】系统类型 - data.SystemArch = gl.config.systemKernel //【系统】系统架构 + data.RequestIp = ag.config.systemOutsideIp //【请求】请求Ip + data.SystemOs = ag.config.systemOs //【系统】系统类型 + data.SystemArch = ag.config.systemKernel //【系统】系统架构 - err := gl.gormClient.GetDb().Table(gl.gormConfig.tableName).Create(&data).Error + err := ag.gormClient.GetDb().Table(ag.gormConfig.tableName).Create(&data).Error if err != nil { log.Printf("记录接口日志错误:%s", err) log.Printf("记录接口日志数据:%+v", data) @@ -69,13 +69,13 @@ func (gl *ApiGorm) gormRecord(ctx context.Context, data apiPostgresqlLog) { } // GormDelete 删除 -func (gl *ApiGorm) GormDelete(ctx context.Context, hour int64) error { - return gl.GormCustomTableDelete(ctx, gl.gormConfig.tableName, hour) +func (ag *ApiGorm) GormDelete(ctx context.Context, hour int64) error { + return ag.GormCustomTableDelete(ctx, ag.gormConfig.tableName, hour) } // GormCustomTableDelete 删除数据 - 自定义表名 -func (gl *ApiGorm) GormCustomTableDelete(ctx context.Context, tableName string, hour int64) error { - err := gl.gormClient.GetDb().Table(tableName).Where("request_time < ?", gotime.Current().BeforeHour(hour).Format()).Delete(&apiPostgresqlLog{}).Error +func (ag *ApiGorm) GormCustomTableDelete(ctx context.Context, tableName string, hour int64) error { + err := ag.gormClient.GetDb().Table(tableName).Where("request_time < ?", gotime.Current().BeforeHour(hour).Format()).Delete(&apiPostgresqlLog{}).Error if err != nil { log.Printf("删除失败:%s", err) } @@ -83,7 +83,7 @@ func (gl *ApiGorm) GormCustomTableDelete(ctx context.Context, tableName string, } // 中间件 -func (gl *ApiGorm) gormMiddleware(ctx context.Context, request gorequest.Response) { +func (ag *ApiGorm) gormMiddleware(ctx context.Context, request gorequest.Response) { data := apiPostgresqlLog{ RequestTime: request.RequestTime, //【请求】时间 RequestUri: request.RequestUri, //【请求】链接 @@ -103,11 +103,11 @@ func (gl *ApiGorm) gormMiddleware(ctx context.Context, request gorequest.Respons } } - gl.gormRecord(ctx, data) + ag.gormRecord(ctx, data) } // 中间件 -func (gl *ApiGorm) gormMiddlewareXml(ctx context.Context, request gorequest.Response) { +func (ag *ApiGorm) gormMiddlewareXml(ctx context.Context, request gorequest.Response) { data := apiPostgresqlLog{ RequestTime: request.RequestTime, //【请求】时间 RequestUri: request.RequestUri, //【请求】链接 @@ -127,11 +127,11 @@ func (gl *ApiGorm) gormMiddlewareXml(ctx context.Context, request gorequest.Resp } } - gl.gormRecord(ctx, data) + ag.gormRecord(ctx, data) } // 中间件 -func (gl *ApiGorm) gormMiddlewareCustom(ctx context.Context, api string, request gorequest.Response) { +func (ag *ApiGorm) gormMiddlewareCustom(ctx context.Context, api string, request gorequest.Response) { data := apiPostgresqlLog{ RequestTime: request.RequestTime, //【请求】时间 RequestUri: request.RequestUri, //【请求】链接 @@ -151,5 +151,5 @@ func (gl *ApiGorm) gormMiddlewareCustom(ctx context.Context, api string, request } } - gl.gormRecord(ctx, data) + ag.gormRecord(ctx, data) } diff --git a/api_slog_config.go b/api_slog_config.go index 7f1f695..c0e9ca4 100644 --- a/api_slog_config.go +++ b/api_slog_config.go @@ -1,20 +1,20 @@ package golog // ConfigSLogClientFun 日志配置 -func (sl *ApiSLog) ConfigSLogClientFun(sLogFun SLogFun) { +func (al *ApiSLog) ConfigSLogClientFun(sLogFun SLogFun) { sLog := sLogFun() if sLog != nil { - sl.slog.client = sLog - sl.slog.status = true + al.slog.client = sLog + al.slog.status = true } } // ConfigSLogResultClientFun 日志配置然后返回 -func (sl *ApiSLog) ConfigSLogResultClientFun(sLogFun SLogFun) *ApiSLog { +func (al *ApiSLog) ConfigSLogResultClientFun(sLogFun SLogFun) *ApiSLog { sLog := sLogFun() if sLog != nil { - sl.slog.client = sLog - sl.slog.status = true + al.slog.client = sLog + al.slog.status = true } - return sl + return al } diff --git a/api_slog_record.go b/api_slog_record.go index 02647e5..11d627e 100644 --- a/api_slog_record.go +++ b/api_slog_record.go @@ -26,7 +26,7 @@ type apiSLog struct { } // Middleware 中间件 -func (sl *ApiSLog) Middleware(ctx context.Context, request gorequest.Response) { +func (al *ApiSLog) Middleware(ctx context.Context, request gorequest.Response) { data := apiSLog{ TraceID: gotrace_id.GetTraceIdContext(ctx), RequestTime: request.RequestTime, @@ -41,8 +41,8 @@ func (sl *ApiSLog) Middleware(ctx context.Context, request gorequest.Response) { ResponseBody: dorm.JsonDecodeNoError(request.ResponseBody), ResponseTime: request.ResponseTime, } - if sl.slog.status { - sl.slog.client.WithTraceId(ctx).Info("Middleware", + if al.slog.status { + al.slog.client.WithTraceId(ctx).Info("Middleware", "request_time", data.RequestTime, "request_uri", data.RequestUri, "request_url", data.RequestUrl, @@ -59,7 +59,7 @@ func (sl *ApiSLog) Middleware(ctx context.Context, request gorequest.Response) { } // MiddlewareXml 中间件 -func (sl *ApiSLog) MiddlewareXml(ctx context.Context, request gorequest.Response) { +func (al *ApiSLog) MiddlewareXml(ctx context.Context, request gorequest.Response) { data := apiSLog{ TraceID: gotrace_id.GetTraceIdContext(ctx), RequestTime: request.RequestTime, @@ -74,8 +74,8 @@ func (sl *ApiSLog) MiddlewareXml(ctx context.Context, request gorequest.Response ResponseBody: dorm.XmlDecodeNoError(request.ResponseBody), ResponseTime: request.ResponseTime, } - if sl.slog.status { - sl.slog.client.WithTraceId(ctx).Info("MiddlewareXml", + if al.slog.status { + al.slog.client.WithTraceId(ctx).Info("MiddlewareXml", "request_time", data.RequestTime, "request_uri", data.RequestUri, "request_url", data.RequestUrl, @@ -92,7 +92,7 @@ func (sl *ApiSLog) MiddlewareXml(ctx context.Context, request gorequest.Response } // MiddlewareCustom 中间件 -func (sl *ApiSLog) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response) { +func (al *ApiSLog) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response) { data := apiSLog{ TraceID: gotrace_id.GetTraceIdContext(ctx), RequestTime: request.RequestTime, @@ -107,8 +107,8 @@ func (sl *ApiSLog) MiddlewareCustom(ctx context.Context, api string, request gor ResponseBody: dorm.JsonDecodeNoError(request.ResponseBody), ResponseTime: request.ResponseTime, } - if sl.slog.status { - sl.slog.client.WithTraceId(ctx).Info("MiddlewareCustom", + if al.slog.status { + al.slog.client.WithTraceId(ctx).Info("MiddlewareCustom", "request_time", data.RequestTime, "request_uri", data.RequestUri, "request_url", data.RequestUrl, diff --git a/gin_gorm.go b/gin_gorm.go new file mode 100644 index 0000000..cda3500 --- /dev/null +++ b/gin_gorm.go @@ -0,0 +1,158 @@ +package golog + +import ( + "bytes" + "context" + "errors" + "github.com/gin-gonic/gin" + "go.dtapp.net/dorm" + "go.dtapp.net/goip" + "go.dtapp.net/gojson" + "go.dtapp.net/gorequest" + "go.dtapp.net/gotime" + "go.dtapp.net/gotrace_id" + "io/ioutil" +) + +// GinGorm 框架日志 +type GinGorm struct { + gormClient *dorm.GormClient // 数据库驱动 + ipService *goip.Client // IP服务 + config struct { + systemHostname string // 主机名 + systemOs string // 系统类型 + systemVersion string // 系统版本 + systemKernel string // 系统内核 + systemKernelVersion string // 系统内核版本 + systemBootTime uint64 // 系统开机时间 + cpuCores int // CPU核数 + cpuModelName string // CPU型号名称 + cpuMhz float64 // CPU兆赫 + systemInsideIp string // 内网ip + systemOutsideIp string // 外网ip + goVersion string // go版本 + sdkVersion string // sdk版本 + } + gormConfig struct { + stats bool // 状态 + tableName string // 表名 + } +} + +// GinGormFun *GinGorm 框架日志驱动 +type GinGormFun func() *GinGorm + +// NewGinGorm 创建框架实例化 +func NewGinGorm(ctx context.Context, systemOutsideIp string, ipService *goip.Client, gormClient *dorm.GormClient, gormTableName string) (*GinGorm, error) { + + gg := &GinGorm{} + + // 配置信息 + if systemOutsideIp == "" { + return nil, errors.New("没有设置外网IP") + } + gg.setConfig(ctx, systemOutsideIp) + + gg.ipService = ipService + + if gormClient == nil || gormClient.GetDb() == nil { + gg.gormConfig.stats = false + } else { + + gg.gormClient = gormClient + + if gormTableName == "" { + return nil, errors.New("没有设置表名") + } else { + gg.gormConfig.tableName = gormTableName + } + + // 创建模型 + gg.gormAutoMigrate(ctx) + + gg.gormConfig.stats = true + } + + return gg, nil +} + +type bodyGormWriter struct { + gin.ResponseWriter + body *bytes.Buffer +} + +func (w bodyGormWriter) Write(b []byte) (int, error) { + w.body.Write(b) + return w.ResponseWriter.Write(b) +} + +func (w bodyGormWriter) WriteString(s string) (int, error) { + w.body.WriteString(s) + return w.ResponseWriter.WriteString(s) +} + +func (gg *GinGorm) jsonUnmarshal(data string) (result interface{}) { + _ = gojson.Unmarshal([]byte(data), &result) + return +} + +// Middleware 中间件 +func (gg *GinGorm) Middleware() gin.HandlerFunc { + return func(ginCtx *gin.Context) { + + // 开始时间 + startTime := gotime.Current().TimestampWithMillisecond() + requestTime := gotime.Current().Time + + // 获取全部内容 + paramsBody := gorequest.NewParams() + queryParams := ginCtx.Request.URL.Query() // 请求URL参数 + for key, values := range queryParams { + for _, value := range values { + paramsBody.Set(key, value) + } + } + var dataMap map[string]interface{} + rawData, _ := ginCtx.GetRawData() // 请求内容参数 + if gojson.IsValidJSON(string(rawData)) { + dataMap = gojson.JsonDecodeNoError(string(rawData)) + } else { + dataMap = gojson.ParseQueryString(string(rawData)) + } + for key, value := range dataMap { + paramsBody.Set(key, value) + } + + // 重新赋值 + ginCtx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(rawData)) + + blw := &bodyGormWriter{body: bytes.NewBufferString(""), ResponseWriter: ginCtx.Writer} + ginCtx.Writer = blw + + // 处理请求 + ginCtx.Next() + + // 响应 + responseCode := ginCtx.Writer.Status() + responseBody := blw.body.String() + + // 结束时间 + endTime := gotime.Current().TimestampWithMillisecond() + + go func() { + + clientIp := gorequest.ClientIp(ginCtx.Request) + var info = goip.AnalyseResult{} + + if gg.ipService != nil { + info = gg.ipService.Analyse(clientIp) + } + + var traceId = gotrace_id.GetGinTraceId(ginCtx) + + // 记录 + gg.recordJson(ginCtx, traceId, requestTime, paramsBody, responseCode, responseBody, startTime, endTime, info) + + }() + } +} diff --git a/gin_gorm_config.go b/gin_gorm_config.go new file mode 100644 index 0000000..d56c1e0 --- /dev/null +++ b/gin_gorm_config.go @@ -0,0 +1,29 @@ +package golog + +import ( + "context" + "go.dtapp.net/goip" + "runtime" +) + +func (gg *GinGorm) setConfig(ctx context.Context, systemOutsideIp string) { + + info := getSystem() + + gg.config.systemHostname = info.SystemHostname + gg.config.systemOs = info.SystemOs + gg.config.systemVersion = info.SystemVersion + gg.config.systemKernel = info.SystemKernel + gg.config.systemKernelVersion = info.SystemKernelVersion + gg.config.systemBootTime = info.SystemBootTime + gg.config.cpuCores = info.CpuCores + gg.config.cpuModelName = info.CpuModelName + gg.config.cpuMhz = info.CpuMhz + + gg.config.systemInsideIp = goip.GetInsideIp(ctx) + gg.config.systemOutsideIp = systemOutsideIp + + gg.config.sdkVersion = Version + gg.config.goVersion = runtime.Version() + +} diff --git a/gin_gorm_record.go b/gin_gorm_record.go new file mode 100644 index 0000000..c04038a --- /dev/null +++ b/gin_gorm_record.go @@ -0,0 +1,108 @@ +package golog + +import ( + "context" + "github.com/gin-gonic/gin" + "go.dtapp.net/dorm" + "go.dtapp.net/goip" + "go.dtapp.net/gorequest" + "go.dtapp.net/gotime" + "go.dtapp.net/gourl" + "log" + "time" +) + +// 模型 +type ginPostgresqlLog struct { + LogId uint `gorm:"primaryKey;comment:【记录】编号" json:"log_id,omitempty"` //【记录】编号 + TraceId string `gorm:"index;comment:【系统】跟踪编号" json:"trace_id,omitempty"` //【系统】跟踪编号 + RequestTime time.Time `gorm:"index;comment:【请求】时间" json:"request_time,omitempty"` //【请求】时间 + RequestUri string `gorm:"comment:【请求】请求链接 域名+路径+参数" json:"request_uri,omitempty"` //【请求】请求链接 域名+路径+参数 + RequestUrl string `gorm:"comment:【请求】请求链接 域名+路径" json:"request_url,omitempty"` //【请求】请求链接 域名+路径 + RequestApi string `gorm:"index;comment:【请求】请求接口 路径" json:"request_api,omitempty"` //【请求】请求接口 路径 + RequestMethod string `gorm:"index;comment:【请求】请求方式" json:"request_method,omitempty"` //【请求】请求方式 + RequestProto string `gorm:"comment:【请求】请求协议" json:"request_proto,omitempty"` //【请求】请求协议 + RequestUa string `gorm:"comment:【请求】请求UA" json:"request_ua,omitempty"` //【请求】请求UA + RequestReferer string `gorm:"comment:【请求】请求referer" json:"request_referer,omitempty"` //【请求】请求referer + RequestBody string `gorm:"comment:【请求】请求主体" json:"request_body,omitempty"` //【请求】请求主体 + RequestUrlQuery string `gorm:"comment:【请求】请求URL参数" json:"request_url_query,omitempty"` //【请求】请求URL参数 + RequestIp string `gorm:"index;comment:【请求】请求客户端Ip" json:"request_ip,omitempty"` //【请求】请求客户端Ip + RequestIpCountry string `gorm:"index;comment:【请求】请求客户端城市" json:"request_ip_country,omitempty"` //【请求】请求客户端城市 + RequestIpProvince string `gorm:"index;comment:【请求】请求客户端省份" json:"request_ip_province,omitempty"` //【请求】请求客户端省份 + RequestIpCity string `gorm:"index;comment:【请求】请求客户端城市" json:"request_ip_city,omitempty"` //【请求】请求客户端城市 + RequestIpIsp string `gorm:"index;comment:【请求】请求客户端运营商" json:"request_ip_isp,omitempty"` //【请求】请求客户端运营商 + RequestIpLongitude float64 `gorm:"index;comment:【请求】请求客户端经度" json:"request_ip_longitude,omitempty"` //【请求】请求客户端经度 + RequestIpLatitude float64 `gorm:"index;comment:【请求】请求客户端纬度" json:"request_ip_latitude,omitempty"` //【请求】请求客户端纬度 + RequestHeader string `gorm:"comment:【请求】请求头" json:"request_header,omitempty"` //【请求】请求头 + ResponseTime time.Time `gorm:"index;comment:【返回】时间" json:"response_time,omitempty"` //【返回】时间 + ResponseCode int `gorm:"index;comment:【返回】状态码" json:"response_code,omitempty"` //【返回】状态码 + ResponseMsg string `gorm:"comment:【返回】描述" json:"response_msg,omitempty"` //【返回】描述 + ResponseData string `gorm:"comment:【返回】数据" json:"response_data,omitempty"` //【返回】数据 + CostTime int64 `gorm:"comment:【系统】花费时间" json:"cost_time,omitempty"` //【系统】花费时间 + SystemHostName string `gorm:"index;comment:【系统】主机名" json:"system_host_name,omitempty"` //【系统】主机名 + SystemInsideIp string `gorm:"comment:【系统】内网ip" json:"system_inside_ip,omitempty"` //【系统】内网ip + SystemOs string `gorm:"index;comment:【系统】系统类型" json:"system_os,omitempty"` //【系统】系统类型 + SystemArch string `gorm:"index;comment:【系统】系统架构" json:"system_arch,omitempty"` //【系统】系统架构 + GoVersion string `gorm:"comment:【程序】Go版本" json:"go_version,omitempty"` //【程序】Go版本 + SdkVersion string `gorm:"comment:【程序】Sdk版本" json:"sdk_version,omitempty"` //【程序】Sdk版本 +} + +// 创建模型 +func (gg *GinGorm) gormAutoMigrate(ctx context.Context) { + err := gg.gormClient.GetDb().Table(gg.gormConfig.tableName).AutoMigrate(&ginPostgresqlLog{}) + if err != nil { + log.Printf("创建模型:%s\n", err) + } +} + +// gormRecord 记录日志 +func (gg *GinGorm) gormRecord(data ginPostgresqlLog) { + + data.SystemHostName = gg.config.systemHostname //【系统】主机名 + data.SystemInsideIp = gg.config.systemInsideIp //【系统】内网ip + data.GoVersion = gg.config.goVersion //【程序】Go版本 + data.SdkVersion = gg.config.sdkVersion //【程序】Sdk版本 + data.SystemOs = gg.config.systemOs //【系统】系统类型 + data.SystemArch = gg.config.systemKernel //【系统】系统架构 + + err := gg.gormClient.GetDb().Table(gg.gormConfig.tableName).Create(&data).Error + if err != nil { + log.Printf("记录框架日志错误:%s\n", err) + log.Printf("记录框架日志数据:%+v\n", data) + } +} + +func (gg *GinGorm) recordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody gorequest.Params, responseCode int, responseBody string, startTime, endTime int64, ipInfo goip.AnalyseResult) { + + data := ginPostgresqlLog{ + TraceId: traceId, //【系统】跟踪编号 + RequestTime: requestTime, //【请求】时间 + RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 + RequestApi: gourl.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 + RequestMethod: ginCtx.Request.Method, //【请求】请求方式 + RequestProto: ginCtx.Request.Proto, //【请求】请求协议 + RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA + RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer + RequestBody: dorm.JsonEncodeNoError(requestBody), //【请求】请求主体 + RequestUrlQuery: dorm.JsonEncodeNoError(ginCtx.Request.URL.Query()), //【请求】请求URL参数 + RequestIp: ipInfo.Ip, //【请求】请求客户端Ip + RequestIpCountry: ipInfo.Country, //【请求】请求客户端城市 + RequestIpProvince: ipInfo.Province, //【请求】请求客户端省份 + RequestIpCity: ipInfo.City, //【请求】请求客户端城市 + RequestIpIsp: ipInfo.Isp, //【请求】请求客户端运营商 + RequestIpLatitude: ipInfo.LocationLatitude, //【请求】请求客户端纬度 + RequestIpLongitude: ipInfo.LocationLongitude, //【请求】请求客户端经度 + RequestHeader: dorm.JsonEncodeNoError(ginCtx.Request.Header), //【请求】请求头 + ResponseTime: gotime.Current().Time, //【返回】时间 + ResponseCode: responseCode, //【返回】状态码 + ResponseData: responseBody, //【返回】数据 + CostTime: endTime - startTime, //【系统】花费时间 + } + if ginCtx.Request.TLS == nil { + data.RequestUri = "http://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接 + } else { + data.RequestUri = "https://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接 + } + + gg.gormRecord(data) +} diff --git a/gin_slog.go b/gin_slog.go index 2bba99d..76c4a61 100644 --- a/gin_slog.go +++ b/gin_slog.go @@ -31,28 +31,28 @@ func NewGinSLog(ctx context.Context, ipService *goip.Client) (*GinSLog, error) { return c, nil } -type bodyLogWriter struct { +type bodySLogWriter struct { gin.ResponseWriter body *bytes.Buffer } -func (w bodyLogWriter) Write(b []byte) (int, error) { +func (w bodySLogWriter) Write(b []byte) (int, error) { w.body.Write(b) return w.ResponseWriter.Write(b) } -func (w bodyLogWriter) WriteString(s string) (int, error) { +func (w bodySLogWriter) WriteString(s string) (int, error) { w.body.WriteString(s) return w.ResponseWriter.WriteString(s) } -func (c *GinSLog) jsonUnmarshal(data string) (result interface{}) { +func (gl *GinSLog) jsonUnmarshal(data string) (result interface{}) { _ = gojson.Unmarshal([]byte(data), &result) return } // Middleware 中间件 -func (c *GinSLog) Middleware() gin.HandlerFunc { +func (gl *GinSLog) Middleware() gin.HandlerFunc { return func(ginCtx *gin.Context) { // 开始时间 @@ -81,7 +81,7 @@ func (c *GinSLog) Middleware() gin.HandlerFunc { // 重新赋值 ginCtx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(rawData)) - blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: ginCtx.Writer} + blw := &bodySLogWriter{body: bytes.NewBufferString(""), ResponseWriter: ginCtx.Writer} ginCtx.Writer = blw // 处理请求 @@ -99,14 +99,14 @@ func (c *GinSLog) Middleware() gin.HandlerFunc { clientIp := gorequest.ClientIp(ginCtx.Request) var info = goip.AnalyseResult{} - if c.ipService != nil { - info = c.ipService.Analyse(clientIp) + if gl.ipService != nil { + info = gl.ipService.Analyse(clientIp) } var traceId = gotrace_id.GetGinTraceId(ginCtx) // 记录 - c.recordJson(ginCtx, traceId, requestTime, paramsBody, responseCode, responseBody, startTime, endTime, info) + gl.recordJson(ginCtx, traceId, requestTime, paramsBody, responseCode, responseBody, startTime, endTime, info) }() } diff --git a/gin_slog_cofing.go b/gin_slog_cofing.go index 6168d9e..07437a3 100644 --- a/gin_slog_cofing.go +++ b/gin_slog_cofing.go @@ -1,20 +1,20 @@ package golog // ConfigSLogClientFun 日志配置 -func (c *GinSLog) ConfigSLogClientFun(sLogFun SLogFun) { +func (gl *GinSLog) ConfigSLogClientFun(sLogFun SLogFun) { sLog := sLogFun() if sLog != nil { - c.slog.client = sLog - c.slog.status = true + gl.slog.client = sLog + gl.slog.status = true } } // ConfigSLogResultClientFun 日志配置然后返回 -func (c *GinSLog) ConfigSLogResultClientFun(sLogFun SLogFun) *GinSLog { +func (gl *GinSLog) ConfigSLogResultClientFun(sLogFun SLogFun) *GinSLog { sLog := sLogFun() if sLog != nil { - c.slog.client = sLog - c.slog.status = true + gl.slog.client = sLog + gl.slog.status = true } - return c + return gl } diff --git a/gin_slog_record.go b/gin_slog_record.go index 2c3586e..e067258 100644 --- a/gin_slog_record.go +++ b/gin_slog_record.go @@ -2,7 +2,6 @@ package golog import ( "github.com/gin-gonic/gin" - "go.dtapp.net/dorm" "go.dtapp.net/goip" "go.dtapp.net/gorequest" "go.dtapp.net/gotime" @@ -40,8 +39,8 @@ type ginSLog struct { } // record 记录日志 -func (c *GinSLog) record(msg string, data ginSLog) { - c.slog.client.WithTraceIdStr(data.TraceID).Info(msg, +func (gl *GinSLog) record(msg string, data ginSLog) { + gl.slog.client.WithTraceIdStr(data.TraceID).Info(msg, "request_time", data.RequestTime, "request_uri", data.RequestUri, "request_url", data.RequestUrl, @@ -69,7 +68,7 @@ func (c *GinSLog) record(msg string, data ginSLog) { ) } -func (c *GinSLog) recordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, paramsBody gorequest.Params, responseCode int, responseBody string, startTime, endTime int64, ipInfo goip.AnalyseResult) { +func (gl *GinSLog) recordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, paramsBody gorequest.Params, responseCode int, responseBody string, startTime, endTime int64, ipInfo goip.AnalyseResult) { data := ginSLog{ TraceID: traceId, //【系统】跟踪编号 RequestTime: requestTime, //【请求】时间 @@ -99,41 +98,5 @@ func (c *GinSLog) recordJson(ginCtx *gin.Context, traceId string, requestTime ti } else { data.RequestUri = "https://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接 } - c.record("json", data) -} - -func (c *GinSLog) recordXml(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, paramsBody gorequest.Params, responseCode int, responseBody string, startTime, endTime int64, ipInfo goip.AnalyseResult) { - data := ginSLog{ - TraceID: traceId, //【系统】跟踪编号 - RequestTime: requestTime, //【请求】时间 - RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 - RequestApi: gourl.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 - RequestMethod: ginCtx.Request.Method, //【请求】请求方式 - RequestProto: ginCtx.Request.Proto, //【请求】请求协议 - RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA - RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer - RequestUrlQuery: ginCtx.Request.URL.Query(), //【请求】请求URL参数 - RequestIP: ipInfo.Ip, //【请求】请求客户端Ip - RequestIpCountry: ipInfo.Country, //【请求】请求客户端城市 - RequestIpProvince: ipInfo.Province, //【请求】请求客户端省份 - RequestIpCity: ipInfo.City, //【请求】请求客户端城市 - RequestIpIsp: ipInfo.Isp, //【请求】请求客户端运营商 - RequestIpLatitude: ipInfo.LocationLatitude, //【请求】请求客户端纬度 - RequestIpLongitude: ipInfo.LocationLongitude, //【请求】请求客户端经度 - RequestHeader: ginCtx.Request.Header, //【请求】请求头 - RequestAllContent: paramsBody, //【请求】请求全部内容 - ResponseTime: gotime.Current().Time, //【返回】时间 - ResponseCode: responseCode, //【返回】状态码 - ResponseData: responseBody, //【返回】数据 - CostTime: endTime - startTime, //【系统】花费时间 - } - if ginCtx.Request.TLS == nil { - data.RequestUri = "http://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接 - } else { - data.RequestUri = "https://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接 - } - if len(requestBody) > 0 { - data.RequestBody = dorm.XmlEncodeNoError(dorm.XmlDecodeNoError(requestBody)) //【请求】请求内容 - } - c.record("xml", data) + gl.record("json", data) } diff --git a/version.go b/version.go index 86163ec..648c560 100644 --- a/version.go +++ b/version.go @@ -1,5 +1,5 @@ package golog const ( - Version = "1.0.104" + Version = "1.0.105" )