- update golog

master
李光春 8 months ago
parent 3d3c2109ae
commit 624394f6d4

@ -1,5 +1,5 @@
package go_library
func Version() string {
return "1.0.162"
return "1.0.163"
}

@ -1,385 +0,0 @@
package golog
import (
"context"
"github.com/dtapps/go-library"
"github.com/dtapps/go-library/utils/dorm"
"github.com/dtapps/go-library/utils/goip"
"github.com/dtapps/go-library/utils/gorequest"
"github.com/dtapps/go-library/utils/gostring"
"github.com/dtapps/go-library/utils/gotime"
"github.com/dtapps/go-library/utils/gotrace_id"
"github.com/dtapps/go-library/utils/gourl"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"runtime"
"time"
)
// ApiZapLogFun *ApiClient 驱动
type ApiZapLogFun func() *ApiZapLog
type ApiZapLogConfig struct {
LogPath string // 日志文件路径
LogName string // 日志文件名
MaxSize int // 单位为MB,默认为512MB
MaxBackups int // 保留旧文件的最大个数
MaxAge int // 文件最多保存多少天 0=不删除
LocalTime bool // 采用本地时间
Compress bool // 是否压缩日志
ShowLine bool // 显示代码行
}
type ApiZapLog struct {
config *ApiZapLogConfig
logger *zap.Logger
zapCore zapcore.Core
systemConfig struct {
systemHostname string // 主机名
systemOs string // 系统类型
systemKernel string // 系统内核
systemInsideIp string // 内网ip
systemOutsideIp string // 外网ip
goVersion string // go版本
sdkVersion string // sdk版本
}
}
func NewApiZapLog(ctx context.Context, config *ApiZapLogConfig) *ApiZapLog {
zl := &ApiZapLog{config: config}
var syncer zapcore.WriteSyncer
// 定义日志切割配置
hook := lumberjack.Logger{
Filename: zl.config.LogPath + zl.config.LogName, // ⽇志⽂件路径
MaxSize: zl.config.MaxSize, // 单位为MB,默认为512MB
MaxAge: zl.config.MaxAge, // 文件最多保存多少天
MaxBackups: zl.config.MaxBackups, // 保留旧文件的最大个数
LocalTime: zl.config.LocalTime, // 采用本地时间
Compress: zl.config.Compress, // 是否压缩日志
}
// 在文件输出日志
syncer = zapcore.AddSync(&hook)
// 自定义时间输出格式
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_type",
MessageKey: "msg",
TimeKey: "zap_time",
NameKey: "logger",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeTime: customTimeEncoder, // 自定义时间格式
EncodeLevel: customLevelEncoder, // 小写编码器
EncodeCaller: zapcore.ShortCallerEncoder, // 全路径编码器
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeName: zapcore.FullNameEncoder,
}
// json格式输出
zl.zapCore = zapcore.NewCore(zapcore.NewJSONEncoder(encoderConf),
syncer, zap.NewAtomicLevelAt(zapcore.InfoLevel))
zl.logger = zl.withShowLine(zap.New(zl.zapCore))
zl.setConfig(ctx)
return zl
}
// 判断是否显示代码行号
func (zl *ApiZapLog) withShowLine(logger *zap.Logger) *zap.Logger {
if zl.config.ShowLine {
logger = logger.WithOptions(zap.AddCaller())
}
return logger
}
// Middleware 中间件
func (zl *ApiZapLog) Middleware(ctx context.Context, request gorequest.Response) {
zl.logger.With(
zapcore.Field{
Key: "trace_id",
Type: zapcore.StringType,
String: gotrace_id.GetTraceIdContext(ctx),
}, zapcore.Field{
Key: "request_time",
Type: zapcore.StringType,
String: gotime.SetCurrent(request.RequestTime).Format(),
}, zapcore.Field{
Key: "request_uri",
Type: zapcore.StringType,
String: request.RequestUri,
}, zapcore.Field{
Key: "request_url",
Type: zapcore.StringType,
String: gourl.UriParse(request.RequestUri).Url,
}, zapcore.Field{
Key: "request_api",
Type: zapcore.StringType,
String: gourl.UriParse(request.RequestUri).Path,
}, zapcore.Field{
Key: "request_method",
Type: zapcore.StringType,
String: request.RequestMethod,
}, zapcore.Field{
Key: "request_params",
Type: zapcore.StringType,
String: gostring.ToString(request.RequestParams), //
}, zapcore.Field{
Key: "request_header",
Type: zapcore.StringType,
String: gostring.ToString(request.RequestHeader), //
}, zapcore.Field{
Key: "request_ip",
Type: zapcore.StringType,
String: zl.systemConfig.systemOutsideIp,
}, zapcore.Field{
Key: "response_header",
Type: zapcore.StringType,
String: gostring.ToString(request.ResponseHeader), //
}, zapcore.Field{
Key: "response_status_code",
Type: zapcore.Int64Type,
Interface: request.ResponseStatusCode,
}, zapcore.Field{
Key: "response_body",
Type: zapcore.StringType,
String: gostring.ToString(dorm.JsonDecodeNoError(request.ResponseBody)), //
}, zapcore.Field{
Key: "response_content_length",
Type: zapcore.Int64Type,
Interface: request.ResponseContentLength,
}, zapcore.Field{
Key: "response_time",
Type: zapcore.StringType,
String: gotime.SetCurrent(request.ResponseTime).Format(),
}, zapcore.Field{
Key: "system_host_name",
Type: zapcore.StringType,
String: zl.systemConfig.systemHostname,
}, zapcore.Field{
Key: "system_inside_ip",
Type: zapcore.StringType,
String: zl.systemConfig.systemInsideIp,
}, zapcore.Field{
Key: "system_os",
Type: zapcore.StringType,
String: zl.systemConfig.systemOs,
}, zapcore.Field{
Key: "system_arch",
Type: zapcore.StringType,
String: zl.systemConfig.systemKernel,
}, zapcore.Field{
Key: "go_version",
Type: zapcore.StringType,
String: zl.systemConfig.goVersion,
}, zapcore.Field{
Key: "sdk_version",
Type: zapcore.StringType,
String: zl.systemConfig.sdkVersion,
}).
Info("Middleware")
}
// MiddlewareXml 中间件
func (zl *ApiZapLog) MiddlewareXml(ctx context.Context, request gorequest.Response) {
zl.logger.With(
zapcore.Field{
Key: "trace_id",
Type: zapcore.StringType,
String: gotrace_id.GetTraceIdContext(ctx),
}, zapcore.Field{
Key: "request_time",
Type: zapcore.StringType,
String: gotime.SetCurrent(request.RequestTime).Format(),
}, zapcore.Field{
Key: "request_uri",
Type: zapcore.StringType,
String: request.RequestUri,
}, zapcore.Field{
Key: "request_url",
Type: zapcore.StringType,
String: gourl.UriParse(request.RequestUri).Url,
}, zapcore.Field{
Key: "request_api",
Type: zapcore.StringType,
String: gourl.UriParse(request.RequestUri).Path,
}, zapcore.Field{
Key: "request_method",
Type: zapcore.StringType,
String: request.RequestMethod,
}, zapcore.Field{
Key: "request_params",
Type: zapcore.StringType,
String: gostring.ToString(request.RequestParams), //
}, zapcore.Field{
Key: "request_header",
Type: zapcore.StringType,
String: gostring.ToString(request.RequestHeader), //
}, zapcore.Field{
Key: "request_ip",
Type: zapcore.StringType,
String: zl.systemConfig.systemOutsideIp,
}, zapcore.Field{
Key: "response_header",
Type: zapcore.StringType,
String: gostring.ToString(request.ResponseHeader), //
}, zapcore.Field{
Key: "response_status_code",
Type: zapcore.Int64Type,
Interface: request.ResponseStatusCode,
}, zapcore.Field{
Key: "response_body",
Type: zapcore.StringType,
String: gostring.ToString(dorm.XmlDecodeNoError(request.ResponseBody)), //
}, zapcore.Field{
Key: "response_content_length",
Type: zapcore.Int64Type,
Interface: request.ResponseContentLength,
}, zapcore.Field{
Key: "response_time",
Type: zapcore.StringType,
String: gotime.SetCurrent(request.ResponseTime).Format(),
}, zapcore.Field{
Key: "system_host_name",
Type: zapcore.StringType,
String: zl.systemConfig.systemHostname,
}, zapcore.Field{
Key: "system_inside_ip",
Type: zapcore.StringType,
String: zl.systemConfig.systemInsideIp,
}, zapcore.Field{
Key: "system_os",
Type: zapcore.StringType,
String: zl.systemConfig.systemOs,
}, zapcore.Field{
Key: "system_arch",
Type: zapcore.StringType,
String: zl.systemConfig.systemKernel,
}, zapcore.Field{
Key: "go_version",
Type: zapcore.StringType,
String: zl.systemConfig.goVersion,
}, zapcore.Field{
Key: "sdk_version",
Type: zapcore.StringType,
String: zl.systemConfig.sdkVersion,
}).
Info("MiddlewareXml")
}
// MiddlewareCustom 中间件
func (zl *ApiZapLog) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response) {
zl.logger.With(
zapcore.Field{
Key: "trace_id",
Type: zapcore.StringType,
String: gotrace_id.GetTraceIdContext(ctx),
}, zapcore.Field{
Key: "request_time",
Type: zapcore.StringType,
String: gotime.SetCurrent(request.RequestTime).Format(),
}, zapcore.Field{
Key: "request_uri",
Type: zapcore.StringType,
String: request.RequestUri,
}, zapcore.Field{
Key: "request_url",
Type: zapcore.StringType,
String: gourl.UriParse(request.RequestUri).Url,
}, zapcore.Field{
Key: "request_api",
Type: zapcore.StringType,
String: api,
}, zapcore.Field{
Key: "request_method",
Type: zapcore.StringType,
String: request.RequestMethod,
}, zapcore.Field{
Key: "request_params",
Type: zapcore.StringType,
String: gostring.ToString(request.RequestParams), //
}, zapcore.Field{
Key: "request_header",
Type: zapcore.StringType,
String: gostring.ToString(request.RequestHeader), //
}, zapcore.Field{
Key: "request_ip",
Type: zapcore.StringType,
String: zl.systemConfig.systemOutsideIp,
}, zapcore.Field{
Key: "response_header",
Type: zapcore.StringType,
String: gostring.ToString(request.ResponseHeader), //
}, zapcore.Field{
Key: "response_status_code",
Type: zapcore.Int64Type,
Interface: request.ResponseStatusCode,
}, zapcore.Field{
Key: "response_body",
Type: zapcore.StringType,
String: gostring.ToString(dorm.JsonDecodeNoError(request.ResponseBody)), //
}, zapcore.Field{
Key: "response_content_length",
Type: zapcore.Int64Type,
Interface: request.ResponseContentLength,
}, zapcore.Field{
Key: "response_time",
Type: zapcore.StringType,
String: gotime.SetCurrent(request.ResponseTime).Format(),
}, zapcore.Field{
Key: "system_host_name",
Type: zapcore.StringType,
String: zl.systemConfig.systemHostname,
}, zapcore.Field{
Key: "system_inside_ip",
Type: zapcore.StringType,
String: zl.systemConfig.systemInsideIp,
}, zapcore.Field{
Key: "system_os",
Type: zapcore.StringType,
String: zl.systemConfig.systemOs,
}, zapcore.Field{
Key: "system_arch",
Type: zapcore.StringType,
String: zl.systemConfig.systemKernel,
}, zapcore.Field{
Key: "go_version",
Type: zapcore.StringType,
String: zl.systemConfig.goVersion,
}, zapcore.Field{
Key: "sdk_version",
Type: zapcore.StringType,
String: zl.systemConfig.sdkVersion,
}).
Info("MiddlewareCustom")
}
func (zl *ApiZapLog) setConfig(ctx context.Context) {
info := getSystem()
zl.systemConfig.systemHostname = info.SystemHostname
zl.systemConfig.systemOs = info.SystemOs
zl.systemConfig.systemKernel = info.SystemKernel
zl.systemConfig.systemInsideIp = goip.GetInsideIp(ctx)
zl.systemConfig.sdkVersion = go_library.Version()
zl.systemConfig.goVersion = runtime.Version()
}

