From 8d2ee23a5ca10aec5bd28f7e2084fed4b3277d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 17 May 2022 11:08:38 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + api.go | 16 +++------------- app.go | 5 +++-- client.go | 9 +++++---- gin.go | 16 +++------------- go.mod | 2 +- go.sum | 2 ++ system.go | 30 ++++++++++++++++++++++++++++++ system_test.go | 11 +++++++++++ version.go | 2 +- 10 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 system.go create mode 100644 system_test.go diff --git a/.gitignore b/.gitignore index 114804c..5d31b6e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ *.log gomod.sh /vendor/ +time_test.go \ No newline at end of file diff --git a/api.go b/api.go index b32de42..2c98ff5 100644 --- a/api.go +++ b/api.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() - }() -} diff --git a/app.go b/app.go index a69f7ba..5e0b5e1 100644 --- a/app.go +++ b/app.go @@ -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() } diff --git a/client.go b/client.go index 497a7af..da7e1c6 100644 --- a/client.go +++ b/client.go @@ -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 } diff --git a/gin.go b/gin.go index b23d215..6baa8b2 100644 --- a/gin.go +++ b/gin.go @@ -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() - }() -} diff --git a/go.mod b/go.mod index 47bbb67..59d5376 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index e8b7992..0100976 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/system.go b/system.go new file mode 100644 index 0000000..bfd5751 --- /dev/null +++ b/system.go @@ -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 +} diff --git a/system_test.go b/system_test.go new file mode 100644 index 0000000..3a748a6 --- /dev/null +++ b/system_test.go @@ -0,0 +1,11 @@ +package golog + +import ( + "testing" +) + +func TestSystem(t *testing.T) { + var s System + s.Init() + t.Logf("%+v", s) +} diff --git a/version.go b/version.go index 32cb2f6..edf29cd 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package golog -const Version = "1.0.6" +const Version = "1.0.7"