Compare commits

...

100 Commits

Author SHA1 Message Date
dtapps 7b6e0fc4dc - optimization gorm
4 months ago
dtapps f0ebc78416 - update
5 months ago
dtapps 28f62d88e6 - add `DeleteData` function
5 months ago
dtapps c3f557e7f1 - update ip
5 months ago
dtapps 78c77ff6ad - update gin_gorm
5 months ago
dtapps d63a3e16d1 - update gin_slog
5 months ago
dtapps 0b68d2d075 - update mod
5 months ago
dtapps 0e2a64323d add slog
5 months ago
李光春 dffb6465a9 - update
1 year ago
李光春 9393e23930 - update mongo
2 years ago
李光春 8e0ffea774 - update time
2 years ago
李光春 bd3b2c9ba0 - update ip
2 years ago
李光春 0c597201ff - update
2 years ago
李光春 5e72ef4cd4 - update
2 years ago
李光春 a2030c149f - update system
2 years ago
李光春 359d8bf17b - update
2 years ago
李光春 b4009b5862 - update
2 years ago
李光春 f8a9284813 - update mongo
2 years ago
李光春 54b64a6175 - update
2 years ago
李光春 699b1a6e5f - update
2 years ago
李光春 a7e10e9661 - update db
2 years ago
李光春 592b43fc26 - update db
2 years ago
李光春 e05fcc3f4e - update mongo
2 years ago
李光春 4df2eab876 - update mongo
2 years ago
李光春 f38cafd8f7 - update mongo
2 years ago
李光春 694b738017 - update mongo
2 years ago
李光春 14f6ae68fe - update
2 years ago
李光春 7dead7925c - update ip
2 years ago
李光春 0fb387879e - update config
2 years ago
李光春 729bbf76c6 - update fun
2 years ago
李光春 5b328feee9 - update fun
2 years ago
李光春 10e31bb497 - add fun
2 years ago
李光春 3f0ab29c10 - update gorm
2 years ago
李光春 c4d22be7c9 - update mongo
2 years ago
李光春 0c37436a40 - update mongo
2 years ago
李光春 9027f79406 - update mongo
2 years ago
李光春 d34e9f1566 - update mongo
2 years ago
李光春 c8a251ca62 - update ip
2 years ago
李光春 b664b74537 - add delete
2 years ago
李光春 f900440188 - add vendor
2 years ago
李光春 f6ff746740 - update model
2 years ago
李光春 b2786dcae5 - update api ip
2 years ago
李光春 a9ae30ac48 - add api ip
2 years ago
李光春 105552f60e - update gorm
2 years ago
李光春 09d1f5322c - update
2 years ago
李光春 9ae52fb881 - update gorm
2 years ago
李光春 b491020dfb - update gin
2 years ago
李光春 ee6dd7c3ba - update gin
2 years ago
李光春 6d83b387ca - update
2 years ago
李光春 fabc48f5e5 - update
2 years ago
李光春 99d2f42cda - update json to string
2 years ago
李光春 61c3b48176 - update
2 years ago
李光春 5ad38c7138 - update debug
2 years ago
李光春 ab40cf6a20 - update
2 years ago
李光春 2556f543eb - update gorm
2 years ago
李光春 7e275ff8c6 - update gorm
2 years ago
李光春 f14ff39dde - update
2 years ago
李光春 cca4e98da6 - update gorm
2 years ago
李光春 6084e24c15 - update zap
2 years ago
李光春 621a70a569 - update
2 years ago
李光春 0e6ae9bd78 - update
2 years ago
李光春 0cbf7efddc - update zap
2 years ago
李光春 5eaaf329a8 - update
2 years ago
李光春 acbcfac098 - update
2 years ago
李光春 bfdf600dd3 - update api mongo
2 years ago
李光春 216598bc9d - update gorm
2 years ago
李光春 e6c2c7668b - update mongo
2 years ago
李光春 242112f779 - update mongo
2 years ago
李光春 04f079daf9 - update
2 years ago
李光春 138cb2a350 - update mongo time
2 years ago
李光春 6665e8597c - update mongo
2 years ago
李光春 e27626dd15 - update type
2 years ago
李光春 b615f8f1b1 - update gin
2 years ago
李光春 101ea45f6c - update gin
2 years ago
李光春 b7c4b649c6 - update gin mongo
2 years ago
李光春 5c291f12b8 - add mongo
2 years ago
李光春 d94deede67 - update log
2 years ago
李光春 90184bd169 - update log
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 8707ba3981 - update log
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 09a09e3888 - update
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 510aa3e051 - update
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 1d673bd7a2 - update gin
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 b6ebc957a4 - update gin
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 65ec9457b0 - update logrus.console
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 54da153cab - update log
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 5d338d96aa - update log
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 8c6cc53950 - 删除mongo
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 9d616ddad0 - 删除mongo
continuous-integration/drone/push Build is passing Details
2 years ago
李光春 ec422e395e - add error log
continuous-integration/drone/tag Build is passing Details
continuous-integration/drone/push Build was killed Details
2 years ago
李光春 ab7f32f5a6 - update
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 ca988d4afd - update
continuous-integration/drone/push Build is failing Details
continuous-integration/drone/tag Build is failing Details
2 years ago
李光春 81d369d2aa - update
continuous-integration/drone/push Build is failing Details
2 years ago
李光春 73a2861132 - update middleware
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 c0fde6aa14 - update api
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 0105a46f00 - update api
2 years ago
李光春 54e83ece3b - update gorm
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 55ce9d44a9 - update
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 b2d2318593 - update
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 df4e46c01c - update gorm
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/tag Build is passing Details
2 years ago
李光春 7b4d7fdf7f - update gorm
continuous-integration/drone/push Build is passing Details
2 years ago

5
.gitignore vendored

@ -5,5 +5,6 @@
.vscode
*.log
gomod.sh
time_test.go
/vendor/
*_test.go
mod.sh
/vendor/

