- update gin

- add api mongo
master v1.0.40
李光春 2 years ago
parent b7c4b649c6
commit 101ea45f6c

@ -18,15 +18,24 @@ type ApiClientConfig struct {
// ApiClient 接口
type ApiClient struct {
gormClient *dorm.GormClient // 数据库驱动
logClient *ZapLog // 日志驱动
config struct {
gormClient *dorm.GormClient // 数据库驱动
mongoClient *dorm.MongoClient // 数据库驱动
logClient *ZapLog // 日志驱动
config struct {
tableName string // 表名
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
logDebug bool // 日志开关
}
mongoConfig struct {
databaseName string // 库名
collectionName string // 表名
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
debug bool // 日志开关
}
}
// NewApiClient 创建接口实例化
@ -61,3 +70,35 @@ func NewApiClient(config *ApiClientConfig) (*ApiClient, error) {
return c, nil
}
// NewApiMongoClient 创建接口实例化
func NewApiMongoClient(mongoClient *dorm.MongoClient, databaseName string, collectionName string, debug bool) (*ApiClient, error) {
c := &ApiClient{}
if mongoClient.Db == nil {
return nil, errors.New("没有设置驱动")
}
c.mongoClient = mongoClient
if databaseName == "" {
return nil, errors.New("没有设置库名")
}
c.mongoConfig.databaseName = databaseName
if collectionName == "" {
return nil, errors.New("没有设置表名")
}
c.mongoConfig.collectionName = collectionName
c.mongoConfig.debug = debug
hostname, _ := os.Hostname()
c.mongoConfig.hostname = hostname
c.mongoConfig.insideIp = goip.GetInsideIp(context.Background())
c.mongoConfig.goVersion = runtime.Version()
return c, nil
}

@ -0,0 +1,128 @@
package golog
import (
"context"
"go.dtapp.net/dorm"
"go.dtapp.net/gorequest"
"go.dtapp.net/gotrace_id"
"go.dtapp.net/gourl"
"go.mongodb.org/mongo-driver/bson/primitive"
"time"
)
// 模型结构体
type apiMongolLog struct {
LogId primitive.ObjectID `json:"log_id,omitempty" bson:"_id,omitempty"` //【记录】编号
TraceId string `json:"trace_id,omitempty" bson:"trace_id,omitempty"` //【系统】跟踪编号
RequestTime time.Time `json:"request_time,omitempty" bson:"request_time,omitempty"` //【请求】时间
RequestUri string `json:"request_uri,omitempty" bson:"request_uri,omitempty"` //【请求】链接
RequestUrl string `json:"request_url,omitempty" bson:"request_url,omitempty"` //【请求】链接
RequestApi string `json:"request_api,omitempty" bson:"request_api,omitempty"` //【请求】接口
RequestMethod string `json:"request_method,omitempty" bson:"request_method,omitempty"` //【请求】方式
RequestParams interface{} `json:"request_params,omitempty" bson:"request_params,omitempty"` //【请求】参数
RequestHeader interface{} `json:"request_header,omitempty" bson:"request_header,omitempty"` //【请求】头部
ResponseHeader interface{} `json:"response_header,omitempty" bson:"response_header,omitempty"` //【返回】头部
ResponseStatusCode int `json:"response_status_code,omitempty" bson:"response_status_code,omitempty"` //【返回】状态码
ResponseBody interface{} `json:"response_body,omitempty" bson:"response_body,omitempty"` //【返回】内容
ResponseContentLength int64 `json:"response_content_length,omitempty" bson:"response_content_length,omitempty"` //【返回】大小
ResponseTime time.Time `json:"response_time,omitempty" bson:"response_time,omitempty"` //【返回】时间
SystemHostName string `json:"system_host_name,omitempty" bson:"system_host_name,omitempty"` //【系统】主机名
SystemInsideIp string `json:"system_inside_ip,omitempty" bson:"system_inside_ip,omitempty"` //【系统】内网ip
GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【程序】Go版本
SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【程序】Sdk版本
}
// 记录日志
func (c *ApiClient) mongoRecord(ctx context.Context, mongoLog apiMongolLog) error {
mongoLog.SystemHostName = c.mongoConfig.hostname
if mongoLog.SystemInsideIp == "" {
mongoLog.SystemInsideIp = c.mongoConfig.insideIp
}
mongoLog.GoVersion = c.mongoConfig.goVersion
mongoLog.TraceId = gotrace_id.GetTraceIdContext(ctx)
mongoLog.LogId = primitive.NewObjectID()
_, err := c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).InsertOne(mongoLog)
return err
}
// MongoQuery 查询
func (c *ApiClient) MongoQuery() *dorm.MongoClient {
return c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName)
}
// MongoMiddleware 中间件
func (c *ApiClient) MongoMiddleware(ctx context.Context, request gorequest.Response, sdkVersion string) {
err := c.mongoRecord(ctx, apiMongolLog{
RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: gourl.UriParse(request.RequestUri).Path, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: request.RequestParams, //【请求】参数
RequestHeader: request.RequestHeader, //【请求】头部
ResponseHeader: request.ResponseHeader, //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: request.ResponseBody, //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: request.ResponseTime, //【返回】时间
SdkVersion: sdkVersion, //【程序】Sdk版本
})
if err != nil {
if c.config.logDebug {
c.logClient.Errorf(ctx, "[log.MongoMiddleware]%s", err.Error())
}
}
}
// MongoMiddlewareXml 中间件
func (c *ApiClient) MongoMiddlewareXml(ctx context.Context, request gorequest.Response, sdkVersion string) {
err := c.mongoRecord(ctx, apiMongolLog{
RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: gourl.UriParse(request.RequestUri).Path, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: request.RequestParams, //【请求】参数
RequestHeader: request.RequestHeader, //【请求】头部
ResponseHeader: request.ResponseHeader, //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: dorm.XmlDecodeNoError(request.ResponseBody), //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: request.ResponseTime, //【返回】时间
SdkVersion: sdkVersion, //【程序】Sdk版本
})
if err != nil {
if c.config.logDebug {
c.logClient.Errorf(ctx, "[log.MongoMiddlewareXml]%s", err.Error())
}
}
}
// MongoMiddlewareCustom 中间件
func (c *ApiClient) MongoMiddlewareCustom(ctx context.Context, api string, request gorequest.Response, sdkVersion string) {
err := c.mongoRecord(ctx, apiMongolLog{
RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: api, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: request.RequestParams, //【请求】参数
RequestHeader: request.RequestHeader, //【请求】头部
ResponseHeader: request.ResponseHeader, //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: request.ResponseBody, //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: request.ResponseTime, //【返回】时间
SdkVersion: sdkVersion, //【程序】Sdk版本
})
if err != nil {
if c.config.logDebug {
c.logClient.Errorf(ctx, "[log.MongoMiddlewareCustom]%s", err.Error())
}
}
}

