Skip to content

Commit

Permalink
AppId、AppName、AppIp 迁移到core包
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Dec 31, 2023
1 parent 4b14731 commit 606a9e3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
14 changes: 14 additions & 0 deletions core/appInfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core

import (
"github.com/farseer-go/fs/dateTime"
)

var (
StartupAt dateTime.DateTime // 应用启动时间
AppName string // 应用名称
HostName string // 主机名称
AppId int64 // 应用ID
AppIp string // 应用IP
ProcessId int // 进程Id
)
8 changes: 7 additions & 1 deletion flog/fopsProvider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ func (r *fopsLoggerPersistent) IsEnabled(logLevel eumLogLevel.Enum) bool {
}

func (r *fopsLoggerPersistent) Log(LogLevel eumLogLevel.Enum, log *LogData, exception error) {
r.queue <- log
if LogLevel != eumLogLevel.NoneLevel {
log.Content = mustCompile.ReplaceAllString(log.Content, "")
log.AppId = core.AppId
log.AppIp = core.AppIp
log.AppName = core.AppName
r.queue <- log
}
}

// 开启上传
Expand Down
5 changes: 5 additions & 0 deletions flog/logData.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type LogData struct {
Component string // 组件名称
Content string
newLine bool // 是否需要换行
// 上传到FOPS时使用
TraceId int64 // 上下文ID
AppId int64 // 应用ID
AppName string // 应用名称
AppIp string // 应用IP
}

func newLogData(logLevel eumLogLevel.Enum, content string, component string) *LogData {
Expand Down
32 changes: 13 additions & 19 deletions initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ import (
)

var (
StartupAt dateTime.DateTime // 应用启动时间
AppName string // 应用名称
HostName string // 主机名称
AppId int64 // 应用ID
AppIp string // 应用IP
ProcessId int // 进程Id
Context context.Context // 最顶层的上下文
dependModules []modules.FarseerModule // 依赖的模块
callbackFnList []callbackFn // 回调函数列表
Expand All @@ -42,22 +36,22 @@ type callbackFn struct {
func Initialize[TModule modules.FarseerModule](appName string) {
sw := stopwatch.StartNew()
Context = context.Background()
AppName = appName
ProcessId = os.Getppid()
HostName, _ = os.Hostname()
StartupAt = dateTime.Now()
core.AppName = appName
core.ProcessId = os.Getppid()
core.HostName, _ = os.Hostname()
core.StartupAt = dateTime.Now()
rand.Seed(time.Now().UnixNano())

snowflake.Init(parse.HashCode64(HostName), rand.Int63n(32))
AppId = snowflake.GenerateId()
AppIp = net.GetIp()
snowflake.Init(parse.HashCode64(core.HostName), rand.Int63n(32))
core.AppId = snowflake.GenerateId()
core.AppIp = net.GetIp()

flog.LogBuffer <- fmt.Sprint("AppName: ", flog.Colors[2](AppName))
flog.LogBuffer <- fmt.Sprint("AppID: ", flog.Colors[2](AppId))
flog.LogBuffer <- fmt.Sprint("AppIP: ", flog.Colors[2](AppIp))
flog.LogBuffer <- fmt.Sprint("HostName:", flog.Colors[2](HostName))
flog.LogBuffer <- fmt.Sprint("HostTime:", flog.Colors[2](StartupAt.ToString("yyyy-MM-dd hh:mm:ss")))
flog.LogBuffer <- fmt.Sprint("PID: ", flog.Colors[2](ProcessId))
flog.LogBuffer <- fmt.Sprint("AppName: ", flog.Colors[2](core.AppName))
flog.LogBuffer <- fmt.Sprint("AppID: ", flog.Colors[2](core.AppId))
flog.LogBuffer <- fmt.Sprint("AppIP: ", flog.Colors[2](core.AppIp))
flog.LogBuffer <- fmt.Sprint("HostName:", flog.Colors[2](core.HostName))
flog.LogBuffer <- fmt.Sprint("HostTime:", flog.Colors[2](core.StartupAt.ToString("yyyy-MM-dd hh:mm:ss")))
flog.LogBuffer <- fmt.Sprint("PID: ", flog.Colors[2](core.ProcessId))
showComponentLog()
flog.LogBuffer <- fmt.Sprint("---------------------------------------")

Expand Down
11 changes: 6 additions & 5 deletions test/initialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"github.com/farseer-go/fs"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/modules"
"github.com/stretchr/testify/assert"
"os"
Expand Down Expand Up @@ -44,11 +45,11 @@ func TestInitialize(t *testing.T) {
assert.Equal(t, 3, lst[2])
assert.Equal(t, 4, lst[3])

assert.Equal(t, 3, strings.Count(fs.AppIp, "."))
assert.Equal(t, 18, len(strconv.FormatInt(fs.AppId, 10)))
assert.Equal(t, "unit test", fs.AppName)
assert.Equal(t, os.Getppid(), fs.ProcessId)
assert.Equal(t, 3, strings.Count(core.AppIp, "."))
assert.Equal(t, 18, len(strconv.FormatInt(core.AppId, 10)))
assert.Equal(t, "unit test", core.AppName)
assert.Equal(t, os.Getppid(), core.ProcessId)

hostName, _ := os.Hostname()
assert.Equal(t, hostName, fs.HostName)
assert.Equal(t, hostName, core.HostName)
}

0 comments on commit 606a9e3

Please sign in to comment.