Compare commits

...

2 Commits

Author SHA1 Message Date
dtapps 78c77ff6ad - update gin_gorm
5 months ago
dtapps d63a3e16d1 - update gin_slog
5 months ago

@ -1,5 +1,5 @@
<h1>
<a href="https://www.dtapp.net/">Golang Log</a>
<a href="https://www.dtapp.net/">Golang</a>
</h1>
[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
```

@ -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)
}
}

@ -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
}

@ -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)
}

@ -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
}

@ -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,

@ -1,38 +0,0 @@
package golog
import "go.dtapp.net/goip"
// GinClient 框架日志
type GinClient struct {
ipService *goip.Client // IP服务
slog struct {
status bool // 状态
client *SLog // 日志服务
}
}
// GinClientFun 框架日志驱动
type GinClientFun func() *GinClient
// GinClientConfig 框架日志配置
type GinClientConfig struct {
IpService *goip.Client // IP服务
}
// GinCustomClient 框架自定义日志
type GinCustomClient struct {
ipService *goip.Client // IP服务
slog struct {
status bool // 状态
client *SLog // 日志服务
}
}
// GinCustomClientFun 框架自定义日志驱动
type GinCustomClientFun func() *GinCustomClient
// GinCustomClientConfig 框架自定义日志配置
type GinCustomClientConfig struct {
IpService *goip.Client // IP服务
CurrentIp string // 当前IP
}

@ -1,11 +0,0 @@
package golog
import (
"context"
)
func NewGinCustomClient(ctx context.Context, config *GinCustomClientConfig) (*GinCustomClient, error) {
c := &GinCustomClient{}
c.ipService = config.IpService
return c, nil
}

@ -1,20 +0,0 @@
package golog
// ConfigSLogClientFun 日志配置
func (c *GinCustomClient) ConfigSLogClientFun(sLogFun SLogFun) {
sLog := sLogFun()
if sLog != nil {
c.slog.client = sLog
c.slog.status = true
}
}
// ConfigSLogResultClientFun 日志配置然后返回
func (c *GinCustomClient) ConfigSLogResultClientFun(sLogFun SLogFun) *GinCustomClient {
sLog := sLogFun()
if sLog != nil {
c.slog.client = sLog
c.slog.status = true
}
return c
}

@ -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)
}()
}
}

@ -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()
}

@ -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)
}

@ -12,35 +12,47 @@ import (
"io/ioutil"
)
// NewGinClient 创建框架实例化
func NewGinClient(ctx context.Context, config *GinClientConfig) (*GinClient, error) {
c := &GinClient{}
c.ipService = config.IpService
// GinSLog 框架日志
type GinSLog struct {
ipService *goip.Client // IP服务
slog struct {
status bool // 状态
client *SLog // 日志服务
}
}
// GinSLogFun 框架日志驱动
type GinSLogFun func() *GinSLog
// NewGinSLog 创建框架实例化
func NewGinSLog(ctx context.Context, ipService *goip.Client) (*GinSLog, error) {
c := &GinSLog{}
c.ipService = ipService
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 *GinClient) jsonUnmarshal(data string) (result interface{}) {
func (gl *GinSLog) jsonUnmarshal(data string) (result interface{}) {
_ = gojson.Unmarshal([]byte(data), &result)
return
}
// Middleware 中间件
func (c *GinClient) Middleware() gin.HandlerFunc {
func (gl *GinSLog) Middleware() gin.HandlerFunc {
return func(ginCtx *gin.Context) {
// 开始时间
@ -69,7 +81,7 @@ func (c *GinClient) 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
// 处理请求
@ -87,14 +99,14 @@ func (c *GinClient) 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)
}()
}

@ -0,0 +1,20 @@
package golog
// ConfigSLogClientFun 日志配置
func (gl *GinSLog) ConfigSLogClientFun(sLogFun SLogFun) {
sLog := sLogFun()
if sLog != nil {
gl.slog.client = sLog
gl.slog.status = true
}
}
// ConfigSLogResultClientFun 日志配置然后返回
func (gl *GinSLog) ConfigSLogResultClientFun(sLogFun SLogFun) *GinSLog {
sLog := sLogFun()
if sLog != nil {
gl.slog.client = sLog
gl.slog.status = true
}
return gl
}

@ -0,0 +1,25 @@
package golog
import (
"context"
"go.dtapp.net/goip"
)
// GinSLogCustom 框架自定义日志
type GinSLogCustom struct {
ipService *goip.Client // IP服务
slog struct {
status bool // 状态
client *SLog // 日志服务
}
}
// GinSLogCustomFun 框架自定义日志驱动
type GinSLogCustomFun func() *GinSLogCustom
// NewGinSLogCustom 创建框架实例化
func NewGinSLogCustom(ctx context.Context, ipService *goip.Client) (*GinSLogCustom, error) {
c := &GinSLogCustom{}
c.ipService = ipService
return c, nil
}

@ -1,7 +1,7 @@
package golog
// ConfigSLogClientFun 日志配置
func (c *GinClient) ConfigSLogClientFun(sLogFun SLogFun) {
func (c *GinSLogCustom) ConfigSLogClientFun(sLogFun SLogFun) {
sLog := sLogFun()
if sLog != nil {
c.slog.client = sLog
@ -10,7 +10,7 @@ func (c *GinClient) ConfigSLogClientFun(sLogFun SLogFun) {
}
// ConfigSLogResultClientFun 日志配置然后返回
func (c *GinClient) ConfigSLogResultClientFun(sLogFun SLogFun) *GinClient {
func (c *GinSLogCustom) ConfigSLogResultClientFun(sLogFun SLogFun) *GinSLogCustom {
sLog := sLogFun()
if sLog != nil {
c.slog.client = sLog

@ -41,7 +41,7 @@ type GinCustomClientGinRecordOperation struct {
}
// GinRecord 记录日志
func (c *GinCustomClient) GinRecord(ginCtx *gin.Context) *GinCustomClientGinRecordOperation {
func (c *GinSLogCustom) GinRecord(ginCtx *gin.Context) *GinCustomClientGinRecordOperation {
operation := &GinCustomClientGinRecordOperation{
slogClient: c.slog.client,
ipService: c.ipService,

@ -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 *GinClient) 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 *GinClient) record(msg string, data ginSLog) {
)
}
func (c *GinClient) 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 *GinClient) recordJson(ginCtx *gin.Context, traceId string, requestTime
} else {
data.RequestUri = "https://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接
}
c.record("json", data)
}
func (c *GinClient) 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)
}

@ -9,9 +9,9 @@ require (
go.dtapp.net/dorm v1.0.55
go.dtapp.net/goip v1.0.43
go.dtapp.net/gojson v1.0.2
go.dtapp.net/gorequest v1.0.40
go.dtapp.net/gorequest v1.0.41
go.dtapp.net/gotime v1.0.6
go.dtapp.net/gotrace_id v1.0.6
go.dtapp.net/gotrace_id v1.0.8
go.dtapp.net/gourl v1.0.0
)
@ -57,7 +57,7 @@ require (
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.dtapp.net/gorandom v1.0.2 // indirect
go.dtapp.net/gostring v1.0.12 // indirect
go.dtapp.net/gostring v1.0.13 // indirect
golang.org/x/arch v0.6.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/mod v0.14.0 // indirect
@ -66,8 +66,9 @@ require (
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/datatypes v1.2.0 // indirect
gorm.io/driver/mysql v1.5.2 // indirect

@ -56,7 +56,6 @@ github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0kt
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@ -144,14 +143,14 @@ go.dtapp.net/gojson v1.0.2 h1:NjslBOhAK3XvJepkML7LXcJRPtSfp3rDXGK/29VlDBw=
go.dtapp.net/gojson v1.0.2/go.mod h1:U0Vd2iSLKqdyg6oungBJVHxuYswTbeQVjruQC3/JqQ4=
go.dtapp.net/gorandom v1.0.2 h1:08BdcBP/fQiRw2Ii0OXATSTtObwNydlDlqc/j/u5O8Q=
go.dtapp.net/gorandom v1.0.2/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8=
go.dtapp.net/gorequest v1.0.40 h1:11WPUWbIb0AZztfALXTeeu9yPwtQG2LNkWm/5hGZGxc=
go.dtapp.net/gorequest v1.0.40/go.mod h1:t9GF4hMOd/UnNpX5ntHKM4hazy4+cBEcc0rGVzdK83Y=
go.dtapp.net/gostring v1.0.12 h1:r1gwy4sa3SZAPJLMXBeYu7zgZLThA9a06SwRnJ1dlnA=
go.dtapp.net/gostring v1.0.12/go.mod h1:QDO8ApzmwFYzlcpA7lQxSO5ofjQZDmbshhRxQWihzyM=
go.dtapp.net/gorequest v1.0.41 h1:IaWPZ4oYEGsFQdnZrJseY+ytxyAcqtrOmwRsm+g5K2s=
go.dtapp.net/gorequest v1.0.41/go.mod h1:iX5s9j4EzQC+6ShOIK3+GPvjjq9fN+7Y/wj6XV8sbQc=
go.dtapp.net/gostring v1.0.13 h1:Z4R6f9q9arQlkSGNywVQssxxv0LkjHsRjHt/hfwO6/M=
go.dtapp.net/gostring v1.0.13/go.mod h1:qgEjuf0/TFC3ZtvZ9y5tWHBSmV1GmlKiRxhJYJih9uk=
go.dtapp.net/gotime v1.0.6 h1:f8YowUxpZtJbYawe5s5PmvGxRj61ydlzxAPYr5Fcetg=
go.dtapp.net/gotime v1.0.6/go.mod h1:Gq7eNLr2iMLP18UNWONRq4V3Uhf/ADp4bIrS+Tc6ktY=
go.dtapp.net/gotrace_id v1.0.6 h1:q6s8jy50vt1820b69JKQaFqbhGS5yJGMVtocwOGOPO0=
go.dtapp.net/gotrace_id v1.0.6/go.mod h1:o5kSzNK4s3GrrKpkRKXtAhArtBG1e5N5O5KGPlBlWG4=
go.dtapp.net/gotrace_id v1.0.8 h1:cNsPYzOz55B8SKit1smz5b03clr3CVaSCrFns9oOruU=
go.dtapp.net/gotrace_id v1.0.8/go.mod h1:tJzdWgm+8baWZa8K84GdiD99iPguXrn1CehFSh2W0OM=
go.dtapp.net/gourl v1.0.0 h1:Zbe0GiMFyyRy2+bjbVsYulakH5d58w3CDZkUPonlMoQ=
go.dtapp.net/gourl v1.0.0/go.mod h1:x9A/pJ3iKDTb6Gu2RtJy2iHg56IowXIcIGQdogqpGjs=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
@ -178,9 +177,8 @@ golang.org/x/tools v0.16.1 h1:TLyB3WofjdOEepBHAU20JdNC1Zbg87elYofWYAY5oZA=
golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=

@ -1,5 +1,5 @@
package golog
const (
Version = "1.0.104"
Version = "1.0.105"
)

Loading…
Cancel
Save