@ -2,15 +2,12 @@ package golog
import (
"context"
"errors"
"github.com/dtapps/go-library/utils/dorm"
"github.com/dtapps/go-library/utils/goip"
)
type GinCustomClient struct {
gormClient *dorm.GormClient // 数据库驱动
ipService *goip.Client // ip服务
config struct {
ipService *goip.Client // IP服务
config struct {
systemHostname string // 主机名
systemOs string // 系统类型
systemKernel string // 系统内核
@ -19,21 +16,18 @@ type GinCustomClient struct {
goVersion string // go版本
sdkVersion string // sdk版本
}
gormConfig struct {
stats bool // 状态
tableName string // 表名
slog struct {
status bool // 状态
client *SLog // 日志服务
}
}
type ConfigGinCustomClient struct {
IpService *goip.Client // ip服务
GormClientFun dorm.GormClientTableFun // 日志配置
CurrentIp string // 当前ip
IpService *goip.Client // IP服务
CurrentIp string // 当前IP
}
func NewGinCustomClient(config *ConfigGinCustomClient) (*GinCustomClient, error) {
var ctx = context.Background()
func NewGinCustomClient(ctx context.Context, config *ConfigGinCustomClient) (*GinCustomClient, error) {
c := &GinCustomClient{}
@ -49,30 +43,5 @@ func NewGinCustomClient(config *ConfigGinCustomClient) (*GinCustomClient, error)
// 配置信息
c.setConfig(ctx)
gormClient, gormTableName := config.GormClientFun()
if gormClient == nil || gormClient.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
}
err := c.autoMigrate(ctx)
if err != nil {
return nil, err
}
c.gormConfig.stats = true
}
return c, nil
}

@ -1,71 +1,62 @@
package golog
import (
"context"
"fmt"
"github.com/dtapps/go-library/utils/dorm"
"github.com/dtapps/go-library/utils/goip"
"github.com/dtapps/go-library/utils/gojson"
"github.com/dtapps/go-library/utils/gorequest"
"github.com/dtapps/go-library/utils/gotime"
"github.com/dtapps/go-library/utils/gotrace_id"
"github.com/dtapps/go-library/utils/gourl"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"time"
)
// 模型
type ginPostgresqlLogCustom struct {
LogID int64 `gorm:"primaryKey;comment:【日志】编号" json:"log_id,omitempty"` //【日志】编号
LogTime time.Time `gorm:"comment:【日志】时间" json:"log_time,omitempty"` //【记日志录】时间
TraceId string `gorm:"index;comment:【系统】跟踪编号" json:"trace_id,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
RequestUrlQuery string `gorm:"comment:【请求】请求URL参数" json:"request_url_query,omitempty"` //【请求】请求URL参数
RequestHeader string `gorm:"comment:【请求】请求头" json:"request_header,omitempty"` //【请求】请求头
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"` //【请求】请求客户端纬度
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版本
CustomId string `gorm:"index;comment:【日志】自定义编号" json:"custom_id,omitempty"` //【日志】自定义编号
CustomType string `gorm:"index;comment:【日志】自定义类型" json:"custom_type,omitempty"` //【日志】自定义类型
CustomContent string `gorm:"comment:【日志】自定义内容" json:"custom_content,omitempty"` //【日志】自定义内容
}
// 创建模型
func (c *GinCustomClient) autoMigrate(ctx context.Context) error {
return c.gormClient.GetDb().Table(c.gormConfig.tableName).AutoMigrate(&ginPostgresqlLogCustom{})
type ginSLogCustom struct {
LogTime time.Time `json:"log_time,omitempty"` //【记日志录】时间
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"` //【请求】请求客户端纬度
SystemHostName string `json:"system_host_name,omitempty"` //【系统】主机名
SystemInsideIp string `json:"system_inside_ip,omitempty"` //【系统】内网ip
SystemOs string `json:"system_os,omitempty"` //【系统】系统类型
SystemArch string `json:"system_arch,omitempty"` //【系统】系统架构
GoVersion string `json:"go_version,omitempty"` //【程序】Go版本
SdkVersion string `json:"sdk_version,omitempty"` //【程序】Sdk版本
CustomId string `json:"custom_id,omitempty"` //【日志】自定义编号
CustomType string `json:"custom_type,omitempty"` //【日志】自定义类型
CustomContent string `json:"custom_content,omitempty"` //【日志】自定义内容
}
type GinCustomClientGinRecordOperation struct {
gormClient *dorm.GormClient // 数据库驱动
ipService *goip.Client // ip服务
tableName string // 表名
data *ginPostgresqlLogCustom // 数据
slogClient *SLog // 日志服务
ipService *goip.Client // IP服务
data *ginSLogCustom // 数据
}
// GinRecord 记录日志
func (c *GinCustomClient) GinRecord(ginCtx *gin.Context) *GinCustomClientGinRecordOperation {
operation := &GinCustomClientGinRecordOperation{
gormClient: c.gormClient,
slogClient: c.slog.client,
ipService: c.ipService,
tableName: c.gormConfig.tableName,
}
operation.data = new(ginPostgresqlLogCustom)
operation.data = new(ginSLogCustom)
operation.data.LogTime = gotime.Current().Time //【日志】时间
operation.data.TraceId = gotrace_id.GetGinTraceId(ginCtx) // 【系统】跟踪编号
if ginCtx.Request.TLS == nil {
@ -98,42 +89,10 @@ func (o *GinCustomClientGinRecordOperation) CustomInfo(customId any, customType
return o
}
func (o *GinCustomClientGinRecordOperation) CreateData() error {
err := o.gormClient.GetDb().Table(o.tableName).Create(&o.data).Error
if o.data.LogID != 0 {
go func() {
ginCustomClientRecordUpdateIpInfo(o.ipService, o.gormClient.GetDb(), o.tableName, o.data.LogID, o.data.RequestIp)
}()
}
return err
func (o *GinCustomClientGinRecordOperation) CreateData() {
o.slogClient.WithLogger().Info(gojson.JsonEncodeNoError(o.data))
}
func (o *GinCustomClientGinRecordOperation) CreateDataNoError() {
o.gormClient.GetDb().Table(o.tableName).Create(&o.data)
if o.data.LogID != 0 {
go func() {
ginCustomClientRecordUpdateIpInfo(o.ipService, o.gormClient.GetDb(), o.tableName, o.data.LogID, o.data.RequestIp)
}()
}
}
func ginCustomClientRecordUpdateIpInfo(ipService *goip.Client, pdTx *gorm.DB, tableName string, logId int64, requestIp string) {
ipInfo := ipService.Analyse(requestIp)
pdTx.Table(tableName).Where("log_id = ?", logId).
Select(
"request_ip_country",
"request_ip_province",
"request_ip_city",
"request_ip_isp",
"request_ip_longitude",
"request_ip_longitude",
).
Updates(ginPostgresqlLogCustom{
RequestIpCountry: ipInfo.Country, //【请求】请求客户端城市
RequestIpProvince: ipInfo.Province, //【请求】请求客户端省份
RequestIpCity: ipInfo.City, //【请求】请求客户端城市
RequestIpIsp: ipInfo.Isp, //【请求】请求客户端运营商
RequestIpLongitude: ipInfo.LocationLatitude, //【请求】请求客户端纬度
RequestIpLatitude: ipInfo.LocationLongitude, //【请求】请求客户端经度
})
o.slogClient.WithLogger().Info(gojson.JsonEncodeNoError(o.data))
}

@ -3,8 +3,6 @@ package golog
import (
"bytes"
"context"
"errors"
"github.com/dtapps/go-library/utils/dorm"
"github.com/dtapps/go-library/utils/goip"
"github.com/dtapps/go-library/utils/gojson"
"github.com/dtapps/go-library/utils/gorequest"
@ -19,10 +17,8 @@ type GinClientFun func() *GinClient
// GinClient 框架
type GinClient struct {
gormClient *dorm.GormClient // 数据库驱动
ipService *goip.Client // ip服务
zapLog *ZapLog // 日志服务
config struct {
ipService *goip.Client // IP服务
config struct {
systemHostname string // 主机名
systemOs string // 系统类型
systemVersion string // 系统版本
@ -37,29 +33,23 @@ type GinClient struct {
goVersion string // go版本
sdkVersion string // sdk版本
}
gormConfig struct {
stats bool // 状态
tableName string // 表名
slog struct {
status bool // 状态
client *SLog // 日志服务
}
}
// GinClientConfig 框架实例配置
type GinClientConfig struct {
IpService *goip.Client // ip服务
GormClientFun dorm.GormClientTableFun // 日志配置
ZapLog *ZapLog // 日志服务
CurrentIp string // 当前ip
IpService *goip.Client // IP服务
CurrentIp string // 当前IP
}
// NewGinClient 创建框架实例化
func NewGinClient(config *GinClientConfig) (*GinClient, error) {
var ctx = context.Background()
func NewGinClient(ctx context.Context, config *GinClientConfig) (*GinClient, error) {
c := &GinClient{}
c.zapLog = config.ZapLog
if config.CurrentIp != "" && config.CurrentIp != "0.0.0.0" {
c.config.systemOutsideIp = config.CurrentIp
}
@ -73,28 +63,6 @@ func NewGinClient(config *GinClientConfig) (*GinClient, error) {
// 配置信息
c.setConfig(ctx)
gormClient, gormTableName := config.GormClientFun()
if gormClient == nil || gormClient.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
}
return c, nil
}
@ -170,13 +138,14 @@ func (c *GinClient) Middleware() gin.HandlerFunc {
var traceId = gotrace_id.GetGinTraceId(ginCtx)
// 记录
if c.gormConfig.stats {
if c.slog.status {
if dataJson {
c.gormRecordJson(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, info)
} else {
c.gormRecordXml(ginCtx, traceId, requestTime, data, responseCode, responseBody, startTime, endTime, info)
}
}
}()
}
}

@ -7,6 +7,15 @@ import (
"runtime"
)
// ConfigSLogClientFun 日志配置
func (c *GinClient) ConfigSLogClientFun(sLogFun SLogFun) {
sLog := sLogFun()
if sLog != nil {
c.slog.client = sLog
c.slog.status = true
}
}
func (c *GinClient) setConfig(ctx context.Context) {
info := getSystem()

@ -1,9 +1,9 @@
package golog
import (
"context"
"github.com/dtapps/go-library/utils/dorm"
"github.com/dtapps/go-library/utils/goip"
"github.com/dtapps/go-library/utils/gojson"
"github.com/dtapps/go-library/utils/gotime"
"github.com/dtapps/go-library/utils/gourl"
"github.com/gin-gonic/gin"
@ -11,50 +11,41 @@ import (
)
// 模型
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)
}
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 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 string `json:"request_header,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"` //【系统】花费时间
SystemHostName string `json:"system_host_name,omitempty"` //【系统】主机名
SystemInsideIp string `json:"system_inside_ip,omitempty"` //【系统】内网ip
SystemOs string `json:"system_os,omitempty"` //【系统】系统类型
SystemArch string `json:"system_arch,omitempty"` //【系统】系统架构
GoVersion string `json:"go_version,omitempty"` //【程序】Go版本
SdkVersion string `json:"sdk_version,omitempty"` //【程序】Sdk版本
}
// gormRecord 记录日志
func (c *GinClient) gormRecord(data ginPostgresqlLog) {
func (c *GinClient) gormRecord(data ginSLog) {
data.SystemHostName = c.config.systemHostname //【系统】主机名
data.SystemInsideIp = c.config.systemInsideIp //【系统】内网ip
@ -63,16 +54,12 @@ func (c *GinClient) gormRecord(data ginPostgresqlLog) {
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)
}
c.slog.client.WithLogger().Info(gojson.JsonEncodeNoError(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{
data := ginSLog{
TraceId: traceId, //【系统】跟踪编号
RequestTime: requestTime, //【请求】时间
RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接
@ -110,7 +97,7 @@ func (c *GinClient) gormRecordJson(ginCtx *gin.Context, traceId string, requestT
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{
data := ginSLog{
TraceId: traceId, //【系统】跟踪编号
RequestTime: requestTime, //【请求】时间
RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接
@ -145,17 +132,3 @@ func (c *GinClient) gormRecordXml(ginCtx *gin.Context, traceId string, requestTi
c.gormRecord(data)
}
// GormDelete 删除
func (c *GinClient) GormDelete(ctx context.Context, hour int64) error {
return c.GormCustomTableDelete(ctx, c.gormConfig.tableName, hour)
}
// GormCustomTableDelete 删除数据 - 自定义表名
func (c *GinClient) GormCustomTableDelete(ctx context.Context, tableName string, hour int64) error {
err := c.gormClient.GetDb().Table(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
}

@ -11,6 +11,8 @@ import (
"os"
)
type SLogFun func() *SLog
type SLogConfig struct {
LogPath string // 日志文件路径
LogName string // 日志文件名
@ -32,7 +34,7 @@ type SLog struct {
logger *slog.Logger
}
func NewSlog(config *SLogConfig) *SLog {
func NewSlog(ctx context.Context, config *SLogConfig) *SLog {
sl := &SLog{config: config}

@ -1,137 +0,0 @@
package golog
import (
"context"
"github.com/dtapps/go-library/utils/gotime"
"github.com/dtapps/go-library/utils/gotrace_id"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"time"
)
// ZapLogFun *ApiClient 驱动
type ZapLogFun func() *ZapLog
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
MaxAge: zl.config.MaxAge, // 文件最多保存多少天
MaxBackups: zl.config.MaxBackups, // 保留旧文件的最大个数
LocalTime: zl.config.LocalTime, // 采用本地时间
Compress: zl.config.Compress, // 是否压缩日志
}
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
}

@ -1,11 +0,0 @@
package rocron
import (
"github.com/robfig/cron/v3"
)
// https://github.com/robfig/cron/
var (
Cron *cron.Cron
)
Loading…
Cancel
Save