@ -1,7 +1,9 @@
<h1>
<a href="https://www.dtapp.net/">Golang Log</a>
<a href="https://www.dtapp.net/">Golang</a>
</h1>
📦 Golang 日志
[comment]: <> (go)
[![godoc](https://pkg.go.dev/badge/go.dtapp.net/golog?status.svg)](https://pkg.go.dev/go.dtapp.net/golog)
[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/golog/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/golog)
@ -10,6 +12,6 @@
#### 安装
```go
go get -v -u go.dtapp.net/golog
```shell
go get -v -u go.dtapp.net/golog@v1.0.109
```

@ -1,94 +0,0 @@
package golog
import (
"errors"
"go.dtapp.net/dorm"
"go.dtapp.net/goip"
"gorm.io/gorm"
"os"
"runtime"
)
// ApiClient 接口
type ApiClient struct {
gormClient *gorm.DB // 数据库驱动
mongoClient *dorm.MongoClient // 数据库驱动
config struct {
logType string // 日志类型
tableName string // 表名
databaseName string // 库名
collectionName string // 表名
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
} // 配置
}
// NewApiClient 创建接口实例化
// WithGormClient && WithTableName
// WithMongoCollectionClient && WithDatabaseName && WithCollectionName
func NewApiClient(attrs ...*OperationAttr) (*ApiClient, error) {
c := &ApiClient{}
for _, attr := range attrs {
if attr.gormClient != nil {
c.gormClient = attr.gormClient
c.config.logType = attr.logType
}
if attr.mongoClient != nil {
c.mongoClient = attr.mongoClient
c.config.logType = attr.logType
}
if attr.tableName != "" {
c.config.tableName = attr.tableName
}
if attr.databaseName != "" {
c.config.databaseName = attr.databaseName
}
if attr.collectionName != "" {
c.config.collectionName = attr.collectionName
}
}
switch c.config.logType {
case logTypeGorm:
if c.gormClient == nil {
return nil, errors.New("驱动不能为空")
}
if c.config.tableName == "" {
return nil, errors.New("表名不能为空")
}
err := c.gormClient.Table(c.config.tableName).AutoMigrate(&apiPostgresqlLog{})
if err != nil {
return nil, errors.New("创建表失败:" + err.Error())
}
case logTypeMongo:
if c.mongoClient.Db == nil {
return nil, errors.New("没有设置驱动")
}
if c.config.databaseName == "" {
return nil, errors.New("没有设置库名")
}
if c.config.collectionName == "" {
return nil, errors.New("没有设置表名")
}
default:
return nil, errors.New("驱动为空")
}
hostname, _ := os.Hostname()
c.config.hostname = hostname
c.config.insideIp = goip.GetInsideIp()
c.config.goVersion = runtime.Version()
return c, nil
}

@ -1,112 +1,89 @@
package golog
import (
"go.dtapp.net/dorm"
"go.dtapp.net/gojson"
"context"
"errors"
"go.dtapp.net/gorequest"
"gorm.io/datatypes"
"gorm.io/gorm"
"log"
"time"
"unicode/utf8"
)
// 模型结构体
type apiPostgresqlLog struct {
LogId uint `gorm:"primaryKey" json:"log_id,omitempty"` //【记录】编号
RequestTime time.Time `gorm:"index" json:"request_time,omitempty"` //【请求】时间
RequestUri string `json:"request_uri,omitempty"` //【请求】链接
RequestUrl string `json:"request_url,omitempty"` //【请求】链接
RequestApi string `gorm:"index" json:"request_api,omitempty"` //【请求】接口
RequestMethod string `gorm:"index" json:"request_method,omitempty"` //【请求】方式
RequestParams datatypes.JSON `gorm:"type:jsonb" json:"request_params,omitempty"` //【请求】参数
RequestHeader datatypes.JSON `gorm:"type:jsonb" json:"request_header,omitempty"` //【请求】头部
ResponseHeader datatypes.JSON `gorm:"type:jsonb" json:"response_header,omitempty"` //【返回】头部
ResponseStatusCode int `gorm:"index" json:"response_status_code,omitempty"` //【返回】状态码
ResponseBody datatypes.JSON `gorm:"type:jsonb" json:"response_body,omitempty"` //【返回】内容
ResponseContentLength int64 `json:"response_content_length,omitempty"` //【返回】大小
ResponseTime time.Time `gorm:"index" json:"response_time,omitempty"` //【返回】时间
SystemHostName string `gorm:"index" json:"system_host_name,omitempty"` //【系统】主机名
SystemInsideIp string `gorm:"index" json:"system_inside_ip,omitempty"` //【系统】内网ip
GoVersion string `gorm:"index" json:"go_version,omitempty"` //【程序】Go版本
// ApiGorm 接口日志
type ApiGorm struct {
gormClient *gorm.DB // 数据库驱动
config struct {
systemHostname string // 主机名
systemOs string // 系统类型
systemVersion string // 系统版本
systemKernel string // 系统内核
systemKernelVersion string // 系统内核版本
systemBootTime uint64 // 系统开机时间
cpuCores int // CPU核数
cpuModelName string // CPU型号名称
cpuMhz float64 // CPU兆赫
systemInsideIp string // 内网ip
systemOutsideIp string // 外网ip
goVersion string // go版本
sdkVersion string // sdk版本
}
gormConfig struct {
stats bool // 状态
tableName string // 表名
}
}
// 记录日志
func (c *ApiClient) gormRecord(postgresqlLog apiPostgresqlLog) error {
// ApiGormFun 接口日志驱动
type ApiGormFun func() *ApiGorm
if utf8.ValidString(string(postgresqlLog.ResponseBody)) == false {
log.Println("内容格式无法记录")
postgresqlLog.ResponseBody = datatypes.JSON("")
}
// NewApiGorm 创建接口实例化
func NewApiGorm(ctx context.Context, systemOutsideIp string, gormClient *gorm.DB, gormTableName string) (*ApiGorm, error) {
gl := &ApiGorm{}
postgresqlLog.SystemHostName = c.config.hostname
if postgresqlLog.SystemInsideIp == "" {
postgresqlLog.SystemInsideIp = c.config.insideIp
// 配置信息
if systemOutsideIp == "" {
return nil, errors.New("没有设置外网IP")
}
postgresqlLog.GoVersion = c.config.goVersion
gl.setConfig(ctx, systemOutsideIp)
return c.gormClient.Table(c.config.tableName).Create(&postgresqlLog).Error
}
if gormClient == nil {
gl.gormConfig.stats = false
} else {
gl.gormClient = gormClient
if gormTableName == "" {
return nil, errors.New("没有设置表名")
} else {
gl.gormConfig.tableName = gormTableName
}
// GormQuery 查询
func (c *ApiClient) GormQuery() *gorm.DB {
return c.gormClient.Table(c.config.tableName)
// 创建模型
gl.gormAutoMigrate(ctx)
gl.gormConfig.stats = true
}
return gl, nil
}
// GormMiddleware 中间件
func (c *ApiClient) GormMiddleware(request gorequest.Response) {
if request.ResponseHeader.Get("Content-Type") == "image/jpeg" || request.ResponseHeader.Get("Content-Type") == "image/png" {
log.Println("内容格式无法记录")
return
// Middleware 中间件
func (ag *ApiGorm) Middleware(ctx context.Context, request gorequest.Response) {
if ag.gormConfig.stats {
ag.gormMiddleware(ctx, request)
}
c.gormRecord(apiPostgresqlLog{
RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: datatypes.JSON(gojson.JsonEncodeNoError(request.RequestParams)), //【请求】参数
RequestHeader: datatypes.JSON(gojson.JsonEncodeNoError(request.RequestHeader)), //【请求】头部
ResponseHeader: datatypes.JSON(gojson.JsonEncodeNoError(request.ResponseHeader)), //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: request.ResponseBody, //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: request.ResponseTime, //【返回】时间
})
}
// GormMiddlewareXml 中间件
func (c *ApiClient) GormMiddlewareXml(request gorequest.Response) {
c.gormRecord(apiPostgresqlLog{
RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: datatypes.JSON(gojson.JsonEncodeNoError(request.RequestParams)), //【请求】参数
RequestHeader: datatypes.JSON(gojson.JsonEncodeNoError(request.RequestHeader)), //【请求】头部
ResponseHeader: datatypes.JSON(gojson.JsonEncodeNoError(request.ResponseHeader)), //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: datatypes.JSON(gojson.JsonEncodeNoError(dorm.XmlDecodeNoError(request.ResponseBody))), //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: request.ResponseTime, //【返回】时间
})
// MiddlewareXml 中间件
func (ag *ApiGorm) MiddlewareXml(ctx context.Context, request gorequest.Response) {
if ag.gormConfig.stats {
ag.gormMiddlewareXml(ctx, request)
}
}
// GormMiddlewareCustom 中间件
func (c *ApiClient) GormMiddlewareCustom(api string, request gorequest.Response) {
c.gormRecord(apiPostgresqlLog{
RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: api, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: datatypes.JSON(gojson.JsonEncodeNoError(request.RequestParams)), //【请求】参数
RequestHeader: datatypes.JSON(gojson.JsonEncodeNoError(request.RequestHeader)), //【请求】头部
ResponseHeader: datatypes.JSON(gojson.JsonEncodeNoError(request.ResponseHeader)), //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: request.ResponseBody, //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: request.ResponseTime, //【返回】时间
})
// MiddlewareCustom 中间件
func (ag *ApiGorm) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response) {
if ag.gormConfig.stats {
ag.gormMiddlewareCustom(ctx, api, request)
}
}

@ -0,0 +1,29 @@
package golog
import (
"context"
"go.dtapp.net/gorequest"
"runtime"
)
func (ag *ApiGorm) setConfig(ctx context.Context, systemOutsideIp string) {
info := getSystem()
ag.config.systemHostname = info.SystemHostname
ag.config.systemOs = info.SystemOs
ag.config.systemVersion = info.SystemVersion
ag.config.systemKernel = info.SystemKernel
ag.config.systemKernelVersion = info.SystemKernelVersion
ag.config.systemBootTime = info.SystemBootTime
ag.config.cpuCores = info.CpuCores
ag.config.cpuModelName = info.CpuModelName
ag.config.cpuMhz = info.CpuMhz
ag.config.systemInsideIp = gorequest.GetInsideIp(ctx)
ag.config.systemOutsideIp = systemOutsideIp
ag.config.goVersion = runtime.Version()
ag.config.sdkVersion = Version
}

@ -0,0 +1,24 @@
package golog
import (
"context"
"errors"
"go.dtapp.net/gotime"
)
// GormDeleteData 删除N天前数据
func (ag *ApiGorm) GormDeleteData(ctx context.Context, day int) error {
return ag.GormDeleteDataCustom(ctx, ag.gormConfig.tableName, day)
}
// GormDeleteDataCustom 删除N天前数据
func (ag *ApiGorm) GormDeleteDataCustom(ctx context.Context, tableName string, day int) error {
if ag.gormConfig.stats == false {
return nil
}
if tableName == "" {
return errors.New("没有设置表名")
}
return ag.gormClient.Table(tableName).Where("request_time < ?", gotime.Current().BeforeDay(day).Format()).Delete(&apiPostgresqlLog{}).Error
}

@ -0,0 +1,143 @@
package golog
import (
"context"
"go.dtapp.net/gojson"
"go.dtapp.net/gorequest"
"go.dtapp.net/gotrace_id"
"go.dtapp.net/gourl"
"log"
"time"
"unicode/utf8"
)
// 模型
type apiGormLog struct {
LogID int64 `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"` //【返回】数据
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 (ag *ApiGorm) gormAutoMigrate(ctx context.Context) {
if ag.gormConfig.stats == false {
return
}
err := ag.gormClient.Table(ag.gormConfig.tableName).AutoMigrate(&apiGormLog{})
if err != nil {
log.Printf("创建模型:%s", err)
}
}
// 记录日志
func (ag *ApiGorm) gormRecord(ctx context.Context, data apiGormLog) {
if ag.gormConfig.stats == false {
return
}
if utf8.ValidString(data.ResponseBody) == false {
data.ResponseBody = ""
}
data.SystemHostName = ag.config.systemHostname //【系统】主机名
data.SystemInsideIp = ag.config.systemInsideIp //【系统】内网ip
data.GoVersion = ag.config.goVersion //【程序】Go版本
data.SdkVersion = ag.config.systemVersion //【程序】Sdk版本
data.TraceID = gotrace_id.GetTraceIdContext(ctx) //【记录】跟踪编号
data.RequestIp = ag.config.systemOutsideIp //【请求】请求Ip
data.SystemOs = ag.config.systemOs //【系统】系统类型
data.SystemArch = ag.config.systemKernel //【系统】系统架构
err := ag.gormClient.Table(ag.gormConfig.tableName).Create(&data).Error
if err != nil {
log.Printf("记录接口日志错误:%s", err)
log.Printf("记录接口日志数据:%+v", data)
}
}
// 中间件
func (ag *ApiGorm) gormMiddleware(ctx context.Context, request gorequest.Response) {
data := apiGormLog{
RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: gourl.UriParse(request.RequestUri).Path, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: gojson.JsonEncodeNoError(request.RequestParams), //【请求】参数
RequestHeader: gojson.JsonEncodeNoError(request.RequestHeader), //【请求】头部
ResponseHeader: gojson.JsonEncodeNoError(request.ResponseHeader), //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseTime: request.ResponseTime, //【返回】时间
}
if !request.HeaderIsImg() {
if len(request.ResponseBody) > 0 {
data.ResponseBody = gojson.JsonEncodeNoError(gojson.JsonDecodeNoError(string(request.ResponseBody))) //【返回】数据
}
}
ag.gormRecord(ctx, data)
}
// 中间件
func (ag *ApiGorm) gormMiddlewareXml(ctx context.Context, request gorequest.Response) {
data := apiGormLog{
RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: gourl.UriParse(request.RequestUri).Path, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: gojson.JsonEncodeNoError(request.RequestParams), //【请求】参数
RequestHeader: gojson.JsonEncodeNoError(request.RequestHeader), //【请求】头部
ResponseHeader: gojson.JsonEncodeNoError(request.ResponseHeader), //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseTime: request.ResponseTime, //【返回】时间
}
if !request.HeaderIsImg() {
if len(request.ResponseBody) > 0 {
data.ResponseBody = gojson.XmlEncodeNoError(gojson.XmlDecodeNoError(request.ResponseBody)) //【返回】内容
}
}
ag.gormRecord(ctx, data)
}
// 中间件
func (ag *ApiGorm) gormMiddlewareCustom(ctx context.Context, api string, request gorequest.Response) {
data := apiGormLog{
RequestTime: request.RequestTime, //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gourl.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: api, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: gojson.JsonEncodeNoError(request.RequestParams), //【请求】参数
RequestHeader: gojson.JsonEncodeNoError(request.RequestHeader), //【请求】头部
ResponseHeader: gojson.JsonEncodeNoError(request.ResponseHeader), //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseTime: request.ResponseTime, //【返回】时间
}
if !request.HeaderIsImg() {
if len(request.ResponseBody) > 0 {
data.ResponseBody = gojson.JsonEncodeNoError(gojson.JsonDecodeNoError(string(request.ResponseBody))) //【返回】数据
}
}
ag.gormRecord(ctx, data)
}

@ -1,103 +0,0 @@
package golog
import (
"go.dtapp.net/dorm"
"go.dtapp.net/gorequest"
"go.dtapp.net/gotime"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// 模型结构体
type apiMongoLog struct {
LogId primitive.ObjectID `json:"log_id,omitempty" bson:"_id,omitempty"` //【记录】编号
RequestTime int64 `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 map[string]interface{} `json:"request_params,omitempty" bson:"request_params,omitempty"` //【请求】参数
RequestHeader map[string]string `json:"request_header,omitempty" bson:"request_header,omitempty"` //【请求】头部
ResponseHeader map[string][]string `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 int64 `json:"response_time,omitempty" bson:"response_time,omitempty"` //【返回】时间
SystemHostName string `json:"system_host_name,omitempty" bson:"system_host_name,omitempty"` //【系统】主机名
SystemInsideIp string `json:"system_inside_ip,omitempty" bson:"system_inside_ip,omitempty"` //【系统】内网ip
GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【程序】Go版本
}
// 记录日志
func (c *ApiClient) mongoRecord(mongoLog apiMongoLog) error {
mongoLog.SystemHostName = c.config.hostname
if mongoLog.SystemInsideIp == "" {
mongoLog.SystemInsideIp = c.config.insideIp
}
mongoLog.GoVersion = c.config.goVersion
mongoLog.LogId = primitive.NewObjectID()
_, err := c.mongoClient.Database(c.config.databaseName).Collection(c.config.collectionName).InsertOne(mongoLog)
return err
}
// MongoQuery 查询
func (c *ApiClient) MongoQuery() *dorm.MongoClient {
return c.mongoClient.Database(c.config.databaseName).Collection(c.config.collectionName)
}
// MongoMiddleware 中间件
func (c *ApiClient) MongoMiddleware(request gorequest.Response) {
c.mongoRecord(apiMongoLog{
RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp(), //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: request.RequestParams, //【请求】参数
RequestHeader: request.RequestHeader, //【请求】头部
ResponseHeader: request.ResponseHeader, //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: request.ResponseBody, //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp(), //【返回】时间
})
}
// MongoMiddlewareXml 中间件
func (c *ApiClient) MongoMiddlewareXml(request gorequest.Response) {
c.mongoRecord(apiMongoLog{
RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp(), //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: request.RequestParams, //【请求】参数
RequestHeader: request.RequestHeader, //【请求】头部
ResponseHeader: request.ResponseHeader, //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: dorm.XmlDecodeNoError(request.ResponseBody), //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp(), //【返回】时间
})
}
// MongoMiddlewareCustom 中间件
func (c *ApiClient) MongoMiddlewareCustom(api string, request gorequest.Response) {
c.mongoRecord(apiMongoLog{
RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp(), //【请求】时间
RequestUri: request.RequestUri, //【请求】链接
RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接
RequestApi: api, //【请求】接口
RequestMethod: request.RequestMethod, //【请求】方式
RequestParams: request.RequestParams, //【请求】参数
RequestHeader: request.RequestHeader, //【请求】头部
ResponseHeader: request.ResponseHeader, //【返回】头部
ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码
ResponseBody: request.ResponseBody, //【返回】内容
ResponseContentLength: request.ResponseContentLength, //【返回】大小
ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp(), //【返回】时间
})
}

@ -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 (al *ApiSLog) ConfigSLogClientFun(sLogFun SLogFun) {
sLog := sLogFun()
if sLog != nil {
al.slog.client = sLog
al.slog.status = true
}
}
// ConfigSLogResultClientFun 日志配置然后返回
func (al *ApiSLog) ConfigSLogResultClientFun(sLogFun SLogFun) *ApiSLog {
sLog := sLogFun()
if sLog != nil {
al.slog.client = sLog
al.slog.status = true
}
return al
}

@ -0,0 +1,125 @@
package golog
import (
"context"
"go.dtapp.net/gojson"
"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 (al *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: gojson.JsonDecodeNoError(string(request.ResponseBody)),
ResponseTime: request.ResponseTime,
}
if al.slog.status {
al.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 (al *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: gojson.XmlDecodeNoError(request.ResponseBody),
ResponseTime: request.ResponseTime,
}
if al.slog.status {
al.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 (al *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: gojson.JsonDecodeNoError(string(request.ResponseBody)),
ResponseTime: request.ResponseTime,
}
if al.slog.status {
al.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,
)
}
}

@ -0,0 +1,47 @@
package golog
import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"
)
type systemResult struct {
SystemHostname string // 主机名
SystemOs string // 系统类型
SystemVersion string // 系统版本
SystemKernel string // 系统内核
SystemKernelVersion string // 系统内核版本
SystemUpTime uint64 // 系统运行时间
SystemBootTime uint64 // 系统开机时间
CpuCores int // CPU核数
CpuModelName string // CPU型号名称
CpuMhz float64 // CPU兆赫
}
func getSystem() (result systemResult) {
hInfo, _ := host.Info()
result.SystemHostname = hInfo.Hostname
result.SystemOs = hInfo.OS
result.SystemVersion = hInfo.PlatformVersion
result.SystemKernel = hInfo.KernelArch
result.SystemKernelVersion = hInfo.KernelVersion
result.SystemUpTime = hInfo.Uptime
if hInfo.BootTime != 0 {
result.SystemBootTime = hInfo.BootTime
}
hCpu, _ := cpu.Times(true)
result.CpuCores = len(hCpu)
cInfo, _ := cpu.Info()
if len(cInfo) > 0 {
result.CpuModelName = cInfo[0].ModelName
result.CpuMhz = cInfo[0].Mhz
}
return result
}

121
gin.go

@ -1,121 +0,0 @@
package golog
import (
"bytes"
"encoding/json"
"errors"
"github.com/gin-gonic/gin"
"go.dtapp.net/dorm"
"go.dtapp.net/goip"
"gorm.io/gorm"
"os"
"runtime"
)
// GinClient 框架
type GinClient struct {
gormClient *gorm.DB // 数据库驱动
mongoClient *dorm.MongoClient // 数据库驱动
ipService *goip.Client // ip服务
config struct {
logType string // 日志类型
tableName string // 表名
databaseName string // 库名
collectionName string // 表名
insideIp string // 内网ip
hostname string // 主机名
goVersion string // go版本
} // 配置
}
// NewGinClient 创建框架实例化
// WithGormClient && WithTableName
// WithMongoCollectionClient && WithDatabaseName && WithCollectionName
func NewGinClient(attrs ...*OperationAttr) (*GinClient, error) {
c := &GinClient{}
for _, attr := range attrs {
if attr.gormClient != nil {
c.gormClient = attr.gormClient
c.config.logType = attr.logType
}
if attr.mongoClient != nil {
c.mongoClient = attr.mongoClient
c.config.logType = attr.logType
}
if attr.tableName != "" {
c.config.tableName = attr.tableName
}
if attr.databaseName != "" {
c.config.databaseName = attr.databaseName
}
if attr.collectionName != "" {
c.config.collectionName = attr.collectionName
}
if attr.ipService != nil {
c.ipService = attr.ipService
}
}
switch c.config.logType {
case logTypeGorm:
if c.gormClient == nil {
return nil, errors.New("没有设置驱动")
}
if c.config.tableName == "" {
return nil, errors.New("没有设置表名")
}
err := c.gormClient.Table(c.config.tableName).AutoMigrate(&ginPostgresqlLog{})
if err != nil {
return nil, errors.New("创建表失败:" + err.Error())
}
case logTypeMongo:
if c.mongoClient.Db == nil {
return nil, errors.New("没有设置驱动")
}
if c.config.databaseName == "" {
return nil, errors.New("没有设置库名")
}
if c.config.collectionName == "" {
return nil, errors.New("没有设置表名")
}
default:
return nil, errors.New("驱动为空")
}
hostname, _ := os.Hostname()
c.config.hostname = hostname
c.config.insideIp = goip.GetInsideIp()
c.config.goVersion = runtime.Version()
return c, nil
}
type bodyLogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (w bodyLogWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}
func (w bodyLogWriter) WriteString(s string) (int, error) {
w.body.WriteString(s)
return w.ResponseWriter.WriteString(s)
}
func (c *GinClient) jsonUnmarshal(data string) (result interface{}) {
_ = json.Unmarshal([]byte(data), &result)
return
}

@ -2,82 +2,126 @@ package golog
import (
"bytes"
"encoding/json"
"context"
"errors"
"github.com/gin-gonic/gin"
"go.dtapp.net/gojson"
"go.dtapp.net/gorequest"
"go.dtapp.net/gotime"
"go.dtapp.net/goxml"
"gorm.io/datatypes"
"gorm.io/gorm"
"io/ioutil"
"net"
"time"
)
// 模型结构体
type ginPostgresqlLog struct {
LogId uint `gorm:"primaryKey" json:"log_id,omitempty"` //【记录】编号
TraceId string `gorm:"index" json:"trace_id,omitempty"` //【系统】链编号
RequestTime time.Time `gorm:"index" json:"request_time,omitempty"` //【请求】时间
RequestUri string `json:"request_uri,omitempty"` //【请求】请求链接 域名+路径+参数
RequestUrl string `json:"request_url,omitempty"` //【请求】请求链接 域名+路径
RequestApi string `gorm:"index" json:"request_api,omitempty"` //【请求】请求接口 路径
RequestMethod string `gorm:"index" 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 datatypes.JSON `gorm:"type:jsonb" json:"request_body,omitempty"` //【请求】请求主体
RequestUrlQuery datatypes.JSON `gorm:"type:jsonb" json:"request_url_query,omitempty"` //【请求】请求URL参数
RequestIp string `gorm:"index" json:"request_ip,omitempty"` //【请求】请求客户端Ip
RequestIpCountry string `gorm:"index" json:"request_ip_country,omitempty"` //【请求】请求客户端城市
RequestIpRegion string `gorm:"index" json:"request_ip_region,omitempty"` //【请求】请求客户端区域
RequestIpProvince string `gorm:"index" json:"request_ip_province,omitempty"` //【请求】请求客户端省份
RequestIpCity string `gorm:"index" json:"request_ip_city,omitempty"` //【请求】请求客户端城市
RequestIpIsp string `gorm:"index" json:"request_ip_isp,omitempty"` //【请求】请求客户端运营商
RequestHeader datatypes.JSON `gorm:"type:jsonb" json:"request_header,omitempty"` //【请求】请求头
ResponseTime time.Time `gorm:"index" json:"response_time,omitempty"` //【返回】时间
ResponseCode int `gorm:"index" json:"response_code,omitempty"` //【返回】状态码
ResponseMsg string `json:"response_msg,omitempty"` //【返回】描述
ResponseData datatypes.JSON `gorm:"type:jsonb" json:"response_data,omitempty"` //【返回】数据
CostTime int64 `json:"cost_time,omitempty"` //【系统】花费时间
SystemHostName string `gorm:"index" json:"system_host_name,omitempty"` //【系统】主机名
SystemInsideIp string `gorm:"index" json:"system_inside_ip,omitempty"` //【系统】内网ip
GoVersion string `gorm:"index" json:"go_version,omitempty"` //【程序】Go版本
// GinGorm 框架日志
type GinGorm struct {
gormClient *gorm.DB // 数据库驱动
config struct {
systemHostname string // 主机名
systemOs string // 系统类型
systemVersion string // 系统版本
systemKernel string // 系统内核
systemKernelVersion string // 系统内核版本
systemBootTime uint64 // 系统开机时间
cpuCores int // CPU核数
cpuModelName string // CPU型号名称
cpuMhz float64 // CPU兆赫
systemInsideIp string // 内网ip
systemOutsideIp string // 外网ip
goVersion string // go版本
sdkVersion string // sdk版本
}
gormConfig struct {
stats bool // 状态
tableName string // 表名
}
}
// gormRecord 记录日志
func (c *GinClient) gormRecord(postgresqlLog ginPostgresqlLog) error {
// GinGormFun *GinGorm 框架日志驱动
type GinGormFun func() *GinGorm
// NewGinGorm 创建框架实例化
func NewGinGorm(ctx context.Context, systemOutsideIp string, gormClient *gorm.DB, gormTableName string) (*GinGorm, error) {
gg := &GinGorm{}
postgresqlLog.SystemHostName = c.config.hostname
if postgresqlLog.SystemInsideIp == "" {
postgresqlLog.SystemInsideIp = c.config.insideIp
// 配置信息
if systemOutsideIp == "" {
return nil, errors.New("没有设置外网IP")
}
postgresqlLog.GoVersion = c.config.goVersion
gg.setConfig(ctx, systemOutsideIp)
if gormClient == nil {
gg.gormConfig.stats = false
} else {
gg.gormClient = gormClient
if gormTableName == "" {
return nil, errors.New("没有设置表名")
} else {
gg.gormConfig.tableName = gormTableName
}
return c.gormClient.Table(c.config.tableName).Create(&postgresqlLog).Error
// 创建模型
gg.gormAutoMigrate(ctx)
gg.gormConfig.stats = true
}
return gg, nil
}
// GormQuery 查询
func (c *GinClient) GormQuery() *gorm.DB {
return c.gormClient.Table(c.config.tableName)
type bodyGormWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
// GormMiddleware 中间件
func (c *GinClient) GormMiddleware() gin.HandlerFunc {
func (w bodyGormWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}
func (w bodyGormWriter) WriteString(s string) (int, error) {
w.body.WriteString(s)
return w.ResponseWriter.WriteString(s)
}
func (gg *GinGorm) jsonUnmarshal(data string) (result interface{}) {
_ = gojson.Unmarshal([]byte(data), &result)
return
}
// Middleware 中间件
func (gg *GinGorm) Middleware() gin.HandlerFunc {
return func(ginCtx *gin.Context) {
// 开始时间
startTime := gotime.Current().TimestampWithMillisecond()
requestTime := gotime.Current().Time
// 获取
data, _ := ioutil.ReadAll(ginCtx.Request.Body)
// 复用
ginCtx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(data))
blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: ginCtx.Writer}
// 获取全部内容
requestBody := gorequest.NewParams()
queryParams := ginCtx.Request.URL.Query() // 请求URL参数
for key, values := range queryParams {
for _, value := range values {
requestBody.Set(key, value)
}
}
var dataMap map[string]interface{}
rawData, _ := ginCtx.GetRawData() // 请求内容参数
if gojson.IsValidJSON(string(rawData)) {
dataMap = gojson.JsonDecodeNoError(string(rawData))
} else {
dataMap = gojson.ParseQueryString(string(rawData))
}
for key, value := range dataMap {
requestBody.Set(key, value)
}
// 重新赋值
ginCtx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(rawData))
blw := &bodyGormWriter{body: bytes.NewBufferString(""), ResponseWriter: ginCtx.Writer}
ginCtx.Writer = blw
// 处理请求
@ -87,98 +131,15 @@ func (c *GinClient) GormMiddleware() gin.HandlerFunc {
responseCode := ginCtx.Writer.Status()
responseBody := blw.body.String()
//结束时间
// 结束时间
endTime := gotime.Current().TimestampWithMillisecond()
responseTime := gotime.Current().Time
go func() {
// 解析请求内容
var xmlBody map[string]string
var jsonBody map[string]interface{}
_ = json.Unmarshal(data, &jsonBody)
if len(jsonBody) <= 0 {
xmlBody = goxml.XmlDecode(string(data))
}
requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp := "", "", "", "", ""
if c.ipService != nil {
if net.ParseIP(ginCtx.ClientIP()).To4() != nil {
// 判断是不是IPV4
_, info := c.ipService.Ipv4(ginCtx.ClientIP())
requestClientIpCountry = info.Country
requestClientIpRegion = info.Region
requestClientIpProvince = info.Province
requestClientIpCity = info.City
requestClientIpIsp = info.ISP
} else if net.ParseIP(ginCtx.ClientIP()).To16() != nil {
// 判断是不是IPV6
info := c.ipService.Ipv6(ginCtx.ClientIP())
requestClientIpCountry = info.Country
requestClientIpProvince = info.Province
requestClientIpCity = info.City
}
}
// 记录
if c.gormClient != nil {
host := ""
if ginCtx.Request.TLS == nil {
host = "http://" + ginCtx.Request.Host
} else {
host = "https://" + ginCtx.Request.Host
}
if len(jsonBody) > 0 {
c.gormRecord(ginPostgresqlLog{
TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号
RequestTime: requestTime, //【请求】时间
RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接
RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接
RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口
RequestMethod: ginCtx.Request.Method, //【请求】请求方式
RequestProto: ginCtx.Request.Proto, //【请求】请求协议
RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA
RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer
RequestBody: datatypes.JSON(gojson.JsonEncodeNoError(jsonBody)), //【请求】请求主体
RequestUrlQuery: datatypes.JSON(gojson.JsonEncodeNoError(ginCtx.Request.URL.Query())), //【请求】请求URL参数
RequestIp: ginCtx.ClientIP(), //【请求】请求客户端Ip
RequestIpCountry: requestClientIpCountry, //【请求】请求客户端城市
RequestIpRegion: requestClientIpRegion, //【请求】请求客户端区域
RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份
RequestIpCity: requestClientIpCity, //【请求】请求客户端城市
RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商
RequestHeader: datatypes.JSON(gojson.JsonEncodeNoError(ginCtx.Request.Header)), //【请求】请求头
ResponseTime: gotime.Current().Time, //【返回】时间
ResponseCode: responseCode, //【返回】状态码
ResponseData: datatypes.JSON(responseBody), //【返回】数据
CostTime: endTime - startTime, //【系统】花费时间
})
} else {
c.gormRecord(ginPostgresqlLog{
TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号
RequestTime: requestTime, //【请求】时间
RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接
RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接
RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口
RequestMethod: ginCtx.Request.Method, //【请求】请求方式
RequestProto: ginCtx.Request.Proto, //【请求】请求协议
RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA
RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer
RequestBody: datatypes.JSON(gojson.JsonEncodeNoError(xmlBody)), //【请求】请求主体
RequestUrlQuery: datatypes.JSON(gojson.JsonEncodeNoError(ginCtx.Request.URL.Query())), //【请求】请求URL参数
RequestIp: ginCtx.ClientIP(), //【请求】请求客户端Ip
RequestIpCountry: requestClientIpCountry, //【请求】请求客户端城市
RequestIpRegion: requestClientIpRegion, //【请求】请求客户端区域
RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份
RequestIpCity: requestClientIpCity, //【请求】请求客户端城市
RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商
RequestHeader: datatypes.JSON(gojson.JsonEncodeNoError(ginCtx.Request.Header)), //【请求】请求头
ResponseTime: gotime.Current().Time, //【返回】时间
ResponseCode: responseCode, //【返回】状态码
ResponseData: datatypes.JSON(responseBody), //【返回】数据
CostTime: endTime - startTime, //【系统】花费时间
})
}
}
gg.recordJson(ginCtx, requestTime, requestBody, responseTime, responseCode, responseBody, endTime-startTime, gorequest.ClientIp(ginCtx.Request))
}()
}
}

@ -0,0 +1,29 @@
package golog
import (
"context"
"go.dtapp.net/gorequest"
"runtime"
)
func (gg *GinGorm) setConfig(ctx context.Context, systemOutsideIp string) {
info := getSystem()
gg.config.systemHostname = info.SystemHostname
gg.config.systemOs = info.SystemOs
gg.config.systemVersion = info.SystemVersion
gg.config.systemKernel = info.SystemKernel
gg.config.systemKernelVersion = info.SystemKernelVersion
gg.config.systemBootTime = info.SystemBootTime
gg.config.cpuCores = info.CpuCores
gg.config.cpuModelName = info.CpuModelName
gg.config.cpuMhz = info.CpuMhz
gg.config.systemInsideIp = gorequest.GetInsideIp(ctx)
gg.config.systemOutsideIp = systemOutsideIp
gg.config.sdkVersion = Version
gg.config.goVersion = runtime.Version()
}

@ -0,0 +1,24 @@
package golog
import (
"context"
"errors"
"go.dtapp.net/gotime"
)
// GormDeleteData 删除N天前数据
func (gg *GinGorm) GormDeleteData(ctx context.Context, day int) error {
return gg.GormDeleteDataCustom(ctx, gg.gormConfig.tableName, day)
}
// GormDeleteDataCustom 删除N天前数据
func (gg *GinGorm) GormDeleteDataCustom(ctx context.Context, tableName string, day int) error {
if gg.gormConfig.stats == false {
return nil
}
if tableName == "" {
return errors.New("没有设置表名")
}
return gg.gormClient.Table(tableName).Where("request_time < ?", gotime.Current().BeforeDay(day).Format()).Delete(&ginPostgresqlLog{}).Error
}

@ -0,0 +1,95 @@
package golog
import (
"context"
"github.com/gin-gonic/gin"
"go.dtapp.net/gojson"
"go.dtapp.net/gorequest"
"go.dtapp.net/gotrace_id"
"go.dtapp.net/gourl"
"log"
"time"
)
// 模型
type ginGormLog struct {
LogID int64 `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"` //【请求】协议
RequestBody string `gorm:"comment:【请求】参数" json:"request_body,omitempty"` //【请求】参数
RequestIP string `gorm:"index;comment:【请求】客户端IP" json:"request_ip,omitempty"` //【请求】客户端IP
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"` //【返回】状态码
ResponseData string `gorm:"comment:【返回】数据" json:"response_data,omitempty"` //【返回】数据
CostTime int64 `gorm:"comment:【系统】花费时间" json:"cost_time,omitempty"` //【系统】花费时间
SystemHostName string `gorm:"index;comment:【系统】主机名" json:"system_host_name,omitempty"` //【系统】主机名
SystemInsideIP string `gorm:"comment:【系统】内网ip" json:"system_inside_ip,omitempty"` //【系统】内网ip
SystemOs string `gorm:"index;comment:【系统】系统类型" json:"system_os,omitempty"` //【系统】系统类型
SystemArch string `gorm:"index;comment:【系统】系统架构" json:"system_arch,omitempty"` //【系统】系统架构
GoVersion string `gorm:"comment:【程序】Go版本" json:"go_version,omitempty"` //【程序】Go版本
SdkVersion string `gorm:"comment:【程序】Sdk版本" json:"sdk_version,omitempty"` //【程序】Sdk版本
}
// 创建模型
func (gg *GinGorm) gormAutoMigrate(ctx context.Context) {
if gg.gormConfig.stats == false {
return
}
err := gg.gormClient.Table(gg.gormConfig.tableName).AutoMigrate(&ginGormLog{})
if err != nil {
log.Printf("创建模型:%s\n", err)
}
}
// gormRecord 记录日志
func (gg *GinGorm) gormRecord(data ginGormLog) {
if gg.gormConfig.stats == false {
return
}
data.SystemHostName = gg.config.systemHostname //【系统】主机名
data.SystemInsideIP = gg.config.systemInsideIp //【系统】内网ip
data.GoVersion = gg.config.goVersion //【程序】Go版本
data.SdkVersion = gg.config.sdkVersion //【程序】Sdk版本
data.SystemOs = gg.config.systemOs //【系统】系统类型
data.SystemArch = gg.config.systemKernel //【系统】系统架构
err := gg.gormClient.Table(gg.gormConfig.tableName).Create(&data).Error
if err != nil {
log.Printf("记录框架日志错误:%s\n", err)
log.Printf("记录框架日志数据:%+v\n", data)
}
}
func (gg *GinGorm) recordJson(ginCtx *gin.Context, requestTime time.Time, requestBody gorequest.Params, responseTime time.Time, responseCode int, responseBody string, costTime int64, requestIp string) {
data := ginGormLog{
TraceID: gotrace_id.GetGinTraceId(ginCtx), //【系统】跟踪编号
RequestTime: requestTime, //【请求】时间
RequestURL: ginCtx.Request.RequestURI, //【请求】链接
RequestApi: gourl.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】接口
RequestMethod: ginCtx.Request.Method, //【请求】方式
RequestProto: ginCtx.Request.Proto, //【请求】协议
RequestBody: gojson.JsonEncodeNoError(requestBody), //【请求】参数
RequestIP: requestIp, //【请求】客户端IP
RequestHeader: gojson.JsonEncodeNoError(ginCtx.Request.Header), //【请求】头部
ResponseTime: responseTime, //【返回】时间
ResponseCode: responseCode, //【返回】状态码
ResponseData: responseBody, //【返回】数据
CostTime: costTime, //【系统】花费时间
}
if ginCtx.Request.TLS == nil {
data.RequestUri = "http://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】链接
} else {
data.RequestUri = "https://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】链接
}
gg.gormRecord(data)
}

@ -1,23 +0,0 @@
package gin_middleware
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"go.dtapp.net/gostring"
)
// CustomTraceIdContext 自定义设置跟踪编号上下文
func CustomTraceIdContext() context.Context {
return context.WithValue(context.Background(), "trace_id", gostring.GetUuId())
}
// SetTraceIdContext 设置跟踪编号上下文
func SetTraceIdContext(c *gin.Context) context.Context {
return context.WithValue(context.Background(), "trace_id", GetTraceId(c))
}
// GetTraceIdContext 通过上下文获取跟踪编号
func GetTraceIdContext(ctx context.Context) string {
return fmt.Sprintf("%s", ctx.Value("trace_id"))
}

@ -1,16 +0,0 @@
package gin_middleware
import (
"context"
"testing"
)
func TestContext(t *testing.T) {
ctx := context.Background()
t.Log(ctx)
ctx = context.WithValue(ctx, "k1", "测试")
t.Log()
}

@ -1,25 +0,0 @@
package gin_middleware
import (
"fmt"
"github.com/gin-gonic/gin"
"go.dtapp.net/gostring"
)
// SetTraceId 设置跟踪编号 https://www.jianshu.com/p/2a1a74ad3c3a
func SetTraceId() gin.HandlerFunc {
return func(c *gin.Context) {
requestId := c.Request.Header.Get("X-Request-Id")
if requestId == "" {
requestId = gostring.GetUuId()
}
c.Set("trace_id", requestId)
c.Writer.Header().Set("X-Request-Id", requestId)
c.Next()
}
}
// GetTraceId 通过gin中间件获取跟踪编号
func GetTraceId(c *gin.Context) string {
return fmt.Sprintf("%s", c.MustGet("trace_id"))
}

@ -1 +0,0 @@
package gin_middleware

@ -1,186 +0,0 @@
package golog
import (
"bytes"
"encoding/json"
"github.com/gin-gonic/gin"
"go.dtapp.net/dorm"
"go.dtapp.net/gorequest"
"go.dtapp.net/gotime"
"go.dtapp.net/goxml"
"go.mongodb.org/mongo-driver/bson/primitive"
"io/ioutil"
"net"
)
// 模型结构体
type ginMongoLog struct {
LogId primitive.ObjectID `json:"log_id,omitempty" bson:"_id,omitempty"` //【记录】编号
TraceId string `json:"trace_id,omitempty" bson:"trace_id,omitempty"` //【系统】链编号
RequestTime int64 `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 string `json:"request_ip,omitempty" bson:"request_ip,omitempty"` //【请求】请求客户端Ip
RequestIpCountry string `json:"request_ip_country,omitempty" bson:"request_ip_country,omitempty"` //【请求】请求客户端城市
RequestIpRegion string `json:"request_ip_region,omitempty" bson:"request_ip_region,omitempty"` //【请求】请求客户端区域
RequestIpProvince string `json:"request_ip_province,omitempty" bson:"request_ip_province,omitempty"` //【请求】请求客户端省份
RequestIpCity string `json:"request_ip_city,omitempty" bson:"request_ip_city,omitempty"` //【请求】请求客户端城市
RequestIpIsp string `json:"request_ip_isp,omitempty" bson:"request_ip_isp,omitempty"` //【请求】请求客户端运营商
RequestHeader interface{} `json:"request_header,omitempty" bson:"request_header,omitempty"` //【请求】请求头
ResponseTime int64 `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"` //【系统】花费时间
SystemHostName string `json:"system_host_name,omitempty" bson:"system_host_name,omitempty"` //【系统】主机名
SystemInsideIp string `json:"system_inside_ip,omitempty" bson:"system_inside_ip,omitempty"` //【系统】内网ip
GoVersion string `json:"go_version,omitempty" bson:"go_version,omitempty"` //【程序】Go版本
}
// 记录日志
func (c *GinClient) mongoRecord(mongoLog ginMongoLog) error {
mongoLog.SystemHostName = c.config.hostname
if mongoLog.SystemInsideIp == "" {
mongoLog.SystemInsideIp = c.config.insideIp
}
mongoLog.GoVersion = c.config.goVersion
mongoLog.LogId = primitive.NewObjectID()
_, err := c.mongoClient.Database(c.config.databaseName).Collection(c.config.collectionName).InsertOne(mongoLog)
return err
}
// MongoQuery 查询
func (c *GinClient) MongoQuery() *dorm.MongoClient {
return c.mongoClient.Database(c.config.databaseName).Collection(c.config.collectionName)
}
// MongoMiddleware 中间件
func (c *GinClient) MongoMiddleware() gin.HandlerFunc {
return func(ginCtx *gin.Context) {
// 开始时间
startTime := gotime.Current().TimestampWithMillisecond()
requestTime := gotime.Current().Time
// 获取
data, _ := ioutil.ReadAll(ginCtx.Request.Body)
// 复用
ginCtx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(data))
blw := &bodyLogWriter{body: bytes.NewBufferString(""), ResponseWriter: ginCtx.Writer}
ginCtx.Writer = blw
// 处理请求
ginCtx.Next()
// 响应
responseCode := ginCtx.Writer.Status()
responseBody := blw.body.String()
//结束时间
endTime := gotime.Current().TimestampWithMillisecond()
go func() {
// 解析请求内容
var xmlBody map[string]string
var jsonBody map[string]interface{}
_ = json.Unmarshal(data, &jsonBody)
if len(jsonBody) <= 0 {
xmlBody = goxml.XmlDecode(string(data))
}
requestClientIpCountry, requestClientIpRegion, requestClientIpProvince, requestClientIpCity, requestClientIpIsp := "", "", "", "", ""
if c.ipService != nil {
if net.ParseIP(ginCtx.ClientIP()).To4() != nil {
// 判断是不是IPV4
_, info := c.ipService.Ipv4(ginCtx.ClientIP())
requestClientIpCountry = info.Country
requestClientIpRegion = info.Region
requestClientIpProvince = info.Province
requestClientIpCity = info.City
requestClientIpIsp = info.ISP
} else if net.ParseIP(ginCtx.ClientIP()).To16() != nil {
// 判断是不是IPV6
info := c.ipService.Ipv6(ginCtx.ClientIP())
requestClientIpCountry = info.Country
requestClientIpProvince = info.Province
requestClientIpCity = info.City
}
}
// 记录
if c.mongoClient != nil {
host := ""
if ginCtx.Request.TLS == nil {
host = "http://" + ginCtx.Request.Host
} else {
host = "https://" + ginCtx.Request.Host
}
if len(jsonBody) > 0 {
c.mongoRecord(ginMongoLog{
TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号
RequestTime: gotime.SetCurrent(requestTime).Timestamp(), //【请求】时间
RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接
RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接
RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口
RequestMethod: ginCtx.Request.Method, //【请求】请求方式
RequestProto: ginCtx.Request.Proto, //【请求】请求协议
RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA
RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer
RequestBody: jsonBody, //【请求】请求主体
RequestUrlQuery: ginCtx.Request.URL.Query(), //【请求】请求URL参数
RequestIp: ginCtx.ClientIP(), //【请求】请求客户端Ip
RequestIpCountry: requestClientIpCountry, //【请求】请求客户端城市
RequestIpRegion: requestClientIpRegion, //【请求】请求客户端区域
RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份
RequestIpCity: requestClientIpCity, //【请求】请求客户端城市
RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商
RequestHeader: ginCtx.Request.Header, //【请求】请求头
ResponseTime: gotime.SetCurrent(gotime.Current().Time).Timestamp(), //【返回】时间
ResponseCode: responseCode, //【返回】状态码
ResponseData: c.jsonUnmarshal(responseBody), //【返回】数据
CostTime: endTime - startTime, //【系统】花费时间
})
} else {
c.mongoRecord(ginMongoLog{
TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号
RequestTime: gotime.SetCurrent(requestTime).Timestamp(), //【请求】时间
RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接
RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接
RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口
RequestMethod: ginCtx.Request.Method, //【请求】请求方式
RequestProto: ginCtx.Request.Proto, //【请求】请求协议
RequestUa: ginCtx.Request.UserAgent(), //【请求】请求UA
RequestReferer: ginCtx.Request.Referer(), //【请求】请求referer
RequestBody: xmlBody, //【请求】请求主体
RequestUrlQuery: ginCtx.Request.URL.Query(), //【请求】请求URL参数
RequestIp: ginCtx.ClientIP(), //【请求】请求客户端Ip
RequestIpCountry: requestClientIpCountry, //【请求】请求客户端城市
RequestIpRegion: requestClientIpRegion, //【请求】请求客户端区域
RequestIpProvince: requestClientIpProvince, //【请求】请求客户端省份
RequestIpCity: requestClientIpCity, //【请求】请求客户端城市
RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商
RequestHeader: ginCtx.Request.Header, //【请求】请求头
ResponseTime: gotime.SetCurrent(gotime.Current().Time).Timestamp(), //【返回】时间
ResponseCode: responseCode, //【返回】状态码
ResponseData: c.jsonUnmarshal(responseBody), //【返回】数据
CostTime: endTime - startTime, //【系统】花费时间
})
}
}
}()
}
}