@ -1,5 +1,5 @@
package golog
const (
Version = "1.0.39"
Version = "1.0.40"
)

@ -141,17 +141,16 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp := "", "", "", "", ""
if c.ipService != nil {
// 判断是不是IPv4
if net.ParseIP(clientIp).To4() != nil {
// IPv4
_, info := c.ipService.Ipv4(clientIp)
requestClientIpCountry = info.Country
requestClientIpRegion = info.Region
requestClientIpProvince = info.Province
requestClientIpCity = info.City
requestClientIpIsp = info.ISP
}
// 判断是不是IPv6
if net.ParseIP(clientIp).To16() != nil {
} else if net.ParseIP(clientIp).To16() != nil {
// IPv6
info := c.ipService.Ipv6(clientIp)
requestClientIpCountry = info.Country
requestClientIpProvince = info.Province

@ -144,18 +144,17 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp := "", "", "", "", ""
if c.ipService != nil {
if net.ParseIP(ginCtx.ClientIP()).To4() != nil {
// 判断是不是IPV4
_, info := c.ipService.Ipv4(ginCtx.ClientIP())
if net.ParseIP(clientIp).To4() != nil {
// IPv4
_, info := c.ipService.Ipv4(clientIp)
requestClientIpCountry = info.Country
requestClientIpRegion = info.Region
requestClientIpProvince = info.Province
requestClientIpCity = info.City
requestClientIpIsp = info.ISP
}
if net.ParseIP(ginCtx.ClientIP()).To16() != nil {
// 判断是不是IPV6
info := c.ipService.Ipv6(ginCtx.ClientIP())
} else if net.ParseIP(clientIp).To16() != nil {
// IPv6
info := c.ipService.Ipv6(clientIp)
requestClientIpCountry = info.Country
requestClientIpProvince = info.Province
requestClientIpCity = info.City
@ -175,7 +174,7 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
}
if dataJson {
if c.mongoConfig.debug {
log.Printf("[golog.MongoMiddleware.gormRecord.json.request_body] %s\n", jsonBody)
log.Printf("[golog.MongoMiddleware.mongoRecord.json.request_body] %s\n", jsonBody)
}
err := c.mongoRecord(ginMongoLog{
TraceId: traceId, //【系统】跟踪编号
@ -203,13 +202,13 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
})
if err != nil {
if c.mongoConfig.debug {
c.logClient.Errorf(ctx, "[log.gormRecord] %s", err.Error())
log.Printf("[golog.MongoMiddleware.gormRecord.json] %s\n", err)
c.logClient.Errorf(ctx, "[log.mongoRecord] %s", err.Error())
log.Printf("[golog.MongoMiddleware.mongoRecord.json] %s\n", err)
}
}
} else {
if c.mongoConfig.debug {
log.Printf("[golog.MongoMiddleware.gormRecord.xml.request_body] %s\n", xmlBody)
log.Printf("[golog.MongoMiddleware.mongoRecord.xml.request_body] %s\n", xmlBody)
}
err := c.mongoRecord(ginMongoLog{
TraceId: traceId, //【系统】跟踪编号
@ -237,8 +236,8 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
})
if err != nil {
if c.mongoConfig.debug {
c.logClient.Errorf(ctx, "[log.gormRecord] %s", err.Error())
log.Printf("[golog.MongoMiddleware.gormRecord.xml] %s\n", err)
c.logClient.Errorf(ctx, "[log.mongoRecord] %s", err.Error())
log.Printf("[golog.MongoMiddleware.mongoRecord.xml] %s\n", err)
}
}
}

@ -8,7 +8,7 @@ require (
github.com/rs/zerolog v1.27.0
github.com/sirupsen/logrus v1.9.0
go.dtapp.net/dorm v1.0.29
go.dtapp.net/goip v1.0.27
go.dtapp.net/goip v1.0.28
go.dtapp.net/gojson v1.0.1
go.dtapp.net/gorequest v1.0.27
go.dtapp.net/gotime v1.0.5

@ -517,8 +517,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.29 h1:viOJ66S0x84dzSVUd9rHr4aGCziQupfrJGWPXI6GDAo=
go.dtapp.net/dorm v1.0.29/go.mod h1:4YgRRh77Y+zn5eitmID8h33kyGSKYuYIWRlo9oI2YxI=
go.dtapp.net/goip v1.0.27 h1:C12v7Hm2zKW+GkRc5qZsjL1YYnCNCkl5R3DvLDfKubc=
go.dtapp.net/goip v1.0.27/go.mod h1:ZqPPUvpOSzdtB/dEZFiaD0CBRZmvIzjDmm3XkpMC9Bo=
go.dtapp.net/goip v1.0.28 h1:wZt+wbzNhOLRUJnepG4c8HhUr4V9jNXOF6wJ1h7jUvU=
go.dtapp.net/goip v1.0.28/go.mod h1:ZqPPUvpOSzdtB/dEZFiaD0CBRZmvIzjDmm3XkpMC9Bo=
go.dtapp.net/gojson v1.0.1 h1:MHeSGlq1KxzL7rCkm18fhwW4GNORHohdDMmxY5PupKY=
go.dtapp.net/gojson v1.0.1/go.mod h1:TkkpTNxHBKxul0e7gC5MrL1K4ICFB9mQ7wHzjBah3/k=
go.dtapp.net/gorandom v1.0.1 h1:IWfMClh1ECPvyUjlqD7MwLq4mZdUusD1qAwAdsvEJBs=

Loading…
Cancel
Save