diff --git a/.gitignore b/.gitignore index 5c75b7d..c1823b5 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,3 @@ *.log gomod.sh *_test.go -/vendor/ diff --git a/api.go b/api.go index d635824..4a23013 100644 --- a/api.go +++ b/api.go @@ -34,6 +34,11 @@ type ApiClient struct { gorm bool // 日志开关 mongo bool // 日志开关 } + config struct { + os string // 系统类型 + arch string // 系统架构 + maxProCs int // CPU核数 + } } // client 数据库服务 @@ -74,6 +79,10 @@ func NewApiClient(config *ApiClientConfig) (*ApiClient, error) { c.currentIp = config.CurrentIp } + c.config.os = runtime.GOOS + c.config.arch = runtime.GOARCH + c.config.maxProCs = runtime.GOMAXPROCS(0) + gormClient, gormTableName := config.GormClientFun() mongoClient, mongoDatabaseName, mongoCollectionName := config.MongoClientFun() diff --git a/api_gorm.go b/api_gorm.go index a7e02c8..b9e8191 100644 --- a/api_gorm.go +++ b/api_gorm.go @@ -83,25 +83,28 @@ func (c *ApiClient) gormAutoMigrate() (err error) { // 模型结构体 type apiPostgresqlLog 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"` //【请求】方式 - 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"` //【返回】数据 - ResponseContentLength int64 `gorm:"comment:【返回】大小" json:"response_content_length,omitempty"` //【返回】大小 - ResponseTime time.Time `gorm:"index;comment:【返回】时间" json:"response_time,omitempty"` //【返回】时间 - SystemHostName string `gorm:"index;comment:【系统】主机名" json:"system_host_name,omitempty"` //【系统】主机名 - SystemInsideIp string `gorm:"index;comment:【系统】内网ip" json:"system_inside_ip,omitempty"` //【系统】内网ip - GoVersion string `gorm:"index;comment:【程序】Go版本" json:"go_version,omitempty"` //【程序】Go版本 - SdkVersion string `gorm:"index;comment:【程序】Sdk版本" json:"sdk_version,omitempty"` //【程序】Sdk版本 + 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"` //【请求】方式 + 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"` //【返回】数据 + ResponseContentLength int64 `gorm:"comment:【返回】大小" json:"response_content_length,omitempty"` //【返回】大小 + ResponseTime time.Time `gorm:"index;comment:【返回】时间" json:"response_time,omitempty"` //【返回】时间 + SystemHostName string `gorm:"index;comment:【系统】主机名" json:"system_host_name,omitempty"` //【系统】主机名 + SystemInsideIp string `gorm:"index;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"` //【系统】系统架构 + SystemCpuQuantity int `gorm:"index;comment:【系统】CPU核数" json:"system_cpu_quantity,omitempty"` //【系统】CPU核数 + GoVersion string `gorm:"index;comment:【程序】Go版本" json:"go_version,omitempty"` //【程序】Go版本 + SdkVersion string `gorm:"index;comment:【程序】Sdk版本" json:"sdk_version,omitempty"` //【程序】Sdk版本 } // 记录日志 @@ -119,6 +122,10 @@ func (c *ApiClient) gormRecord(ctx context.Context, postgresqlLog apiPostgresqlL postgresqlLog.RequestIp = c.currentIp + postgresqlLog.SystemOs = c.config.os + postgresqlLog.SystemArch = c.config.arch + postgresqlLog.SystemCpuQuantity = c.config.maxProCs + 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) diff --git a/api_mongo.go b/api_mongo.go index f2ca6cd..2966b32 100644 --- a/api_mongo.go +++ b/api_mongo.go @@ -119,6 +119,15 @@ func (c *ApiClient) mongoCreateIndexes() { c.zapLog.WithLogger().Sugar().Infof(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ {"system_inside_ip", 1}, }})) + c.zapLog.WithLogger().Sugar().Infof(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"system_os", -1}, + }})) + c.zapLog.WithLogger().Sugar().Infof(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"system_arch", -1}, + }})) + c.zapLog.WithLogger().Sugar().Infof(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ + {"system_cpu_quantity", 1}, + }})) c.zapLog.WithLogger().Sugar().Infof(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{ {"go_version", -1}, }})) @@ -146,6 +155,9 @@ type apiMongolLog struct { ResponseTime primitive.DateTime `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 + SystemOs string `json:"system_os,omitempty" bson:"system_os,omitempty"` //【系统】系统类型 + SystemArch string `json:"system_arch,omitempty" bson:"system_arch,omitempty"` //【系统】系统架构 + SystemCpuQuantity int `json:"system_cpu_quantity,omitempty" bson:"system_cpu_quantity,omitempty"` //【系统】CPU核数 GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【程序】Go版本 SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【程序】Sdk版本 } @@ -159,10 +171,14 @@ func (c *ApiClient) mongoRecord(ctx context.Context, mongoLog apiMongolLog) (err mongoLog.TraceId = gotrace_id.GetTraceIdContext(ctx) - mongoLog.LogId = primitive.NewObjectID() - mongoLog.RequestIp = c.currentIp + mongoLog.SystemOs = c.config.os + mongoLog.SystemArch = c.config.arch + mongoLog.SystemCpuQuantity = c.config.maxProCs + + mongoLog.LogId = primitive.NewObjectID() + _, 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) diff --git a/const.go b/const.go index 6610869..ba1bdf5 100644 --- a/const.go +++ b/const.go @@ -1,5 +1,5 @@ package golog const ( - Version = "1.0.71" + Version = "1.0.72" ) diff --git a/gin.go b/gin.go index 1126b71..6e8e773 100644 --- a/gin.go +++ b/gin.go @@ -41,6 +41,11 @@ type GinClient struct { gorm bool // 日志开关 mongo bool // 日志开关 } + config struct { + os string // 系统类型 + arch string // 系统架构 + maxProCs int // CPU核数 + } } // client 数据库服务 @@ -75,6 +80,10 @@ func NewGinClient(config *GinClientConfig) (*GinClient, error) { c.logDebug = config.Debug + c.config.os = runtime.GOOS + c.config.arch = runtime.GOARCH + c.config.maxProCs = runtime.GOMAXPROCS(0) + gormClient, gormTableName := config.GormClientFun() mongoClient, mongoDatabaseName, mongoCollectionName := config.MongoClientFun() diff --git a/gin_gorm.go b/gin_gorm.go index eb38087..6848caf 100644 --- a/gin_gorm.go +++ b/gin_gorm.go @@ -110,6 +110,9 @@ type ginPostgresqlLog struct { CostTime int64 `gorm:"comment:【系统】花费时间" json:"cost_time,omitempty"` //【系统】花费时间 SystemHostName string `gorm:"index;comment:【系统】主机名" json:"system_host_name,omitempty"` //【系统】主机名 SystemInsideIp string `gorm:"index;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"` //【系统】系统架构 + SystemCpuQuantity int `gorm:"index;comment:【系统】CPU核数" json:"system_cpu_quantity,omitempty"` //【系统】CPU核数 GoVersion string `gorm:"index;comment:【程序】Go版本" json:"go_version,omitempty"` //【程序】Go版本 SdkVersion string `gorm:"index;comment:【程序】Sdk版本" json:"sdk_version,omitempty"` //【程序】Sdk版本 } @@ -123,6 +126,10 @@ func (c *GinClient) gormRecord(postgresqlLog ginPostgresqlLog) (err error) { postgresqlLog.SdkVersion = Version + postgresqlLog.SystemOs = c.config.os + postgresqlLog.SystemArch = c.config.arch + postgresqlLog.SystemCpuQuantity = c.config.maxProCs + err = c.gormClient.Db.Table(c.gormConfig.tableName).Create(&postgresqlLog).Error if err != nil { c.zapLog.WithTraceIdStr(postgresqlLog.TraceId).Sugar().Errorf("[golog.gin.gormRecord]:%s", err) diff --git a/gin_mongo.go b/gin_mongo.go index 2c9a3c8..8c2e3b7 100644 --- a/gin_mongo.go +++ b/gin_mongo.go @@ -157,6 +157,18 @@ func (c *GinClient) mongoCreateIndexes() { Keys: bson.D{ {"system_inside_ip", 1}, }})) + c.zapLog.WithLogger().Sugar().Infof(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"system_os", -1}, + }})) + c.zapLog.WithLogger().Sugar().Infof(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"system_arch", -1}, + }})) + c.zapLog.WithLogger().Sugar().Infof(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ + Keys: bson.D{ + {"system_cpu_quantity", 1}, + }})) c.zapLog.WithLogger().Sugar().Infof(c.mongoClient.Db.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).Indexes().CreateOne(context.TODO(), mongo.IndexModel{ Keys: bson.D{ {"go_version", -1}, @@ -195,6 +207,9 @@ type ginMongoLog struct { CostTime int64 `json:"cost_time,omitempty" bson:"cost_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 + SystemOs string `json:"system_os,omitempty" bson:"system_os,omitempty"` //【系统】系统类型 + SystemArch string `json:"system_arch,omitempty" bson:"system_arch,omitempty"` //【系统】系统架构 + SystemCpuQuantity int `json:"system_cpu_quantity,omitempty" bson:"system_cpu_quantity,omitempty"` //【系统】CPU核数 GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【程序】Go版本 SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【程序】Sdk版本 } @@ -208,6 +223,10 @@ func (c *GinClient) mongoRecord(mongoLog ginMongoLog) (err error) { mongoLog.SdkVersion = Version + mongoLog.SystemOs = c.config.os + mongoLog.SystemArch = c.config.arch + mongoLog.SystemCpuQuantity = c.config.maxProCs + mongoLog.LogId = primitive.NewObjectID() _, err = c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).InsertOne(mongoLog) diff --git a/go.mod b/go.mod index f467497..b4f5a0d 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/natefinch/lumberjack v2.0.0+incompatible go.dtapp.net/dorm v1.0.32 go.dtapp.net/goip v1.0.29 - go.dtapp.net/gorequest v1.0.28 + go.dtapp.net/gorequest v1.0.29 go.dtapp.net/gotime v1.0.5 go.dtapp.net/gotrace_id v1.0.6 go.dtapp.net/gourl v1.0.0 @@ -76,9 +76,9 @@ require ( go.uber.org/multierr v1.8.0 // indirect golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect - golang.org/x/net v0.0.0-20220907135653-1e95f45603a7 // indirect + golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect golang.org/x/sync v0.0.0-20220907140024-f12130a52804 // indirect - golang.org/x/sys v0.0.0-20220907062415-87db552b00fd // indirect + golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 // indirect golang.org/x/text v0.3.7 // indirect google.golang.org/protobuf v1.28.1 // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect diff --git a/go.sum b/go.sum index f1f5f14..5c10028 100644 --- a/go.sum +++ b/go.sum @@ -501,8 +501,8 @@ go.dtapp.net/goip v1.0.29 h1:yM+9m70EytQr8MzewlnQmb6tcNtADcrlt0Wx6Z2FPZQ= go.dtapp.net/goip v1.0.29/go.mod h1:ewEUMDUCcNwSUu6l10tnjcSMTCYfjNgxUFRZ1GDHxEs= go.dtapp.net/gorandom v1.0.1 h1:IWfMClh1ECPvyUjlqD7MwLq4mZdUusD1qAwAdsvEJBs= go.dtapp.net/gorandom v1.0.1/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8= -go.dtapp.net/gorequest v1.0.28 h1:EehFtF5WcvyZ9rB6MAQoI2kJkhR8Wv8tVgpoxXcb3sI= -go.dtapp.net/gorequest v1.0.28/go.mod h1:Nr0BIOXhapY3Es86hL7pN2c2PgjZwImcrWdMpLzHlJE= +go.dtapp.net/gorequest v1.0.29 h1:3crr5h+gFFM/X5/BjY1qf8AY3Lzytnx2Bb70l0TznvM= +go.dtapp.net/gorequest v1.0.29/go.mod h1:bRayjDYWr3Cmj+lXkz6HFHUzbwIxhCloPYdckImI8YY= go.dtapp.net/gostring v1.0.10 h1:eG+1kQehdJUitj9Hfwy79SndMHYOB7ABpWkTs7mDGeQ= go.dtapp.net/gostring v1.0.10/go.mod h1:L4kREy89a9AraMHB5tUjjl+5rxP1gpXkDouRKKuzT50= go.dtapp.net/gotime v1.0.5 h1:12aNgB2ULpP6QgQHEUkLilZ4ASvhpFxMFQkBwn0par8= @@ -592,8 +592,8 @@ golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220907135653-1e95f45603a7 h1:1WGATo9HAhkWMbfyuVU0tEFP88OIkUvwaHFveQPvzCQ= -golang.org/x/net v0.0.0-20220907135653-1e95f45603a7/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/net v0.0.0-20220909164309-bea034e7d591 h1:D0B/7al0LLrVC8aWF4+oxpv/m8bc7ViFfVS8/gXGdqI= +golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -639,8 +639,8 @@ golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220907062415-87db552b00fd h1:AZeIEzg+8RCELJYq8w+ODLVxFgLMMigSwO/ffKPEd9U= -golang.org/x/sys v0.0.0-20220907062415-87db552b00fd/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 h1:wM1k/lXfpc5HdkJJyW9GELpd8ERGdnh8sMGL6Gzq3Ho= +golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=