@ -0,0 +1,101 @@
package golog
import (
"bytes"
"context"
"github.com/gin-gonic/gin"
"go.dtapp.net/gojson"
"go.dtapp.net/gorequest"
"go.dtapp.net/gotime"
"io/ioutil"
)
// GinSLog 框架日志
type GinSLog struct {
slog struct {
status bool // 状态
client *SLog // 日志服务
}
}
// GinSLogFun 框架日志驱动
type GinSLogFun func() *GinSLog
// NewGinSLog 创建框架实例化
func NewGinSLog(ctx context.Context) (*GinSLog, error) {
c := &GinSLog{}
return c, nil
}
type bodySLogWriter struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (w bodySLogWriter) Write(b []byte) (int, error) {
w.body.Write(b)
return w.ResponseWriter.Write(b)
}
func (w bodySLogWriter) WriteString(s string) (int, error) {
w.body.WriteString(s)
return w.ResponseWriter.WriteString(s)
}
func (gl *GinSLog) jsonUnmarshal(data string) (result interface{}) {
_ = gojson.Unmarshal([]byte(data), &result)
return
}
// Middleware 中间件
func (gl *GinSLog) Middleware() gin.HandlerFunc {
return func(ginCtx *gin.Context) {
// 开始时间
startTime := gotime.Current().TimestampWithMillisecond()
requestTime := gotime.Current().Time
// 获取全部内容
requestAllContent := gorequest.NewParams()
queryParams := ginCtx.Request.URL.Query() // 请求URL参数
for key, values := range queryParams {
for _, value := range values {
requestAllContent.Set(key, value)
}
}
var dataMap map[string]interface{}
rawData, _ := ginCtx.GetRawData() // 请求内容参数
if gojson.IsValidJSON(string(rawData)) {
dataMap = gojson.JsonDecodeNoError(string(rawData))
} else {
dataMap = gojson.ParseQueryString(string(rawData))
}
for key, value := range dataMap {
requestAllContent.Set(key, value)
}
// 重新赋值
ginCtx.Request.Body = ioutil.NopCloser(bytes.NewBuffer(rawData))
blw := &bodySLogWriter{body: bytes.NewBufferString(""), ResponseWriter: ginCtx.Writer}
ginCtx.Writer = blw
// 处理请求
ginCtx.Next()
// 响应
responseCode := ginCtx.Writer.Status()
responseBody := blw.body.String()
// 结束时间
endTime := gotime.Current().TimestampWithMillisecond()
responseTime := gotime.Current().Time
go func() {
// 记录
gl.recordJson(ginCtx, requestTime, requestAllContent, responseTime, responseCode, responseBody, endTime-startTime, gorequest.ClientIp(ginCtx.Request))
}()
}
}

