parent
dffb6465a9
commit
0e2a64323d
@ -1,156 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/goip"
|
||||
"go.dtapp.net/gorequest"
|
||||
)
|
||||
|
||||
// ApiClientFun *ApiClient 驱动
|
||||
type ApiClientFun func() *ApiClient
|
||||
|
||||
// ApiClient 接口
|
||||
type ApiClient struct {
|
||||
gormClient *dorm.GormClient // 数据库驱动
|
||||
mongoClient *dorm.MongoClient // 数据库驱动
|
||||
zapLog *ZapLog // 日志服务
|
||||
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版本
|
||||
mongoVersion string // mongo版本
|
||||
mongoSdkVersion string // mongo sdk版本
|
||||
}
|
||||
gormConfig struct {
|
||||
stats bool // 状态
|
||||
tableName string // 表名
|
||||
}
|
||||
mongoConfig struct {
|
||||
stats bool // 状态
|
||||
databaseName string // 库名
|
||||
collectionName string // 表名
|
||||
}
|
||||
}
|
||||
|
||||
// ApiClientConfig 接口实例配置
|
||||
type ApiClientConfig struct {
|
||||
GormClientFun dorm.GormClientTableFun // 日志配置
|
||||
MongoClientFun dorm.MongoClientCollectionFun // 日志配置
|
||||
ZapLog *ZapLog // 日志服务
|
||||
CurrentIp string // 当前ip
|
||||
}
|
||||
|
||||
// NewApiClient 创建接口实例化
|
||||
func NewApiClient(config *ApiClientConfig) (*ApiClient, error) {
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
c := &ApiClient{}
|
||||
|
||||
c.zapLog = config.ZapLog
|
||||
|
||||
if config.CurrentIp != "" && config.CurrentIp != "0.0.0.0" {
|
||||
c.config.systemOutsideIp = config.CurrentIp
|
||||
}
|
||||
c.config.systemOutsideIp = goip.IsIp(c.config.systemOutsideIp)
|
||||
if c.config.systemOutsideIp == "" {
|
||||
return nil, currentIpNoConfig
|
||||
}
|
||||
|
||||
// 配置信息
|
||||
c.setConfig(ctx)
|
||||
|
||||
gormClient, gormTableName := config.GormClientFun()
|
||||
mongoClient, mongoDatabaseName, mongoCollectionName := config.MongoClientFun()
|
||||
|
||||
if (gormClient == nil || gormClient.GetDb() == nil) || (mongoClient == nil || mongoClient.GetDb() == nil) {
|
||||
return nil, dbClientFunNoConfig
|
||||
}
|
||||
|
||||
// 配置关系数据库
|
||||
if gormClient != nil || gormClient.GetDb() != nil {
|
||||
|
||||
c.gormClient = gormClient
|
||||
|
||||
if gormTableName == "" {
|
||||
return nil, errors.New("没有设置表名")
|
||||
} else {
|
||||
c.gormConfig.tableName = gormTableName
|
||||
}
|
||||
|
||||
// 创建模型
|
||||
c.gormAutoMigrate(ctx)
|
||||
|
||||
c.gormConfig.stats = true
|
||||
}
|
||||
|
||||
// 配置非关系数据库
|
||||
if mongoClient != nil || mongoClient.GetDb() != nil {
|
||||
|
||||
c.mongoClient = mongoClient
|
||||
|
||||
if mongoDatabaseName == "" {
|
||||
return nil, errors.New("没有设置库名")
|
||||
} else {
|
||||
c.mongoConfig.databaseName = mongoDatabaseName
|
||||
}
|
||||
|
||||
if mongoCollectionName == "" {
|
||||
return nil, errors.New("没有设置表名")
|
||||
} else {
|
||||
c.mongoConfig.collectionName = mongoCollectionName
|
||||
}
|
||||
|
||||
// 创建时间序列集合
|
||||
c.mongoCreateCollection(ctx)
|
||||
|
||||
// 创建索引
|
||||
c.mongoCreateIndexes(ctx)
|
||||
|
||||
c.mongoConfig.stats = true
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// Middleware 中间件
|
||||
func (c *ApiClient) Middleware(ctx context.Context, request gorequest.Response, sdkVersion string) {
|
||||
if c.gormConfig.stats {
|
||||
c.gormMiddleware(ctx, request, sdkVersion)
|
||||
}
|
||||
if c.mongoConfig.stats {
|
||||
c.mongoMiddleware(ctx, request, sdkVersion)
|
||||
}
|
||||
}
|
||||
|
||||
// MiddlewareXml 中间件
|
||||
func (c *ApiClient) MiddlewareXml(ctx context.Context, request gorequest.Response, sdkVersion string) {
|
||||
if c.gormConfig.stats {
|
||||
c.gormMiddlewareXml(ctx, request, sdkVersion)
|
||||
}
|
||||
if c.mongoConfig.stats {
|
||||
c.mongoMiddlewareXml(ctx, request, sdkVersion)
|
||||
}
|
||||
}
|
||||
|
||||
// MiddlewareCustom 中间件
|
||||
func (c *ApiClient) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response, sdkVersion string) {
|
||||
if c.gormConfig.stats {
|
||||
c.gormMiddlewareCustom(ctx, api, request, sdkVersion)
|
||||
}
|
||||
if c.mongoConfig.stats {
|
||||
c.mongoMiddlewareCustom(ctx, api, request, sdkVersion)
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/goip"
|
||||
"go.mongodb.org/mongo-driver/version"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func (c *ApiClient) setConfig(ctx context.Context) {
|
||||
|
||||
info := getSystem()
|
||||
|
||||
c.config.systemHostname = info.SystemHostname
|
||||
c.config.systemOs = info.SystemOs
|
||||
c.config.systemVersion = info.SystemVersion
|
||||
c.config.systemKernel = info.SystemKernel
|
||||
c.config.systemKernelVersion = info.SystemKernelVersion
|
||||
c.config.systemBootTime = info.SystemBootTime
|
||||
c.config.cpuCores = info.CpuCores
|
||||
c.config.cpuModelName = info.CpuModelName
|
||||
c.config.cpuMhz = info.CpuMhz
|
||||
|
||||
c.config.systemInsideIp = goip.GetInsideIp(ctx)
|
||||
|
||||
c.config.sdkVersion = Version
|
||||
c.config.goVersion = runtime.Version()
|
||||
|
||||
c.config.mongoSdkVersion = version.Driver
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/goip"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func (gl *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
|
||||
|
||||
gl.config.systemInsideIp = goip.GetInsideIp(ctx)
|
||||
gl.config.systemOutsideIp = systemOutsideIp
|
||||
|
||||
gl.config.goVersion = runtime.Version()
|
||||
gl.config.sdkVersion = Version
|
||||
|
||||
}
|
@ -0,0 +1,155 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/gorequest"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.dtapp.net/gotrace_id"
|
||||
"go.dtapp.net/gourl"
|
||||
"log"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// 模型
|
||||
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:"default:0.0.0.0;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_body,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:"default:0.0.0.0;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 (gl *ApiGorm) gormAutoMigrate(ctx context.Context) {
|
||||
err := gl.gormClient.GetDb().Table(gl.gormConfig.tableName).AutoMigrate(&apiPostgresqlLog{})
|
||||
if err != nil {
|
||||
log.Printf("创建模型:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 记录日志
|
||||
func (gl *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.TraceID = gotrace_id.GetTraceIdContext(ctx) //【记录】跟踪编号
|
||||
data.RequestIp = gl.config.systemOutsideIp //【请求】请求Ip
|
||||
data.SystemOs = gl.config.systemOs //【系统】系统类型
|
||||
data.SystemArch = gl.config.systemKernel //【系统】系统架构
|
||||
|
||||
err := gl.gormClient.GetDb().Table(gl.gormConfig.tableName).Create(&data).Error
|
||||
if err != nil {
|
||||
log.Printf("记录接口日志错误:%s", err)
|
||||
log.Printf("记录接口日志数据:%+v", data)
|
||||
}
|
||||
}
|
||||
|
||||
// GormDelete 删除
|
||||
func (gl *ApiGorm) GormDelete(ctx context.Context, hour int64) error {
|
||||
return gl.GormCustomTableDelete(ctx, gl.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
|
||||
if err != nil {
|
||||
log.Printf("删除失败:%s", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 中间件
|
||||
func (gl *ApiGorm) gormMiddleware(ctx context.Context, request gorequest.Response) {
|
||||
data := apiPostgresqlLog{
|
||||
RequestTime: request.RequestTime, //【请求】时间
|
||||
RequestUri: request.RequestUri, //【请求】链接
|
||||
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
|
||||
RequestApi: gourl.UriParse(request.RequestUri).Path, //【请求】接口
|
||||
RequestMethod: request.RequestMethod, //【请求】方式
|
||||
RequestParams: dorm.JsonEncodeNoError(request.RequestParams), //【请求】参数
|
||||
RequestHeader: dorm.JsonEncodeNoError(request.RequestHeader), //【请求】头部
|
||||
ResponseHeader: dorm.JsonEncodeNoError(request.ResponseHeader), //【返回】头部
|
||||
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
|
||||
ResponseContentLength: request.ResponseContentLength, //【返回】大小
|
||||
ResponseTime: request.ResponseTime, //【返回】时间
|
||||
}
|
||||
if !request.HeaderIsImg() {
|
||||
if len(request.ResponseBody) > 0 {
|
||||
data.ResponseBody = dorm.JsonEncodeNoError(dorm.JsonDecodeNoError(request.ResponseBody)) //【返回】数据
|
||||
}
|
||||
}
|
||||
|
||||
gl.gormRecord(ctx, data)
|
||||
}
|
||||
|
||||
// 中间件
|
||||
func (gl *ApiGorm) gormMiddlewareXml(ctx context.Context, request gorequest.Response) {
|
||||
data := apiPostgresqlLog{
|
||||
RequestTime: request.RequestTime, //【请求】时间
|
||||
RequestUri: request.RequestUri, //【请求】链接
|
||||
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
|
||||
RequestApi: gourl.UriParse(request.RequestUri).Path, //【请求】接口
|
||||
RequestMethod: request.RequestMethod, //【请求】方式
|
||||
RequestParams: dorm.JsonEncodeNoError(request.RequestParams), //【请求】参数
|
||||
RequestHeader: dorm.JsonEncodeNoError(request.RequestHeader), //【请求】头部
|
||||
ResponseHeader: dorm.JsonEncodeNoError(request.ResponseHeader), //【返回】头部
|
||||
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
|
||||
ResponseContentLength: request.ResponseContentLength, //【返回】大小
|
||||
ResponseTime: request.ResponseTime, //【返回】时间
|
||||
}
|
||||
if !request.HeaderIsImg() {
|
||||
if len(request.ResponseBody) > 0 {
|
||||
data.ResponseBody = dorm.XmlEncodeNoError(dorm.XmlDecodeNoError(request.ResponseBody)) //【返回】内容
|
||||
}
|
||||
}
|
||||
|
||||
gl.gormRecord(ctx, data)
|
||||
}
|
||||
|
||||
// 中间件
|
||||
func (gl *ApiGorm) gormMiddlewareCustom(ctx context.Context, api string, request gorequest.Response) {
|
||||
data := apiPostgresqlLog{
|
||||
RequestTime: request.RequestTime, //【请求】时间
|
||||
RequestUri: request.RequestUri, //【请求】链接
|
||||
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
|
||||
RequestApi: api, //【请求】接口
|
||||
RequestMethod: request.RequestMethod, //【请求】方式
|
||||
RequestParams: dorm.JsonEncodeNoError(request.RequestParams), //【请求】参数
|
||||
RequestHeader: dorm.JsonEncodeNoError(request.RequestHeader), //【请求】头部
|
||||
ResponseHeader: dorm.JsonEncodeNoError(request.ResponseHeader), //【返回】头部
|
||||
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
|
||||
ResponseContentLength: request.ResponseContentLength, //【返回】大小
|
||||
ResponseTime: request.ResponseTime, //【返回】时间
|
||||
}
|
||||
if !request.HeaderIsImg() {
|
||||
if len(request.ResponseBody) > 0 {
|
||||
data.ResponseBody = dorm.JsonEncodeNoError(dorm.JsonDecodeNoError(request.ResponseBody)) //【返回】数据
|
||||
}
|
||||
}
|
||||
|
||||
gl.gormRecord(ctx, data)
|
||||
}
|
@ -1,181 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/gorequest"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.dtapp.net/gotrace_id"
|
||||
"go.dtapp.net/gourl"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// 模型结构体
|
||||
type apiMongolLog struct {
|
||||
LogId primitive.ObjectID `json:"log_id,omitempty" bson:"_id,omitempty"` //【记录】编号
|
||||
LogTime primitive.DateTime `json:"log_time,omitempty" bson:"log_time"` //【记录】时间
|
||||
TraceId string `json:"trace_id,omitempty" bson:"trace_id,omitempty"` //【记录】跟踪编号
|
||||
RequestTime string `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 string `json:"response_time,omitempty" bson:"response_time,omitempty"` //【返回】时间
|
||||
System struct {
|
||||
Hostname string `json:"hostname" bson:"hostname"` //【系统】主机名
|
||||
Os string `json:"os" bson:"os"` //【系统】系统类型
|
||||
Version string `json:"version" bson:"version"` //【系统】系统版本
|
||||
Kernel string `json:"kernel" bson:"kernel"` //【系统】系统内核
|
||||
KernelVersion string `json:"kernel_version" bson:"kernel_version"` //【系统】系统内核版本
|
||||
BootTime string `json:"boot_time" bson:"boot_time"` //【系统】系统开机时间
|
||||
CpuCores int `json:"cpu_cores,omitempty" bson:"cpu_cores,omitempty"` //【系统】CPU核数
|
||||
CpuModelName string `json:"cpu_model_name,omitempty" bson:"cpu_model_name,omitempty"` //【系统】CPU型号名称
|
||||
CpuMhz float64 `json:"cpu_mhz,omitempty" bson:"cpu_mhz,omitempty"` //【系统】CPU兆赫
|
||||
InsideIp string `json:"inside_ip" bson:"inside_ip"` //【系统】内网ip
|
||||
OutsideIp string `json:"outside_ip" bson:"outside_ip"` //【系统】外网ip
|
||||
GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【系统】go版本
|
||||
SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【系统】sdk版本
|
||||
MongoVersion string `json:"mongo_version,omitempty" bson:"mongo_version,omitempty"` //【系统】mongo版本
|
||||
MongoSdkVersion string `json:"mongo_sdk_version,omitempty" bson:"mongo_sdk_version,omitempty"` //【系统】mongo sdk版本
|
||||
} `json:"system,omitempty" bson:"system,omitempty"` //【系统】信息
|
||||
}
|
||||
|
||||
// 创建时间序列集合
|
||||
func (c *ApiClient) mongoCreateCollection(ctx context.Context) {
|
||||
err := c.mongoClient.Database(c.mongoConfig.databaseName).CreateCollection(ctx, c.mongoConfig.collectionName, options.CreateCollection().SetTimeSeriesOptions(options.TimeSeries().SetTimeField("log_time")))
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("创建时间序列集合:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 创建索引
|
||||
func (c *ApiClient) mongoCreateIndexes(ctx context.Context) {
|
||||
_, err := c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).CreateManyIndexes(ctx, []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{
|
||||
Key: "log_time",
|
||||
Value: -1,
|
||||
}},
|
||||
}})
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("创建索引:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// MongoDelete 删除
|
||||
func (c *ApiClient) MongoDelete(ctx context.Context, hour int64) (*mongo.DeleteResult, error) {
|
||||
filter := bson.D{{"log_time", bson.D{{"$lt", primitive.NewDateTimeFromTime(gotime.Current().BeforeHour(hour).Time)}}}}
|
||||
return c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).DeleteMany(ctx, filter)
|
||||
}
|
||||
|
||||
// 记录日志
|
||||
func (c *ApiClient) mongoRecord(ctx context.Context, data apiMongolLog, sdkVersion string) {
|
||||
|
||||
data.LogId = primitive.NewObjectID() //【记录】编号
|
||||
data.TraceId = gotrace_id.GetTraceIdContext(ctx) //【记录】跟踪编号
|
||||
data.System.Hostname = c.config.systemHostname //【系统】主机名
|
||||
data.System.Os = c.config.systemOs //【系统】系统类型
|
||||
data.System.Version = c.config.systemVersion //【系统】系统版本
|
||||
data.System.Kernel = c.config.systemKernel //【系统】系统内核
|
||||
data.System.KernelVersion = c.config.systemKernelVersion //【系统】系统内核版本
|
||||
data.System.BootTime = gotime.SetCurrent(gotime.SetCurrentUnix(int64(c.config.systemBootTime)).Time).Format() //【系统】系统开机时间
|
||||
data.System.CpuCores = c.config.cpuCores //【系统】CPU核数
|
||||
data.System.CpuModelName = c.config.cpuModelName //【系统】CPU型号名称
|
||||
data.System.CpuMhz = c.config.cpuMhz //【程序】CPU兆赫
|
||||
data.System.InsideIp = c.config.systemInsideIp //【系统】内网ip
|
||||
data.System.OutsideIp = c.config.systemOutsideIp //【系统】外网ip
|
||||
data.System.GoVersion = c.config.goVersion //【系统】Go版本
|
||||
data.System.SdkVersion = sdkVersion //【系统】Sdk版本
|
||||
data.System.MongoVersion = c.config.mongoVersion //【系统】mongo版本
|
||||
data.System.MongoSdkVersion = c.config.mongoSdkVersion //【系统】mongo sdk版本
|
||||
|
||||
_, err := c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).InsertOne(ctx, data)
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存接口日志错误:%s", err)
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("保存接口日志数据:%+v", data)
|
||||
}
|
||||
}
|
||||
|
||||
// 中间件
|
||||
func (c *ApiClient) mongoMiddleware(ctx context.Context, request gorequest.Response, sdkVersion string) {
|
||||
data := apiMongolLog{
|
||||
LogTime: primitive.NewDateTimeFromTime(request.RequestTime), //【记录】时间
|
||||
RequestTime: gotime.SetCurrent(request.RequestTime).Format(), //【请求】时间
|
||||
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, //【返回】状态码
|
||||
ResponseContentLength: request.ResponseContentLength, //【返回】大小
|
||||
ResponseTime: gotime.SetCurrent(request.ResponseTime).Format(), //【返回】时间
|
||||
}
|
||||
if !request.HeaderIsImg() {
|
||||
if len(request.ResponseBody) > 0 {
|
||||
data.ResponseBody = dorm.JsonDecodeNoError(request.ResponseBody) //【返回】内容
|
||||
}
|
||||
}
|
||||
|
||||
c.mongoRecord(ctx, data, sdkVersion)
|
||||
}
|
||||
|
||||
// 中间件
|
||||
func (c *ApiClient) mongoMiddlewareXml(ctx context.Context, request gorequest.Response, sdkVersion string) {
|
||||
data := apiMongolLog{
|
||||
LogTime: primitive.NewDateTimeFromTime(request.RequestTime), //【记录】时间
|
||||
RequestTime: gotime.SetCurrent(request.RequestTime).Format(), //【请求】时间
|
||||
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, //【返回】状态码
|
||||
ResponseContentLength: request.ResponseContentLength, //【返回】大小
|
||||
ResponseTime: gotime.SetCurrent(request.ResponseTime).Format(), //【返回】时间
|
||||
}
|
||||
if !request.HeaderIsImg() {
|
||||
if len(request.ResponseBody) > 0 {
|
||||
data.ResponseBody = dorm.XmlDecodeNoError(request.ResponseBody) //【返回】内容
|
||||
}
|
||||
}
|
||||
|
||||
c.mongoRecord(ctx, data, sdkVersion)
|
||||
}
|
||||
|
||||
// 中间件
|
||||
func (c *ApiClient) mongoMiddlewareCustom(ctx context.Context, api string, request gorequest.Response, sdkVersion string) {
|
||||
data := apiMongolLog{
|
||||
LogTime: primitive.NewDateTimeFromTime(request.RequestTime), //【记录】时间
|
||||
RequestTime: gotime.SetCurrent(request.RequestTime).Format(), //【请求】时间
|
||||
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, //【返回】状态码
|
||||
ResponseContentLength: request.ResponseContentLength, //【返回】大小
|
||||
ResponseTime: gotime.SetCurrent(request.ResponseTime).Format(), //【返回】时间
|
||||
}
|
||||
if !request.HeaderIsImg() {
|
||||
if len(request.ResponseBody) > 0 {
|
||||
data.ResponseBody = dorm.JsonDecodeNoError(request.ResponseBody) //【返回】内容
|
||||
}
|
||||
}
|
||||
|
||||
c.mongoRecord(ctx, data, sdkVersion)
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// ApiSLog 接口日志
|
||||
type ApiSLog struct {
|
||||
slog struct {
|
||||
status bool // 状态
|
||||
client *SLog // 日志服务
|
||||
}
|
||||
}
|
||||
|
||||
// ApiSLogFun 接口日志驱动
|
||||
type ApiSLogFun func() *ApiSLog
|
||||
|
||||
func NewApiSlog(ctx context.Context) *ApiSLog {
|
||||
sl := &ApiSLog{}
|
||||
return sl
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package golog
|
||||
|
||||
// ConfigSLogClientFun 日志配置
|
||||
func (sl *ApiSLog) ConfigSLogClientFun(sLogFun SLogFun) {
|
||||
sLog := sLogFun()
|
||||
if sLog != nil {
|
||||
sl.slog.client = sLog
|
||||
sl.slog.status = true
|
||||
}
|
||||
}
|
||||
|
||||
// ConfigSLogResultClientFun 日志配置然后返回
|
||||
func (sl *ApiSLog) ConfigSLogResultClientFun(sLogFun SLogFun) *ApiSLog {
|
||||
sLog := sLogFun()
|
||||
if sLog != nil {
|
||||
sl.slog.client = sLog
|
||||
sl.slog.status = true
|
||||
}
|
||||
return sl
|
||||
}
|
@ -0,0 +1,125 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/gorequest"
|
||||
"go.dtapp.net/gotrace_id"
|
||||
"go.dtapp.net/gourl"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 结构体
|
||||
type apiSLog struct {
|
||||
TraceID string `json:"trace_id,omitempty"`
|
||||
RequestTime time.Time `json:"request_time,omitempty"`
|
||||
RequestUri string `json:"request_uri,omitempty"`
|
||||
RequestUrl string `json:"request_url,omitempty"`
|
||||
RequestApi string `json:"request_api,omitempty"`
|
||||
RequestMethod string `json:"request_method,omitempty"`
|
||||
RequestParams map[string]interface{} `json:"request_params,omitempty"`
|
||||
RequestHeader map[string]string `json:"request_header,omitempty"`
|
||||
ResponseHeader map[string][]string `json:"response_header,omitempty"`
|
||||
ResponseStatusCode int `json:"response_status_code,omitempty"`
|
||||
ResponseBody map[string]interface{} `json:"response_body,omitempty"`
|
||||
ResponseTime time.Time `json:"response_time,omitempty,omitempty"`
|
||||
}
|
||||
|
||||
// Middleware 中间件
|
||||
func (sl *ApiSLog) Middleware(ctx context.Context, request gorequest.Response) {
|
||||
data := apiSLog{
|
||||
TraceID: gotrace_id.GetTraceIdContext(ctx),
|
||||
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.JsonDecodeNoError(request.ResponseBody),
|
||||
ResponseTime: request.ResponseTime,
|
||||
}
|
||||
if sl.slog.status {
|
||||
sl.slog.client.WithTraceId(ctx).Info("Middleware",
|
||||
"request_time", data.RequestTime,
|
||||
"request_uri", data.RequestUri,
|
||||
"request_url", data.RequestUrl,
|
||||
"request_api", data.RequestApi,
|
||||
"request_method", data.RequestMethod,
|
||||
"request_params", data.RequestParams,
|
||||
"request_header", data.RequestHeader,
|
||||
"response_header", data.ResponseHeader,
|
||||
"response_status_code", data.ResponseStatusCode,
|
||||
"response_body", data.ResponseBody,
|
||||
"response_time", data.ResponseTime,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MiddlewareXml 中间件
|
||||
func (sl *ApiSLog) MiddlewareXml(ctx context.Context, request gorequest.Response) {
|
||||
data := apiSLog{
|
||||
TraceID: gotrace_id.GetTraceIdContext(ctx),
|
||||
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),
|
||||
ResponseTime: request.ResponseTime,
|
||||
}
|
||||
if sl.slog.status {
|
||||
sl.slog.client.WithTraceId(ctx).Info("MiddlewareXml",
|
||||
"request_time", data.RequestTime,
|
||||
"request_uri", data.RequestUri,
|
||||
"request_url", data.RequestUrl,
|
||||
"request_api", data.RequestApi,
|
||||
"request_method", data.RequestMethod,
|
||||
"request_params", data.RequestParams,
|
||||
"request_header", data.RequestHeader,
|
||||
"response_header", data.ResponseHeader,
|
||||
"response_status_code", data.ResponseStatusCode,
|
||||
"response_body", data.ResponseBody,
|
||||
"response_time", data.ResponseTime,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MiddlewareCustom 中间件
|
||||
func (sl *ApiSLog) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response) {
|
||||
data := apiSLog{
|
||||
TraceID: gotrace_id.GetTraceIdContext(ctx),
|
||||
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: dorm.JsonDecodeNoError(request.ResponseBody),
|
||||
ResponseTime: request.ResponseTime,
|
||||
}
|
||||
if sl.slog.status {
|
||||
sl.slog.client.WithTraceId(ctx).Info("MiddlewareCustom",
|
||||
"request_time", data.RequestTime,
|
||||
"request_uri", data.RequestUri,
|
||||
"request_url", data.RequestUrl,
|
||||
"request_api", data.RequestApi,
|
||||
"request_method", data.RequestMethod,
|
||||
"request_params", data.RequestParams,
|
||||
"request_header", data.RequestHeader,
|
||||
"response_header", data.ResponseHeader,
|
||||
"response_status_code", data.ResponseStatusCode,
|
||||
"response_body", data.ResponseBody,
|
||||
"response_time", data.ResponseTime,
|
||||
)
|
||||
}
|
||||
}
|
@ -1,5 +1,38 @@
|
||||
package golog
|
||||
|
||||
const (
|
||||
Version = "1.0.102"
|
||||
)
|
||||
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,8 +0,0 @@
|
||||
package golog
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
currentIpNoConfig = errors.New("请配置 CurrentIp")
|
||||
dbClientFunNoConfig = errors.New("请配置 GormClientFun 或 MongoClientFun")
|
||||
)
|
@ -1,30 +1,20 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go.dtapp.net/goip"
|
||||
"go.mongodb.org/mongo-driver/version"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func (c *GinClient) setConfig(ctx context.Context) {
|
||||
|
||||
info := getSystem()
|
||||
|
||||
c.config.systemHostname = info.SystemHostname
|
||||
c.config.systemOs = info.SystemOs
|
||||
c.config.systemVersion = info.SystemVersion
|
||||
c.config.systemKernel = info.SystemKernel
|
||||
c.config.systemKernelVersion = info.SystemKernelVersion
|
||||
c.config.systemBootTime = info.SystemBootTime
|
||||
c.config.cpuCores = info.CpuCores
|
||||
c.config.cpuModelName = info.CpuModelName
|
||||
c.config.cpuMhz = info.CpuMhz
|
||||
|
||||
c.config.systemInsideIp = goip.GetInsideIp(ctx)
|
||||
|
||||
c.config.sdkVersion = Version
|
||||
c.config.goVersion = runtime.Version()
|
||||
// ConfigSLogClientFun 日志配置
|
||||
func (c *GinClient) ConfigSLogClientFun(sLogFun SLogFun) {
|
||||
sLog := sLogFun()
|
||||
if sLog != nil {
|
||||
c.slog.client = sLog
|
||||
c.slog.status = true
|
||||
}
|
||||
}
|
||||
|
||||
c.config.mongoSdkVersion = version.Driver
|
||||
// ConfigSLogResultClientFun 日志配置然后返回
|
||||
func (c *GinClient) ConfigSLogResultClientFun(sLogFun SLogFun) *GinClient {
|
||||
sLog := sLogFun()
|
||||
if sLog != nil {
|
||||
c.slog.client = sLog
|
||||
c.slog.status = true
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
@ -0,0 +1,11 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
func NewGinCustomClient(ctx context.Context, config *GinCustomClientConfig) (*GinCustomClient, error) {
|
||||
c := &GinCustomClient{}
|
||||
c.ipService = config.IpService
|
||||
return c, nil
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
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,121 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/goip"
|
||||
"go.dtapp.net/gorequest"
|
||||
"go.dtapp.net/gotrace_id"
|
||||
"go.dtapp.net/gourl"
|
||||
)
|
||||
|
||||
// 结构体
|
||||
type ginSLogCustom struct {
|
||||
TraceID string `json:"trace_id,omitempty"` //【系统】跟踪编号
|
||||
RequestUri string `json:"request_uri,omitempty"` //【请求】请求链接 域名+路径+参数
|
||||
RequestUrl string `json:"request_url,omitempty"` //【请求】请求链接 域名+路径
|
||||
RequestApi string `json:"request_api,omitempty"` //【请求】请求接口 路径
|
||||
RequestMethod string `json:"request_method,omitempty"` //【请求】请求方式
|
||||
RequestProto string `json:"request_proto,omitempty"` //【请求】请求协议
|
||||
RequestUa string `json:"request_ua,omitempty"` //【请求】请求UA
|
||||
RequestReferer string `json:"request_referer,omitempty"` //【请求】请求referer
|
||||
RequestUrlQuery string `json:"request_url_query,omitempty"` //【请求】请求URL参数
|
||||
RequestHeader string `json:"request_header,omitempty"` //【请求】请求头
|
||||
RequestIP string `json:"request_ip,omitempty"` //【请求】请求客户端Ip
|
||||
RequestIpCountry string `json:"request_ip_country,omitempty"` //【请求】请求客户端城市
|
||||
RequestIpProvince string `json:"request_ip_province,omitempty"` //【请求】请求客户端省份
|
||||
RequestIpCity string `json:"request_ip_city,omitempty"` //【请求】请求客户端城市
|
||||
RequestIpIsp string `json:"request_ip_isp,omitempty"` //【请求】请求客户端运营商
|
||||
RequestIpLongitude float64 `json:"request_ip_longitude,omitempty"` //【请求】请求客户端经度
|
||||
RequestIpLatitude float64 `json:"request_ip_latitude,omitempty"` //【请求】请求客户端纬度
|
||||
CustomID string `json:"custom_id,omitempty"` //【日志】自定义编号
|
||||
CustomType string `json:"custom_type,omitempty"` //【日志】自定义类型
|
||||
CustomContent string `json:"custom_content,omitempty"` //【日志】自定义内容
|
||||
}
|
||||
|
||||
type GinCustomClientGinRecordOperation struct {
|
||||
slogClient *SLog // 日志服务
|
||||
ipService *goip.Client // IP服务
|
||||
data *ginSLogCustom // 数据
|
||||
}
|
||||
|
||||
// GinRecord 记录日志
|
||||
func (c *GinCustomClient) GinRecord(ginCtx *gin.Context) *GinCustomClientGinRecordOperation {
|
||||
operation := &GinCustomClientGinRecordOperation{
|
||||
slogClient: c.slog.client,
|
||||
ipService: c.ipService,
|
||||
}
|
||||
operation.data = new(ginSLogCustom)
|
||||
operation.data.TraceID = gotrace_id.GetGinTraceId(ginCtx) // 【系统】跟踪编号
|
||||
if ginCtx.Request.TLS == nil {
|
||||
operation.data.RequestUri = "http://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接
|
||||
} else {
|
||||
operation.data.RequestUri = "https://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接
|
||||
}
|
||||
operation.data.RequestUrl = ginCtx.Request.RequestURI //【请求】请求链接 域名+路径
|
||||
operation.data.RequestApi = gourl.UriFilterExcludeQueryString(ginCtx.Request.RequestURI) //【请求】请求接口 路径
|
||||
operation.data.RequestMethod = ginCtx.Request.Method //【请求】请求方式
|
||||
operation.data.RequestProto = ginCtx.Request.Proto //【请求】请求协议
|
||||
operation.data.RequestUa = ginCtx.Request.UserAgent() //【请求】请求UA
|
||||
operation.data.RequestReferer = ginCtx.Request.Referer() //【请求】请求referer
|
||||
operation.data.RequestUrlQuery = dorm.JsonEncodeNoError(ginCtx.Request.URL.Query()) //【请求】请求URL参数
|
||||
operation.data.RequestHeader = dorm.JsonEncodeNoError(ginCtx.Request.Header) //【请求】请求头
|
||||
operation.data.RequestIP = gorequest.ClientIp(ginCtx.Request) //【请求】请求客户端Ip
|
||||
return operation
|
||||
}
|
||||
|
||||
func (o *GinCustomClientGinRecordOperation) CustomInfo(customId any, customType any, customContent any) *GinCustomClientGinRecordOperation {
|
||||
o.data.CustomID = fmt.Sprintf("%s", customId) //【日志】自定义编号
|
||||
o.data.CustomType = fmt.Sprintf("%s", customType) //【日志】自定义类型
|
||||
o.data.CustomContent = fmt.Sprintf("%s", customContent) //【日志】自定义内容
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *GinCustomClientGinRecordOperation) CreateData() {
|
||||
o.slogClient.WithTraceIdStr(o.data.TraceID).Info("custom",
|
||||
"request_uri", o.data.RequestUri,
|
||||
"request_url", o.data.RequestUrl,
|
||||
"request_api", o.data.RequestApi,
|
||||
"request_method", o.data.RequestMethod,
|
||||
"request_proto", o.data.RequestProto,
|
||||
"request_ua", o.data.RequestUa,
|
||||
"request_referer", o.data.RequestReferer,
|
||||
"request_url_query", o.data.RequestUrlQuery,
|
||||
"request_header", o.data.RequestHeader,
|
||||
"request_ip", o.data.RequestIP,
|
||||
"request_ip_country", o.data.RequestIpCountry,
|
||||
"request_ip_province", o.data.RequestIpProvince,
|
||||
"request_ip_city", o.data.RequestIpCity,
|
||||
"request_ip_isp", o.data.RequestIpIsp,
|
||||
"request_ip_longitude", o.data.RequestIpLongitude,
|
||||
"request_ip_latitude", o.data.RequestIpLatitude,
|
||||
"custom_id", o.data.CustomID,
|
||||
"custom_type", o.data.CustomType,
|
||||
"custom_content", o.data.CustomContent,
|
||||
)
|
||||
}
|
||||
|
||||
func (o *GinCustomClientGinRecordOperation) CreateDataNoError() {
|
||||
o.slogClient.WithTraceIdStr(o.data.TraceID).Info("custom",
|
||||
"request_uri", o.data.RequestUri,
|
||||
"request_url", o.data.RequestUrl,
|
||||
"request_api", o.data.RequestApi,
|
||||
"request_method", o.data.RequestMethod,
|
||||
"request_proto", o.data.RequestProto,
|
||||
"request_ua", o.data.RequestUa,
|
||||
"request_referer", o.data.RequestReferer,
|
||||
"request_url_query", o.data.RequestUrlQuery,
|
||||
"request_header", o.data.RequestHeader,
|
||||
"request_ip", o.data.RequestIP,
|
||||
"request_ip_country", o.data.RequestIpCountry,
|
||||
"request_ip_province", o.data.RequestIpProvince,
|
||||
"request_ip_city", o.data.RequestIpCity,
|
||||
"request_ip_isp", o.data.RequestIpIsp,
|
||||
"request_ip_longitude", o.data.RequestIpLongitude,
|
||||
"request_ip_latitude", o.data.RequestIpLatitude,
|
||||
"custom_id", o.data.CustomID,
|
||||
"custom_type", o.data.CustomType,
|
||||
"custom_content", o.data.CustomContent,
|
||||
)
|
||||
}
|
@ -1,156 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/goip"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.dtapp.net/gourl"
|
||||
"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:"default:0.0.0.0;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:"default:0.0.0.0;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 (c *GinClient) gormAutoMigrate(ctx context.Context) {
|
||||
err := c.gormClient.GetDb().Table(c.gormConfig.tableName).AutoMigrate(&ginPostgresqlLog{})
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("创建模型:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// gormRecord 记录日志
|
||||
func (c *GinClient) gormRecord(data ginPostgresqlLog) {
|
||||
|
||||
data.SystemHostName = c.config.systemHostname //【系统】主机名
|
||||
data.SystemInsideIp = c.config.systemInsideIp //【系统】内网ip
|
||||
data.GoVersion = c.config.goVersion //【程序】Go版本
|
||||
data.SdkVersion = c.config.sdkVersion //【程序】Sdk版本
|
||||
data.SystemOs = c.config.systemOs //【系统】系统类型
|
||||
data.SystemArch = c.config.systemKernel //【系统】系统架构
|
||||
|
||||
err := c.gormClient.GetDb().Table(c.gormConfig.tableName).Create(&data).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceIdStr(data.TraceId).Sugar().Errorf("记录框架日志错误:%s", err)
|
||||
c.zapLog.WithTraceIdStr(data.TraceId).Sugar().Errorf("记录框架日志数据:%+v", data)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *GinClient) gormRecordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, 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
|
||||
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 //【请求】请求链接
|
||||
}
|
||||
|
||||
if len(requestBody) > 0 {
|
||||
data.RequestBody = dorm.JsonEncodeNoError(requestBody) //【请求】请求主体
|
||||
}
|
||||
|
||||
c.gormRecord(data)
|
||||
}
|
||||
|
||||
func (c *GinClient) gormRecordXml(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, 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
|
||||
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 //【请求】请求链接
|
||||
}
|
||||
|
||||
if len(requestBody) > 0 {
|
||||
data.RequestBody = dorm.XmlEncodeNoError(dorm.XmlDecodeNoError(requestBody)) //【请求】请求内容
|
||||
}
|
||||
|
||||
c.gormRecord(data)
|
||||
}
|
||||
|
||||
// GormDelete 删除
|
||||
func (c *GinClient) GormDelete(ctx context.Context, hour int64) error {
|
||||
err := c.gormClient.GetDb().Table(c.gormConfig.tableName).Where("request_time < ?", gotime.Current().BeforeHour(hour).Format()).Delete(&ginPostgresqlLog{}).Error
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("删除失败:%s", err)
|
||||
}
|
||||
return err
|
||||
}
|
@ -1,247 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.dtapp.net/dorm"
|
||||
"go.dtapp.net/goip"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.dtapp.net/gotrace_id"
|
||||
"go.dtapp.net/gourl"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ginMongoLogRequestIpLocationLocation struct {
|
||||
Type string `json:"type,omitempty" bson:"type,omitempty"` // GeoJSON类型
|
||||
Coordinates []float64 `json:"coordinates,omitempty" bson:"coordinates,omitempty"` // 经度,纬度
|
||||
}
|
||||
|
||||
// 模型结构体
|
||||
type ginMongoLog struct {
|
||||
LogId primitive.ObjectID `json:"log_id,omitempty" bson:"_id,omitempty"` //【记录】编号
|
||||
TraceId string `json:"trace_id,omitempty" bson:"trace_id,omitempty"` //【记录】跟踪编号
|
||||
RequestTime string `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"` //【请求】请求方式
|
||||
RequestProto string `json:"request_proto,omitempty" bson:"request_proto,omitempty"` //【请求】请求协议
|
||||
RequestUa string `json:"request_ua,omitempty" bson:"request_ua,omitempty"` //【请求】请求UA
|
||||
RequestReferer string `json:"request_referer,omitempty" bson:"request_referer,omitempty"` //【请求】请求referer
|
||||
RequestBody interface{} `json:"request_body,omitempty" bson:"request_body,omitempty"` //【请求】请求主体
|
||||
RequestUrlQuery interface{} `json:"request_url_query,omitempty" bson:"request_url_query,omitempty"` //【请求】请求URL参数
|
||||
RequestIp struct {
|
||||
Ip string `json:"ip,omitempty" bson:"ip,omitempty"` //【请求】请求客户端Ip
|
||||
Continent string `json:"continent,omitempty" bson:"continent,omitempty"` //【请求】请求客户端大陆
|
||||
Country string `json:"country,omitempty" bson:"country,omitempty"` //【请求】请求客户端国家
|
||||
Province string `json:"province,omitempty" bson:"province,omitempty"` //【请求】请求客户端省份
|
||||
City string `json:"city,omitempty" bson:"city,omitempty"` //【请求】请求客户端城市
|
||||
Isp string `json:"isp,omitempty" bson:"isp,omitempty"` //【请求】请求客户端运营商
|
||||
} `json:"request_ip,omitempty" bson:"request_ip,omitempty"` //【请求】请求客户端信息
|
||||
RequestIpLocation interface{} `json:"request_ip_location,omitempty" bson:"request_ip_location,omitempty"` //【请求】请求客户端位置
|
||||
RequestHeader interface{} `json:"request_header,omitempty" bson:"request_header,omitempty"` //【请求】请求头
|
||||
ResponseTime string `json:"response_time,omitempty" bson:"response_time,omitempty"` //【返回】时间
|
||||
ResponseCode int `json:"response_code,omitempty" bson:"response_code,omitempty"` //【返回】状态码
|
||||
ResponseMsg string `json:"response_msg,omitempty" bson:"response_msg,omitempty"` //【返回】描述
|
||||
ResponseData interface{} `json:"response_data,omitempty" bson:"response_data,omitempty"` //【返回】数据
|
||||
CostTime int64 `json:"cost_time,omitempty" bson:"cost_time,omitempty"` //【系统】花费时间
|
||||
System struct {
|
||||
Hostname string `json:"hostname" bson:"hostname"` //【系统】主机名
|
||||
Os string `json:"os" bson:"os"` //【系统】系统类型
|
||||
Version string `json:"version" bson:"version"` //【系统】系统版本
|
||||
Kernel string `json:"kernel" bson:"kernel"` //【系统】系统内核
|
||||
KernelVersion string `json:"kernel_version" bson:"kernel_version"` //【系统】系统内核版本
|
||||
BootTime string `json:"boot_time" bson:"boot_time"` //【系统】系统开机时间
|
||||
CpuCores int `json:"cpu_cores,omitempty" bson:"cpu_cores,omitempty"` //【系统】CPU核数
|
||||
CpuModelName string `json:"cpu_model_name,omitempty" bson:"cpu_model_name,omitempty"` //【系统】CPU型号名称
|
||||
CpuMhz float64 `json:"cpu_mhz,omitempty" bson:"cpu_mhz,omitempty"` //【系统】CPU兆赫
|
||||
InsideIp string `json:"inside_ip" bson:"inside_ip"` //【系统】内网ip
|
||||
OutsideIp string `json:"outside_ip" bson:"outside_ip"` //【系统】外网ip
|
||||
GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【系统】go版本
|
||||
SdkVersion string `json:"sdk_version,omitempty" bson:"sdk_version,omitempty"` //【系统】sdk版本
|
||||
MongoVersion string `json:"mongo_version,omitempty" bson:"mongo_version,omitempty"` //【系统】mongo版本
|
||||
MongoSdkVersion string `json:"mongo_sdk_version,omitempty" bson:"mongo_sdk_version,omitempty"` //【系统】mongo sdk版本
|
||||
} `json:"system,omitempty" bson:"system,omitempty"` //【系统】信息
|
||||
}
|
||||
|
||||
// 创建集合
|
||||
func (c *GinClient) mongoCreateCollection(ctx context.Context) {
|
||||
err := c.mongoClient.Database(c.mongoConfig.databaseName).CreateCollection(ctx, c.mongoConfig.collectionName, options.CreateCollection().SetCollation(&options.Collation{
|
||||
Locale: "request_time",
|
||||
Strength: -1,
|
||||
}))
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Error("创建集合:", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 创建索引
|
||||
func (c *GinClient) mongoCreateIndexes(ctx context.Context) {
|
||||
_, err := c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).CreateManyIndexes(ctx, []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{
|
||||
Key: "trace_id",
|
||||
Value: -1,
|
||||
}},
|
||||
}, {
|
||||
Keys: bson.D{{
|
||||
Key: "request_time",
|
||||
Value: -1,
|
||||
}},
|
||||
}, {
|
||||
Keys: bson.D{{
|
||||
Key: "request_method",
|
||||
Value: 1,
|
||||
}},
|
||||
}, {
|
||||
Keys: bson.D{{
|
||||
Key: "response_time",
|
||||
Value: -1,
|
||||
}},
|
||||
}, {
|
||||
Keys: bson.D{{
|
||||
Key: "response_code",
|
||||
Value: 1,
|
||||
}},
|
||||
}, {
|
||||
Keys: bson.D{{
|
||||
Key: "request_ip_location",
|
||||
Value: "2dsphere",
|
||||
}},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceId(ctx).Sugar().Errorf("创建索引:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// MongoDelete 删除
|
||||
func (c *GinClient) MongoDelete(ctx context.Context, hour int64) (*mongo.DeleteResult, error) {
|
||||
filter := bson.D{{"request_time", bson.D{{"$lt", gotime.SetCurrent(gotime.Current().BeforeHour(hour).Time)}}}}
|
||||
return c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).DeleteMany(ctx, filter)
|
||||
}
|
||||
|
||||
// 记录日志
|
||||
func (c *GinClient) mongoRecord(ctx context.Context, data ginMongoLog) {
|
||||
|
||||
data.LogId = primitive.NewObjectID() //【记录】编号
|
||||
data.System.Hostname = c.config.systemHostname //【系统】主机名
|
||||
data.System.Os = c.config.systemOs //【系统】系统类型
|
||||
data.System.Version = c.config.systemVersion //【系统】系统版本
|
||||
data.System.Kernel = c.config.systemKernel //【系统】系统内核
|
||||
data.System.KernelVersion = c.config.systemKernelVersion //【系统】系统内核版本
|
||||
data.System.BootTime = gotime.SetCurrent(gotime.SetCurrentUnix(int64(c.config.systemBootTime)).Time).Format() //【系统】系统开机时间
|
||||
data.System.CpuCores = c.config.cpuCores //【系统】CPU核数
|
||||
data.System.CpuModelName = c.config.cpuModelName //【程序】CPU型号名称
|
||||
data.System.CpuMhz = c.config.cpuMhz //【系统】CPU兆赫
|
||||
data.System.InsideIp = c.config.systemInsideIp //【系统】内网ip
|
||||
data.System.OutsideIp = c.config.systemOutsideIp //【系统】外网ip
|
||||
data.System.GoVersion = c.config.goVersion //【系统】Go版本
|
||||
data.System.SdkVersion = c.config.sdkVersion //【系统】Sdk版本
|
||||
data.System.MongoVersion = c.config.mongoVersion //【系统】mongo版本
|
||||
data.System.MongoSdkVersion = c.config.mongoSdkVersion //【系统】mongo sdk版本
|
||||
|
||||
_, err := c.mongoClient.Database(c.mongoConfig.databaseName).Collection(c.mongoConfig.collectionName).InsertOne(ctx, data)
|
||||
if err != nil {
|
||||
c.zapLog.WithTraceIdStr(data.TraceId).Sugar().Errorf("保存框架日志错误:%s", err)
|
||||
c.zapLog.WithTraceIdStr(data.TraceId).Sugar().Errorf("保存框架日志数据:%+v", data)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *GinClient) mongoRecordJson(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, ipInfo goip.AnalyseResult) {
|
||||
|
||||
var ctx = gotrace_id.SetGinTraceIdContext(context.Background(), ginCtx)
|
||||
|
||||
data := ginMongoLog{
|
||||
TraceId: traceId, //【记录】跟踪编号
|
||||
RequestTime: gotime.SetCurrent(requestTime).Format(), //【请求】时间
|
||||
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参数
|
||||
RequestHeader: ginCtx.Request.Header, //【请求】请求头
|
||||
ResponseTime: gotime.Current().Format(), //【返回】时间
|
||||
ResponseCode: responseCode, //【返回】状态码
|
||||
ResponseData: dorm.JsonDecodeNoError([]byte(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.JsonDecodeNoError(requestBody) //【请求】请求主体
|
||||
}
|
||||
|
||||
//【请求】请求客户端信息
|
||||
data.RequestIp.Ip = ipInfo.Ip
|
||||
data.RequestIp.Continent = ipInfo.Continent
|
||||
data.RequestIp.Country = ipInfo.Country
|
||||
data.RequestIp.Province = ipInfo.Province
|
||||
data.RequestIp.City = ipInfo.City
|
||||
data.RequestIp.Isp = ipInfo.Isp
|
||||
if ipInfo.LocationLatitude != 0 && ipInfo.LocationLongitude != 0 {
|
||||
data.RequestIpLocation = ginMongoLogRequestIpLocationLocation{
|
||||
Type: "Point",
|
||||
Coordinates: []float64{ipInfo.LocationLongitude, ipInfo.LocationLatitude},
|
||||
}
|
||||
}
|
||||
|
||||
c.mongoRecord(ctx, data)
|
||||
}
|
||||
|
||||
func (c *GinClient) mongoRecordXml(ginCtx *gin.Context, traceId string, requestTime time.Time, requestBody []byte, responseCode int, responseBody string, startTime, endTime int64, ipInfo goip.AnalyseResult) {
|
||||
|
||||
var ctx = gotrace_id.SetGinTraceIdContext(context.Background(), ginCtx)
|
||||
|
||||
data := ginMongoLog{
|
||||
TraceId: traceId, //【记录】跟踪编号
|
||||
RequestTime: gotime.SetCurrent(requestTime).Format(), //【请求】时间
|
||||
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参数
|
||||
RequestHeader: ginCtx.Request.Header, //【请求】请求头
|
||||
ResponseTime: gotime.Current().Format(), //【返回】时间
|
||||
ResponseCode: responseCode, //【返回】状态码
|
||||
ResponseData: dorm.JsonDecodeNoError([]byte(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.XmlDecodeNoError(requestBody) //【请求】请求主体
|
||||
}
|
||||
|
||||
//【请求】请求客户端信息
|
||||
data.RequestIp.Ip = ipInfo.Ip
|
||||
data.RequestIp.Continent = ipInfo.Continent
|
||||
data.RequestIp.Country = ipInfo.Country
|
||||
data.RequestIp.Province = ipInfo.Province
|
||||
data.RequestIp.City = ipInfo.City
|
||||
data.RequestIp.Isp = ipInfo.Isp
|
||||
if ipInfo.LocationLatitude != 0 && ipInfo.LocationLongitude != 0 {
|
||||
data.RequestIpLocation = ginMongoLogRequestIpLocationLocation{
|
||||
Type: "Point",
|
||||
Coordinates: []float64{ipInfo.LocationLongitude, ipInfo.LocationLatitude},
|
||||
}
|
||||
}
|
||||
|
||||
c.mongoRecord(ctx, data)
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"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"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 结构体
|
||||
type ginSLog struct {
|
||||
TraceID string `json:"trace_id,omitempty"` //【系统】跟踪编号
|
||||
RequestTime time.Time `json:"request_time,omitempty"` //【请求】时间
|
||||
RequestUri string `json:"request_uri,omitempty"` //【请求】请求链接 域名+路径+参数
|
||||
RequestUrl string `json:"request_url,omitempty"` //【请求】请求链接 域名+路径
|
||||
RequestApi string `json:"request_api,omitempty"` //【请求】请求接口 路径
|
||||
RequestMethod string `json:"request_method,omitempty"` //【请求】请求方式
|
||||
RequestProto string `json:"request_proto,omitempty"` //【请求】请求协议
|
||||
RequestUa string `json:"request_ua,omitempty"` //【请求】请求UA
|
||||
RequestReferer string `json:"request_referer,omitempty"` //【请求】请求referer
|
||||
RequestBody string `json:"request_body,omitempty"` //【请求】请求主体
|
||||
RequestUrlQuery map[string][]string `json:"request_url_query,omitempty"` //【请求】请求URL参数
|
||||
RequestIP string `json:"request_ip,omitempty"` //【请求】请求客户端Ip
|
||||
RequestIpCountry string `json:"request_ip_country,omitempty"` //【请求】请求客户端城市
|
||||
RequestIpProvince string `json:"request_ip_province,omitempty"` //【请求】请求客户端省份
|
||||
RequestIpCity string `json:"request_ip_city,omitempty"` //【请求】请求客户端城市
|
||||
RequestIpIsp string `json:"request_ip_isp,omitempty"` //【请求】请求客户端运营商
|
||||
RequestIpLongitude float64 `json:"request_ip_longitude,omitempty"` //【请求】请求客户端经度
|
||||
RequestIpLatitude float64 `json:"request_ip_latitude,omitempty"` //【请求】请求客户端纬度
|
||||
RequestHeader map[string][]string `json:"request_header,omitempty"` //【请求】请求头
|
||||
RequestAllContent map[string]interface{} `json:"request_all_content,omitempty"` // 【请求】请求全部内容
|
||||
ResponseTime time.Time `json:"response_time,omitempty"` //【返回】时间
|
||||
ResponseCode int `json:"response_code,omitempty"` //【返回】状态码
|
||||
ResponseMsg string `json:"response_msg,omitempty"` //【返回】描述
|
||||
ResponseData string `json:"response_data,omitempty"` //【返回】数据
|
||||
CostTime int64 `json:"cost_time,omitempty"` //【系统】花费时间
|
||||
}
|
||||
|
||||
// record 记录日志
|
||||
func (c *GinClient) record(msg string, data ginSLog) {
|
||||
c.slog.client.WithTraceIdStr(data.TraceID).Info(msg,
|
||||
"request_time", data.RequestTime,
|
||||
"request_uri", data.RequestUri,
|
||||
"request_url", data.RequestUrl,
|
||||
"request_api", data.RequestApi,
|
||||
"request_method", data.RequestMethod,
|
||||
"request_proto", data.RequestProto,
|
||||
"request_ua", data.RequestUa,
|
||||
"request_referer", data.RequestReferer,
|
||||
"request_body", data.RequestBody,
|
||||
"request_url_query", data.RequestUrlQuery,
|
||||
"request_ip", data.RequestIP,
|
||||
"request_ip_country", data.RequestIpCountry,
|
||||
"request_ip_province", data.RequestIpProvince,
|
||||
"request_ip_city", data.RequestIpCity,
|
||||
"request_ip_isp", data.RequestIpIsp,
|
||||
"request_ip_longitude", data.RequestIpLongitude,
|
||||
"request_ip_latitude", data.RequestIpLatitude,
|
||||
"request_header", data.RequestHeader,
|
||||
"request_all_content", data.RequestAllContent,
|
||||
"response_time", data.ResponseTime,
|
||||
"response_code", data.ResponseCode,
|
||||
"response_msg", data.ResponseMsg,
|
||||
"response_data", data.ResponseData,
|
||||
"cost_time", data.CostTime,
|
||||
)
|
||||
}
|
||||
|
||||
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) {
|
||||
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 //【请求】请求链接
|
||||
}
|
||||
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)
|
||||
}
|
@ -1,97 +1,82 @@
|
||||
module go.dtapp.net/golog
|
||||
|
||||
go 1.19
|
||||
go 1.21.5
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.8.1
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible
|
||||
github.com/shirou/gopsutil v3.21.11+incompatible
|
||||
go.dtapp.net/dorm v1.0.54
|
||||
go.dtapp.net/goip v1.0.39
|
||||
go.dtapp.net/gorequest v1.0.36
|
||||
go.dtapp.net/gotime v1.0.5
|
||||
go.dtapp.net/dorm v1.0.55
|
||||
go.dtapp.net/goip v1.0.42
|
||||
go.dtapp.net/gojson v1.0.2
|
||||
go.dtapp.net/gorequest v1.0.39
|
||||
go.dtapp.net/gotime v1.0.6
|
||||
go.dtapp.net/gotrace_id v1.0.6
|
||||
go.dtapp.net/gourl v1.0.0
|
||||
go.mongodb.org/mongo-driver v1.11.0
|
||||
go.uber.org/zap v1.23.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/MercuryEngineering/CookieMonster v0.0.0-20180304172713-1584578b3403 // indirect
|
||||
github.com/basgys/goxml2json v1.1.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/bytedance/sonic v1.10.2 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
|
||||
github.com/chenzhuoyu/iasm v0.9.1 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.11.1 // indirect
|
||||
github.com/go-redis/redis/v9 v9.0.0-rc.2 // indirect
|
||||
github.com/go-sql-driver/mysql v1.6.0 // indirect
|
||||
github.com/goccy/go-json v0.9.11 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
|
||||
github.com/jackc/pgconn v1.13.0 // indirect
|
||||
github.com/jackc/pgio v1.0.0 // indirect
|
||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.16.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.7.1 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
|
||||
github.com/jackc/pgtype v1.12.0 // indirect
|
||||
github.com/jackc/pgx/v4 v4.17.2 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
|
||||
github.com/jackc/pgx/v5 v5.5.1 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.15.12 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/lib/pq v1.10.7 // indirect
|
||||
github.com/mattn/go-isatty v0.0.16 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
github.com/leodido/go-urn v1.2.4 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/montanaflynn/stats v0.6.6 // indirect
|
||||
github.com/oschwald/geoip2-golang v1.8.0 // indirect
|
||||
github.com/oschwald/maxminddb-golang v1.10.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/oschwald/geoip2-golang v1.9.0 // indirect
|
||||
github.com/oschwald/maxminddb-golang v1.12.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
|
||||
github.com/redis/go-redis/v9 v9.3.1 // indirect
|
||||
github.com/rogpeppe/go-internal v1.12.0 // indirect
|
||||
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda // indirect
|
||||
github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f // indirect
|
||||
github.com/segmentio/fasthash v1.0.3 // indirect
|
||||
github.com/sirupsen/logrus v1.9.0 // indirect
|
||||
github.com/syndtr/goleveldb v1.0.0 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.11 // indirect
|
||||
github.com/tklauser/numcpus v0.6.0 // indirect
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
|
||||
github.com/ugorji/go/codec v1.2.7 // indirect
|
||||
github.com/ulikunitz/xz v0.5.10 // indirect
|
||||
github.com/upper/db/v4 v4.6.0 // indirect
|
||||
github.com/uptrace/bun v1.1.9 // indirect
|
||||
github.com/uptrace/bun/dialect/mysqldialect v1.1.9 // indirect
|
||||
github.com/uptrace/bun/dialect/pgdialect v1.1.9 // indirect
|
||||
github.com/uptrace/bun/driver/pgdriver v1.1.9 // indirect
|
||||
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.1.1 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.3 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
||||
go.dtapp.net/gorandom v1.0.1 // indirect
|
||||
go.dtapp.net/gostring v1.0.10 // indirect
|
||||
go.uber.org/atomic v1.10.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
golang.org/x/crypto v0.3.0 // indirect
|
||||
golang.org/x/mod v0.7.0 // indirect
|
||||
golang.org/x/net v0.2.0 // indirect
|
||||
golang.org/x/sync v0.1.0 // indirect
|
||||
golang.org/x/sys v0.2.0 // indirect
|
||||
golang.org/x/text v0.4.0 // indirect
|
||||
google.golang.org/protobuf v1.28.1 // indirect
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gorm.io/driver/mysql v1.4.4 // indirect
|
||||
gorm.io/driver/postgres v1.4.5 // indirect
|
||||
gorm.io/gorm v1.24.2 // indirect
|
||||
mellium.im/sasl v0.3.0 // indirect
|
||||
modernc.org/libc v1.16.19 // indirect
|
||||
modernc.org/sqlite v1.18.0 // indirect
|
||||
xorm.io/builder v0.3.12 // indirect
|
||||
xorm.io/xorm v1.3.2 // indirect
|
||||
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.13 // indirect
|
||||
github.com/tklauser/numcpus v0.7.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
github.com/ulikunitz/xz v0.5.11 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.3 // indirect
|
||||
go.dtapp.net/gorandom v1.0.2 // indirect
|
||||
go.dtapp.net/gostring v1.0.11 // 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
|
||||
golang.org/x/net v0.19.0 // indirect
|
||||
golang.org/x/sync v0.5.0 // indirect
|
||||
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
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
gorm.io/datatypes v1.2.0 // indirect
|
||||
gorm.io/driver/mysql v1.5.2 // indirect
|
||||
gorm.io/driver/postgres v1.5.4 // indirect
|
||||
gorm.io/gen v0.3.24 // indirect
|
||||
gorm.io/gorm v1.25.5 // indirect
|
||||
gorm.io/hints v1.1.2 // indirect
|
||||
gorm.io/plugin/dbresolver v1.5.0 // indirect
|
||||
)
|
||||
|
@ -0,0 +1,131 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/natefinch/lumberjack"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.dtapp.net/gotrace_id"
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
type SLogFun func() *SLog
|
||||
|
||||
type sLogConfig struct {
|
||||
logPath string // [File]日志文件路径
|
||||
logName string // [File]日志文件名
|
||||
maxSize int // [File]单位为MB,默认为512MB
|
||||
maxBackups int // [File]保留旧文件的最大个数
|
||||
maxAge int // [File]文件最多保存多少天 0=不删除
|
||||
showLine bool // [File、Console]显示代码行
|
||||
}
|
||||
|
||||
type SLog struct {
|
||||
config *sLogConfig
|
||||
jsonHandler *slog.JSONHandler
|
||||
textHandler *slog.TextHandler
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
type SLogFileConfig struct {
|
||||
LogPath string // 日志文件路径
|
||||
LogName string // 日志文件名
|
||||
MaxSize int // 单位为MB,默认为512MB
|
||||
MaxBackups int // 保留旧文件的最大个数
|
||||
MaxAge int // 文件最多保存多少天 0=不删除
|
||||
ShowLine bool // 显示代码行
|
||||
}
|
||||
|
||||
func NewSlogFile(ctx context.Context, config *SLogFileConfig) *SLog {
|
||||
|
||||
sl := &SLog{
|
||||
config: &sLogConfig{
|
||||
logPath: config.LogPath,
|
||||
logName: config.LogName,
|
||||
maxSize: config.MaxSize,
|
||||
maxBackups: config.MaxBackups,
|
||||
maxAge: config.MaxAge,
|
||||
showLine: config.ShowLine,
|
||||
},
|
||||
}
|
||||
|
||||
opts := slog.HandlerOptions{
|
||||
AddSource: sl.config.showLine,
|
||||
Level: slog.LevelDebug,
|
||||
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
|
||||
if a.Key == slog.TimeKey {
|
||||
a.Value = slog.StringValue(a.Value.Time().Format(gotime.DateTimeFormat))
|
||||
//return slog.Attr{}
|
||||
}
|
||||
return a
|
||||
},
|
||||
}
|
||||
|
||||
lumberjackLogger := lumberjack.Logger{
|
||||
Filename: sl.config.logPath + sl.config.logName, // ⽇志⽂件路径
|
||||
MaxSize: sl.config.maxSize, // 单位为MB,默认为512MB
|
||||
MaxAge: sl.config.maxAge, // 文件最多保存多少天
|
||||
MaxBackups: sl.config.maxBackups, // 保留旧文件的最大个数
|
||||
}
|
||||
|
||||
// json格式输出
|
||||
sl.jsonHandler = slog.NewJSONHandler(&lumberjackLogger, &opts)
|
||||
sl.logger = slog.New(sl.jsonHandler)
|
||||
|
||||
return sl
|
||||
}
|
||||
|
||||
type SLogConsoleConfig struct {
|
||||
ShowLine bool // 显示代码行
|
||||
}
|
||||
|
||||
func NewSlogConsole(ctx context.Context, config *SLogConsoleConfig) *SLog {
|
||||
|
||||
sl := &SLog{
|
||||
config: &sLogConfig{
|
||||
showLine: config.ShowLine,
|
||||
},
|
||||
}
|
||||
|
||||
opts := slog.HandlerOptions{
|
||||
AddSource: sl.config.showLine,
|
||||
Level: slog.LevelDebug,
|
||||
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
|
||||
if a.Key == slog.TimeKey {
|
||||
a.Value = slog.StringValue(a.Value.Time().Format(gotime.DateTimeFormat))
|
||||
//return slog.Attr{}
|
||||
}
|
||||
return a
|
||||
},
|
||||
}
|
||||
|
||||
// json格式输出
|
||||
sl.jsonHandler = slog.NewJSONHandler(os.Stdout, &opts)
|
||||
sl.logger = slog.New(sl.jsonHandler)
|
||||
|
||||
return sl
|
||||
}
|
||||
|
||||
// WithLogger 跟踪编号
|
||||
func (sl *SLog) WithLogger() *slog.Logger {
|
||||
logger := slog.New(sl.jsonHandler)
|
||||
return logger
|
||||
}
|
||||
|
||||
// WithTraceId 跟踪编号
|
||||
func (sl *SLog) WithTraceId(ctx context.Context) *slog.Logger {
|
||||
jsonHandler := sl.jsonHandler.WithAttrs([]slog.Attr{
|
||||
slog.String("trace_id", gotrace_id.GetTraceIdContext(ctx)),
|
||||
})
|
||||
logger := slog.New(jsonHandler)
|
||||
return logger
|
||||
}
|
||||
|
||||
// WithTraceIdStr 跟踪编号
|
||||
func (sl *SLog) WithTraceIdStr(traceId string) *slog.Logger {
|
||||
jsonHandler := sl.jsonHandler.WithAttrs([]slog.Attr{
|
||||
slog.String("trace_id", traceId),
|
||||
})
|
||||
logger := slog.New(jsonHandler)
|
||||
return logger
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package golog
|
||||
|
||||
const (
|
||||
Version = "1.0.103"
|
||||
)
|
@ -1,137 +0,0 @@
|
||||
package golog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/natefinch/lumberjack"
|
||||
"go.dtapp.net/gotime"
|
||||
"go.dtapp.net/gotrace_id"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ZapLogConfig struct {
|
||||
LogPath string // 日志文件路径
|
||||
LogName string // 日志文件名
|
||||
MaxSize int // 单位为MB,默认为512MB
|
||||
MaxBackups int // 保留旧文件的最大个数
|
||||
MaxAge int // 文件最多保存多少天 0=不删除
|
||||
LocalTime bool // 采用本地时间
|
||||
Compress bool // 是否压缩日志
|
||||
JsonFormat bool // 是否输出为json格式
|
||||
ShowLine bool // 显示代码行
|
||||
LogInConsole bool // 是否同时输出到控制台
|
||||
}
|
||||
|
||||
type ZapLog struct {
|
||||
config *ZapLogConfig
|
||||
logger *zap.Logger
|
||||
zapCore zapcore.Core
|
||||
}
|
||||
|
||||
func NewZapLog(config *ZapLogConfig) *ZapLog {
|
||||
|
||||
zl := &ZapLog{config: config}
|
||||
|
||||
var syncer zapcore.WriteSyncer
|
||||
|
||||
if zl.config.LogPath != "" && zl.config.LogName != "" {
|
||||
// 定义日志切割配置
|
||||
hook := lumberjack.Logger{
|
||||
Filename: zl.config.LogPath + zl.config.LogName, // ⽇志⽂件路径
|
||||
MaxSize: zl.config.MaxSize, // 单位为MB,默认为512MB
|
||||
MaxBackups: zl.config.MaxBackups, // 保留旧文件的最大个数
|
||||
LocalTime: zl.config.LocalTime, // 采用本地时间
|
||||
Compress: zl.config.Compress, // 是否压缩日志
|
||||
}
|
||||
if zl.config.MaxAge > 0 {
|
||||
// 文件最多保存多少天
|
||||
hook.MaxAge = zl.config.MaxAge
|
||||
}
|
||||
if zl.config.LogInConsole {
|
||||
// 在控制台和文件输出日志
|
||||
syncer = zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&hook))
|
||||
} else {
|
||||
// 在文件输出日志
|
||||
syncer = zapcore.AddSync(&hook)
|
||||
}
|
||||
} else {
|
||||
// 在控制台输出日志
|
||||
syncer = zapcore.NewMultiWriteSyncer()
|
||||
}
|
||||
|
||||
// 自定义时间输出格式
|
||||
customTimeEncoder := func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||
enc.AppendString(t.Format(gotime.DateTimeFormat))
|
||||
}
|
||||
|
||||
// 自定义日志级别显示
|
||||
customLevelEncoder := func(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
|
||||
enc.AppendString(level.CapitalString())
|
||||
}
|
||||
|
||||
// 定义zap配置信息
|
||||
encoderConf := zapcore.EncoderConfig{
|
||||
CallerKey: "caller_line", // 打印文件名和行数
|
||||
LevelKey: "level_name",
|
||||
MessageKey: "msg",
|
||||
TimeKey: "time",
|
||||
NameKey: "logger",
|
||||
StacktraceKey: "stacktrace",
|
||||
LineEnding: zapcore.DefaultLineEnding,
|
||||
EncodeTime: customTimeEncoder, // 自定义时间格式
|
||||
EncodeLevel: customLevelEncoder, // 小写编码器
|
||||
EncodeCaller: zapcore.ShortCallerEncoder, // 全路径编码器
|
||||
EncodeDuration: zapcore.SecondsDurationEncoder,
|
||||
EncodeName: zapcore.FullNameEncoder,
|
||||
}
|
||||
|
||||
// 判断是否json格式输出
|
||||
if zl.config.JsonFormat {
|
||||
zl.zapCore = zapcore.NewCore(zapcore.NewJSONEncoder(encoderConf),
|
||||
syncer, zap.NewAtomicLevelAt(zapcore.InfoLevel))
|
||||
} else {
|
||||
zl.zapCore = zapcore.NewCore(zapcore.NewConsoleEncoder(encoderConf),
|
||||
syncer, zap.NewAtomicLevelAt(zapcore.InfoLevel))
|
||||
}
|
||||
|
||||
zl.logger = zl.withShowLine(zap.New(zl.zapCore))
|
||||
|
||||
return zl
|
||||
}
|
||||
|
||||
// 判断是否显示代码行号
|
||||
func (zl *ZapLog) withShowLine(logger *zap.Logger) *zap.Logger {
|
||||
if zl.config.ShowLine {
|
||||
logger = logger.WithOptions(zap.AddCaller())
|
||||
}
|
||||
return logger
|
||||
}
|
||||
|
||||
// WithLogger 跟踪编号
|
||||
func (zl *ZapLog) WithLogger() *zap.Logger {
|
||||
return zl.logger
|
||||
}
|
||||
|
||||
// WithTraceId 跟踪编号
|
||||
func (zl *ZapLog) WithTraceId(ctx context.Context) *zap.Logger {
|
||||
logger := zl.logger
|
||||
logger = logger.With(zapcore.Field{
|
||||
Key: "trace_id",
|
||||
Type: zapcore.StringType,
|
||||
String: gotrace_id.GetTraceIdContext(ctx),
|
||||
})
|
||||
return logger
|
||||
}
|
||||
|
||||
// WithTraceIdStr 跟踪编号
|
||||
func (zl *ZapLog) WithTraceIdStr(traceId string) *zap.Logger {
|
||||
logger := zl.logger
|
||||
logger = logger.With(zapcore.Field{
|
||||
Key: "trace_id",
|
||||
Type: zapcore.StringType,
|
||||
String: traceId,
|
||||
})
|
||||
return logger
|
||||
}
|
Loading…
Reference in new issue