- add api ip

master v1.0.70
李光春 2 years ago
parent 105552f60e
commit a9ae30ac48

@ -16,6 +16,7 @@ type ApiClient struct {
mongoClient *dorm.MongoClient // 数据库驱动
zapLog *ZapLog // 日志服务
logDebug bool // 日志开关
currentIp string // 当前ip
gormConfig struct {
tableName string // 表名
insideIp string // 内网ip
@ -50,6 +51,7 @@ type ApiClientConfig struct {
MongoClientFun apiMongoClientFun // 日志配置
Debug bool // 日志开关
ZapLog *ZapLog // 日志服务
CurrentIp string // 当前ip
}
// NewApiClient 创建接口实例化
@ -65,6 +67,13 @@ func NewApiClient(config *ApiClientConfig) (*ApiClient, error) {
c.logDebug = config.Debug
if config.CurrentIp == "" {
config.CurrentIp = goip.GetOutsideIp(ctx)
}
if config.CurrentIp != "" && config.CurrentIp != "0.0.0.0" {
c.currentIp = config.CurrentIp
}
gormClient, gormTableName := config.GormClientFun()
mongoClient, mongoDatabaseName, mongoCollectionName := config.MongoClientFun()

@ -84,6 +84,7 @@ type apiPostgresqlLog struct {
RequestMethod string `gorm:"index;comment:【请求】方式" json:"request_method,omitempty"` //【请求】方式
RequestParams string `gorm:"comment:【请求】参数" json:"request_params,omitempty"` //【请求】参数
RequestHeader string `gorm:"comment:【请求】头部" json:"request_header,omitempty"` //【请求】头部
RequestIp string `gorm:"index;comment:【请求】请求Ip" json:"request_ip,omitempty"` //【请求】请求Ip
ResponseHeader string `gorm:"comment:【返回】头部" json:"response_header,omitempty"` //【返回】头部
ResponseStatusCode int `gorm:"index;comment:【返回】状态码" json:"response_status_code,omitempty"` //【返回】状态码
ResponseBody string `gorm:"comment:【返回】数据" json:"response_content,omitempty"` //【返回】数据
@ -108,6 +109,8 @@ func (c *ApiClient) gormRecord(ctx context.Context, postgresqlLog apiPostgresqlL
postgresqlLog.TraceId = gotrace_id.GetTraceIdContext(ctx)
postgresqlLog.RequestIp = c.currentIp
err = c.gormClient.Db.Table(c.gormConfig.tableName).Create(&postgresqlLog).Error
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[golog.api.gormRecord]%s", err)

@ -130,6 +130,7 @@ type apiMongolLog struct {
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"` //【请求】头部
RequestIp string `json:"request_ip,omitempty" bson:"request_ip,omitempty"` //【请求】请求Ip
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"` //【返回】内容
@ -152,6 +153,8 @@ func (c *ApiClient) mongoRecord(ctx context.Context, mongoLog apiMongolLog) (err
mongoLog.LogId = primitive.NewObjectID()
mongoLog.RequestIp = c.currentIp
_, err = c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).InsertOne(mongoLog)
if err != nil {
c.zapLog.WithTraceId(ctx).Sugar().Errorf("[golog.api.mongoRecord]%s", err)

@ -1,5 +1,5 @@
package golog
const (
Version = "1.0.69"
Version = "1.0.70"
)

Loading…
Cancel
Save