@ -0,0 +1,20 @@
package golog
// ConfigSLogClientFun 日志配置
func (gl *GinSLog) ConfigSLogClientFun(sLogFun SLogFun) {
sLog := sLogFun()
if sLog != nil {
gl.slog.client = sLog
gl.slog.status = true
}
}
// ConfigSLogResultClientFun 日志配置然后返回
func (gl *GinSLog) ConfigSLogResultClientFun(sLogFun SLogFun) *GinSLog {
sLog := sLogFun()
if sLog != nil {
gl.slog.client = sLog
gl.slog.status = true
}
return gl
}

@ -0,0 +1,22 @@
package golog
import (
"context"
)
// GinSLogCustom 框架自定义日志
type GinSLogCustom struct {
slog struct {
status bool // 状态
client *SLog // 日志服务
}
}
// GinSLogCustomFun 框架自定义日志驱动
type GinSLogCustomFun func() *GinSLogCustom
// NewGinSLogCustom 创建框架实例化
func NewGinSLogCustom(ctx context.Context) (*GinSLogCustom, error) {
c := &GinSLogCustom{}
return c, nil
}

@ -0,0 +1,20 @@
package golog
// ConfigSLogClientFun 日志配置
func (c *GinSLogCustom) ConfigSLogClientFun(sLogFun SLogFun) {
sLog := sLogFun()
if sLog != nil {
c.slog.client = sLog
c.slog.status = true
}
}
// ConfigSLogResultClientFun 日志配置然后返回
func (c *GinSLogCustom) ConfigSLogResultClientFun(sLogFun SLogFun) *GinSLogCustom {
sLog := sLogFun()
if sLog != nil {
c.slog.client = sLog
c.slog.status = true
}
return c
}

