- 优化
continuous-integration/drone/push Build is passing Details

master v1.0.7
李光春 2 years ago
parent 18e98b3320
commit 8d2ee23a5c

1
.gitignore vendored

@ -6,3 +6,4 @@
*.log
gomod.sh
/vendor/
time_test.go

@ -1,7 +1,6 @@
package golog
import (
"go.dtapp.net/goip"
"gorm.io/datatypes"
"gorm.io/gorm"
)
@ -10,8 +9,8 @@ import (
type api struct {
db *gorm.DB // pgsql数据库
tableName string // 日志表名
outsideIp string // 外网ip
insideIp string // 内网ip
hostname string // 主机名
}
// ApiPostgresqlLog 结构体
@ -29,7 +28,7 @@ type ApiPostgresqlLog struct {
ResponseBody datatypes.JSON `gorm:"type:jsonb" json:"response_body"` //【返回】内容
ResponseContentLength int64 `gorm:"type:bigint" json:"response_content_length"` //【返回】大小
ResponseTime TimeString `gorm:"index" json:"response_time"` //【返回】时间
SystemOutsideIp string `gorm:"type:text" json:"system_outside_ip"` //【系统】外网ip
SystemHostName string `gorm:"type:text" json:"system_host_name"` //【系统】主机名
SystemInsideIp string `gorm:"type:text" json:"system_inside_ip"` //【系统】内网ip
}
@ -43,9 +42,7 @@ func (a *api) AutoMigrate() {
// Record 记录日志
func (a *api) Record(content ApiPostgresqlLog) int64 {
if content.SystemOutsideIp == "" {
content.SystemOutsideIp = a.outsideIp
}
content.SystemHostName = a.hostname
if content.SystemInsideIp == "" {
content.SystemInsideIp = a.insideIp
}
@ -56,10 +53,3 @@ func (a *api) Record(content ApiPostgresqlLog) int64 {
func (a *api) Query() *gorm.DB {
return a.db.Table(a.tableName)
}
// 配置
func (a *api) configOutsideIp() {
go func() {
a.insideIp = goip.GetOutsideIp()
}()
}

@ -3,6 +3,7 @@ package golog
import (
"go.dtapp.net/goip"
"gorm.io/gorm"
"os"
)
type App struct {
@ -22,9 +23,9 @@ func (a *App) InitClientApi() {
}
a.Api.db = a.Pgsql
a.Api.tableName = a.TableName
a.Api.hostname, _ = os.Hostname()
a.Api.insideIp = goip.GetInsideIp()
a.Api.AutoMigrate()
a.Api.configOutsideIp()
}
// InitClientGin 框架实例化
@ -37,7 +38,7 @@ func (a *App) InitClientGin() {
}
a.Gin.db = a.Pgsql
a.Gin.tableName = a.TableName
a.Gin.hostname, _ = os.Hostname()
a.Gin.insideIp = goip.GetInsideIp()
a.Gin.AutoMigrate()
a.Gin.configOutsideIp()
}

@ -3,6 +3,7 @@ package golog
import (
"go.dtapp.net/goip"
"gorm.io/gorm"
"os"
)
type Client struct {
@ -18,9 +19,9 @@ func NewClientGin(db *gorm.DB, tableName string) *Client {
if tableName == "" {
panic("表名不能为空")
}
client := &Client{Gin: gin{db: db, tableName: tableName, insideIp: goip.GetInsideIp()}}
hostname, _ := os.Hostname()
client := &Client{Gin: gin{db: db, tableName: tableName, hostname: hostname, insideIp: goip.GetInsideIp()}}
client.Gin.AutoMigrate()
client.Gin.configOutsideIp()
return client
}
@ -32,8 +33,8 @@ func NewClientApi(db *gorm.DB, tableName string) *Client {
if tableName == "" {
panic("表名不能为空")
}
client := &Client{Api: api{db: db, tableName: tableName, insideIp: goip.GetInsideIp()}}
hostname, _ := os.Hostname()
client := &Client{Api: api{db: db, tableName: tableName, hostname: hostname, insideIp: goip.GetInsideIp()}}
client.Api.AutoMigrate()
client.Api.configOutsideIp()
return client
}

@ -1,7 +1,6 @@
package golog
import (
"go.dtapp.net/goip"
"gorm.io/datatypes"
"gorm.io/gorm"
)
@ -10,8 +9,8 @@ import (
type gin struct {
db *gorm.DB // pgsql数据库
tableName string // 日志表名
outsideIp string // 外网ip
insideIp string // 内网ip
hostname string // 主机名
}
// GinPostgresqlLog 结构体
@ -40,7 +39,7 @@ type GinPostgresqlLog struct {
ResponseMsg string `gorm:"type:text" json:"response_msg"` //【返回】描述
ResponseData datatypes.JSON `gorm:"type:jsonb" json:"response_data"` //【返回】数据
CostTime int64 `gorm:"type:bigint" json:"cost_time"` //【系统】花费时间
SystemOutsideIp string `gorm:"type:text" json:"system_outside_ip"` //【系统】外网ip
SystemHostName string `gorm:"type:text" json:"system_host_name"` //【系统】主机名
SystemInsideIp string `gorm:"type:text" json:"system_inside_ip"` //【系统】内网ip
}
@ -54,9 +53,7 @@ func (g *gin) AutoMigrate() {
// Record 记录日志
func (g *gin) Record(content GinPostgresqlLog) int64 {
if content.SystemOutsideIp == "" {
content.SystemOutsideIp = g.outsideIp
}
content.SystemHostName = g.hostname
if content.SystemInsideIp == "" {
content.SystemInsideIp = g.insideIp
}
@ -67,10 +64,3 @@ func (g *gin) Record(content GinPostgresqlLog) int64 {
func (g *gin) Query() *gorm.DB {
return g.db.Table(g.tableName)
}
// 配置
func (g *gin) configOutsideIp() {
go func() {
g.insideIp = goip.GetOutsideIp()
}()
}

@ -27,7 +27,7 @@ require (
github.com/ulikunitz/xz v0.5.10 // indirect
go.dtapp.net/gorequest v1.0.18 // indirect
go.dtapp.net/gostring v1.0.3 // indirect
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 // indirect
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 // indirect
golang.org/x/text v0.3.7 // indirect
gorm.io/driver/mysql v1.3.3 // indirect
)

@ -178,6 +178,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 h1:NUzdAbFtCJSXU20AOXgeqaUwg8Ypg4MPYmL+d+rsB5c=
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 h1:O8uGbHCqlTp2P6QJSLmCojM4mN6UemYv8K+dCnmHmu0=
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=

@ -0,0 +1,30 @@
package golog
import (
"os"
)
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
}
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()
return s
}

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

@ -1,3 +1,3 @@
package golog
const Version = "1.0.6"
const Version = "1.0.7"

Loading…
Cancel
Save