parent
d63a3e16d1
commit
78c77ff6ad
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
package golog
|
||||
|
||||
const (
|
||||
Version = "1.0.104"
|
||||
Version = "1.0.105"
|
||||
)
|
||||
|
Loading…
Reference in new issue