@ -0,0 +1,100 @@
package golog
import (
"fmt"
"github.com/gin-gonic/gin"
"go.dtapp.net/gojson"
"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
CustomID string `json:"custom_id,omitempty"` //【日志】自定义编号
CustomType string `json:"custom_type,omitempty"` //【日志】自定义类型
CustomContent string `json:"custom_content,omitempty"` //【日志】自定义内容
}
type GinCustomClientGinRecordOperation struct {
slogClient *SLog // 日志服务
data *ginSLogCustom // 数据
}
// GinRecord 记录日志
func (c *GinSLogCustom) GinRecord(ginCtx *gin.Context) *GinCustomClientGinRecordOperation {
operation := &GinCustomClientGinRecordOperation{
slogClient: c.slog.client,
}
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 = gojson.JsonEncodeNoError(ginCtx.Request.URL.Query()) //【请求】请求URL参数
operation.data.RequestHeader = gojson.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,
"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,
"custom_id", o.data.CustomID,
"custom_type", o.data.CustomType,
"custom_content", o.data.CustomContent,
)
}

@ -0,0 +1,70 @@
package golog
import (
"github.com/gin-gonic/gin"
"go.dtapp.net/gorequest"
"go.dtapp.net/gotrace_id"
"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"` //【请求】请求协议
RequestBody map[string]interface{} `json:"request_body,omitempty"` //【请求】请求参数
RequestIP string `json:"request_ip,omitempty"` //【请求】请求客户端IP
RequestHeader map[string][]string `json:"request_header,omitempty"` //【请求】请求头
ResponseTime time.Time `json:"response_time,omitempty"` //【返回】时间
ResponseCode int `json:"response_code,omitempty"` //【返回】状态码
ResponseData string `json:"response_data,omitempty"` //【返回】数据
CostTime int64 `json:"cost_time,omitempty"` //【系统】花费时间
}
// record 记录日志
func (gl *GinSLog) record(msg string, data ginSLog) {
gl.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_body", data.RequestBody,
"request_ip", data.RequestIP,
"request_header", data.RequestHeader,
"response_time", data.ResponseTime,
"response_code", data.ResponseCode,
"response_data", data.ResponseData,
"cost_time", data.CostTime,
)
}
func (gl *GinSLog) recordJson(ginCtx *gin.Context, requestTime time.Time, request_body gorequest.Params, responseTime time.Time, responseCode int, responseBody string, costTime int64, requestIp string) {
data := ginSLog{
TraceID: gotrace_id.GetGinTraceId(ginCtx), //【系统】跟踪编号
RequestTime: requestTime, //【请求】时间
RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接
RequestApi: gourl.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口
RequestMethod: ginCtx.Request.Method, //【请求】请求方式
RequestProto: ginCtx.Request.Proto, //【请求】请求协议
RequestBody: request_body, //【请求】请求参数
RequestIP: requestIp, //【请求】请求客户端IP
RequestHeader: ginCtx.Request.Header, //【请求】请求头
ResponseTime: responseTime, //【返回】时间
ResponseCode: responseCode, //【返回】状态码
ResponseData: responseBody, //【返回】数据
CostTime: costTime, //【系统】花费时间
}
if ginCtx.Request.TLS == nil {
data.RequestUri = "http://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接
} else {
data.RequestUri = "https://" + ginCtx.Request.Host + ginCtx.Request.RequestURI //【请求】请求链接
}
gl.record("json", data)
}

146
go.mod

@ -1,123 +1,57 @@
module go.dtapp.net/golog
go 1.18
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
go.dtapp.net/dorm v1.0.11
go.dtapp.net/goip v1.0.23
go.dtapp.net/gojson v1.0.1
go.dtapp.net/gorequest v1.0.23
go.dtapp.net/gostring v1.0.6
go.dtapp.net/gotime v1.0.5
go.dtapp.net/goxml v1.0.1
go.mongodb.org/mongo-driver v1.10.1
go.uber.org/zap v1.21.0
gorm.io/datatypes v1.0.7
gorm.io/driver/postgres v1.3.8
gorm.io/gorm v1.23.8
github.com/shirou/gopsutil v3.21.11+incompatible
go.dtapp.net/gojson v1.0.4
go.dtapp.net/gorequest v1.0.46
go.dtapp.net/gotime v1.0.6
go.dtapp.net/gotrace_id v1.0.8
go.dtapp.net/gourl v1.0.0
gorm.io/gorm v1.25.5
)
require (
gitee.com/chunanyong/dm v1.8.5 // indirect
gitee.com/chunanyong/zorm v1.5.9 // indirect
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/beego/beego/v2 v2.0.5 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/denisenkom/go-mssqldb v0.12.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/edsrzf/mmap-go v1.1.0 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/glebarez/go-sqlite v1.18.0 // indirect
github.com/glebarez/sqlite v1.4.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.0 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/go-redis/redis/v9 v9.0.0-beta.2 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.10 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.12.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.11.0 // indirect
github.com/jackc/pgx/v4 v4.16.1 // 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/goccy/go-json v0.10.2 // 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.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.10.6 // indirect
github.com/mailru/go-clickhouse/v2 v2.0.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-sqlite3 v1.14.14 // 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/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda // indirect
github.com/saracen/solidblock v0.0.0-20190426153529-45df20abab6f // indirect
github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/syndtr/goleveldb v1.0.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.5.4 // indirect
github.com/uptrace/bun v1.1.7 // indirect
github.com/uptrace/bun/dialect/mssqldialect v1.1.7 // indirect
github.com/uptrace/bun/dialect/mysqldialect v1.1.7 // indirect
github.com/uptrace/bun/dialect/pgdialect v1.1.7 // indirect
github.com/uptrace/bun/driver/pgdriver v1.1.7 // 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
go.dtapp.net/gorandom v1.0.1 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220805013720-a33c5aa5df48 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // 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/yusufpapurcu/wmi v1.2.3 // indirect
go.dtapp.net/gorandom v1.0.2 // indirect
go.dtapp.net/gostring v1.0.13 // indirect
golang.org/x/arch v0.6.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/driver/mysql v1.3.5 // indirect
gorm.io/driver/sqlite v1.3.6 // indirect
gorm.io/driver/sqlserver v1.3.2 // indirect
mellium.im/sasl v0.2.1 // indirect
modernc.org/b v1.0.2 // indirect
modernc.org/db v1.0.4 // indirect
modernc.org/file v1.0.3 // indirect
modernc.org/fileutil v1.1.0 // indirect
modernc.org/golex v1.0.1 // indirect
modernc.org/internal v1.0.4 // indirect
modernc.org/libc v1.16.19 // indirect
modernc.org/lldb v1.0.4 // indirect
modernc.org/mathutil v1.4.1 // indirect
modernc.org/memory v1.1.1 // indirect
modernc.org/ql v1.4.1 // indirect
modernc.org/sortutil v1.1.0 // indirect
modernc.org/sqlite v1.18.0 // indirect
modernc.org/strutil v1.1.2 // indirect
modernc.org/zappy v1.0.5 // indirect
xorm.io/builder v0.3.12 // indirect
xorm.io/xorm v1.3.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

1027
go.sum

File diff suppressed because it is too large Load Diff

@ -1,127 +0,0 @@
package golog
import (
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"time"
)
type ConfigGoLog struct {
LogPath string // 日志文件路径
LogName string // 日志文件名
LogLevel string // 日志级别 debug/info/warn/errordebug输出debug/info/warn/error日志。 info输出info/warn/error日志。 warn输出warn/error日志。 error输出error日志。
MaxSize int // 单个文件大小,MB
MaxBackups int // 保存的文件个数
MaxAge int // 保存的天数 0=不删除
Compress bool // 压缩
JsonFormat bool // 是否输出为json格式
ShowLine bool // 显示代码行
LogInConsole bool // 是否同时输出到控制台
}
type GoLog struct {
ConfigGoLog
Logger *zap.Logger
}
func NewGoLog(config *ConfigGoLog) *GoLog {
g := &GoLog{}
g.LogPath = config.LogPath
g.LogName = config.LogName
g.LogLevel = config.LogLevel
g.MaxSize = config.MaxSize
g.MaxBackups = config.MaxBackups
g.MaxAge = config.MaxAge
g.Compress = config.Compress
g.JsonFormat = config.JsonFormat
g.ShowLine = config.ShowLine
g.LogInConsole = config.LogInConsole
// 设置日志级别
var level zapcore.Level
switch g.LogLevel {
case "debug":
level = zap.DebugLevel
case "info":
level = zap.InfoLevel
case "warn":
level = zap.WarnLevel
case "error":
level = zap.ErrorLevel
default:
level = zap.InfoLevel
}
var (
syncer zapcore.WriteSyncer
// 自定义时间输出格式
customTimeEncoder = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05.000"))
}
// 自定义日志级别显示
customLevelEncoder = func(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(level.CapitalString())
}
)
// 定义日志切割配置
hook := lumberjack.Logger{
Filename: g.LogPath + g.LogName, // 日志文件的位置
MaxSize: g.MaxSize, // 在进行切割之前日志文件的最大大小以MB为单位
MaxBackups: g.MaxBackups, // 保留旧文件的最大个数
Compress: g.Compress, // 是否压缩 disabled by default
}
if g.MaxAge > 0 {
hook.MaxAge = g.MaxAge // days
}
// 判断是否控制台输出日志
if g.LogInConsole {
syncer = zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(&hook))
} else {
syncer = zapcore.AddSync(&hook)
}
// 定义zap配置信息
encoderConfig := zapcore.EncoderConfig{
TimeKey: "time",
LevelKey: "level",
NameKey: "logger",
CallerKey: "line",
MessageKey: "msg",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeTime: customTimeEncoder, // 自定义时间格式
EncodeLevel: customLevelEncoder, // 小写编码器
EncodeCaller: zapcore.ShortCallerEncoder, // 全路径编码器
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeName: zapcore.FullNameEncoder,
}
var encoder zapcore.Encoder
// 判断是否json格式输出
if g.JsonFormat {
encoder = zapcore.NewJSONEncoder(encoderConfig)
} else {
encoder = zapcore.NewConsoleEncoder(encoderConfig)
}
core := zapcore.NewCore(
encoder,
syncer,
level,
)
g.Logger = zap.New(core)
// 判断是否显示代码行号
if g.ShowLine {
g.Logger = g.Logger.WithOptions(zap.AddCaller())
}
return g
}

@ -1,31 +0,0 @@
package golog
import (
"log"
"testing"
)
func TestLog(t *testing.T) {
g := NewGoLog(&ConfigGoLog{
LogPath: "./",
LogName: "all.log",
LogLevel: "debug",
MaxSize: 2,
MaxBackups: 30,
MaxAge: 0,
Compress: false,
JsonFormat: false,
ShowLine: true,
LogInConsole: true,
})
log.Println(g.Logger)
g.Logger.Debug("debug 日志")
g.Logger.Sugar().Debug("debug 日志")
g.Logger.Info("info 日志")
g.Logger.Sugar().Info("info 日志")
g.Logger.Warn("warning 日志")
g.Logger.Sugar().Warn("warning 日志")
g.Logger.Error("error 日志")
g.Logger.Sugar().Error("error 日志")
log.Println(g.Logger)
}

@ -1,8 +0,0 @@
package golog
import "encoding/json"
func JsonEncodeNoError(data interface{}) string {
jsons, _ := json.Marshal(data)
return string(jsons)
}

@ -1,53 +0,0 @@
package golog
import (
"go.dtapp.net/dorm"
"go.dtapp.net/goip"
"gorm.io/gorm"
)
const (
logTypeGorm = "gorm"
logTypeMongo = "mongo"
)
// OperationAttr 操作属性
type OperationAttr struct {
gormClient *gorm.DB // 数据库驱动
mongoClient *dorm.MongoClient // 数据库驱动
ipService *goip.Client // ip服务
logType string // 类型
tableName string // 表名
databaseName string // 库名
collectionName string // 表名
}
// WithGormClient 设置数据库驱动
func WithGormClient(client *gorm.DB) *OperationAttr {
return &OperationAttr{gormClient: client, logType: logTypeGorm}
}
// WithMongoClient 设置数据库驱动
func WithMongoClient(client *dorm.MongoClient) *OperationAttr {
return &OperationAttr{mongoClient: client, logType: logTypeMongo}
}
// WithTableName 设置表名
func WithTableName(tableName string) *OperationAttr {
return &OperationAttr{tableName: tableName}
}
// WithDatabaseName 设置库名
func WithDatabaseName(databaseName string) *OperationAttr {
return &OperationAttr{databaseName: databaseName}
}
// WithCollectionName 设置表名
func WithCollectionName(collectionName string) *OperationAttr {
return &OperationAttr{collectionName: collectionName}
}
// WithIpService 设置ip服务
func WithIpService(ipService *goip.Client) *OperationAttr {
return &OperationAttr{ipService: ipService}
}

@ -1 +0,0 @@
package golog

@ -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
}

@ -1,33 +0,0 @@
package golog
import (
"os"
"runtime"
)
type System struct {
Variable []string // 环境变量
Hostname string // 主机名
Twd string // 当前目录
Uid int // 用户ID
EUid int // 有效用户ID
Gid int // 组ID
EGid int // 有效组ID
Pid int // 进程ID
PPid int // 父进程ID
Version string // 版本
}
func (s *System) Init() *System {
s.Variable = os.Environ()
s.Hostname, _ = os.Hostname()
s.Twd, _ = os.Getwd()
s.Uid = os.Getuid()
s.EUid = os.Geteuid()
s.Gid = os.Getgid()
s.EGid = os.Getegid()
s.Pid = os.Getpid()
s.PPid = os.Getppid()
s.Version = runtime.Version()
return s
}

@ -1,15 +0,0 @@
package golog
import (
"runtime"
"strings"
"testing"
)
func TestSystem(t *testing.T) {
var s System
s.Init()
t.Logf("%+v", s)
t.Logf("%+v", runtime.Version())
t.Logf("%+v", strings.TrimPrefix(runtime.Version(), "go"))
}

@ -1,71 +0,0 @@
package golog
import (
"database/sql/driver"
"errors"
"fmt"
"go.dtapp.net/gotime"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"time"
)
type TimeString struct {
Time time.Time
}
// Value 插入数据,把时间转字符串
func (t TimeString) Value() (driver.Value, error) {
return gotime.SetCurrent(t.Time).Format(), nil
}
// Scan 查询数据,把字符串转时间
func (t *TimeString) Scan(value interface{}) error {
// 如果是空值,直接返回
data, ok := value.(string)
if !ok {
return errors.New(fmt.Sprint("无法解析:", value))
}
// 解析时间
result := gotime.SetCurrentParse(data)
*t = TimeString{
Time: result.Time,
}
return nil
}
// MarshalJSON JSON序列化
func (t TimeString) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, gotime.SetCurrent(t.Time).Format())), nil
}
// UnmarshalJSON JSON反序列化
func (t *TimeString) UnmarshalJSON(data []byte) (err error) {
// 删除双引号
if data[0] == '"' && data[len(data)-1] == '"' {
data = data[1 : len(data)-1]
}
result := gotime.SetCurrentParse(string(data))
*t = TimeString{
Time: result.Time,
}
return
}
func (t TimeString) GormDBDataType(db *gorm.DB, field *schema.Field) string {
// 使用 field.Tag、field.TagSettings 获取字段的 tag
// 查看 https://github.com/go-gorm/gorm/blob/master/schema/field.go 获取全部的选项
// 根据不同的数据库驱动返回不同的数据类型
switch db.Dialector.Name() {
case "mysql", "sqlite":
return "varchar"
case "postgres":
return "text"
}
return ""
}

@ -1,3 +1,5 @@
package golog
const Version = "1.0.17"
const (
Version = "1.0.109"
)

@ -1,7 +0,0 @@
package golog
import "testing"
func TestVersion(t *testing.T) {
t.Log(Version)
}
Loading…
Cancel
Save