Skip to content

Commit

Permalink
feat: add option to disable syslog logging (#53)
Browse files Browse the repository at this point in the history
* feat: add option to disable syslog logging

* missing comments

* service flags

* initialize util.Logger
  • Loading branch information
uubulb authored Aug 9, 2024
1 parent 1f77062 commit ac1a34e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
21 changes: 13 additions & 8 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type AgentCliParam struct {
IPReportPeriod uint32 // 上报IP间隔
UseIPv6CountryCode bool // 默认优先展示IPv6旗帜
UseGiteeToUpgrade bool // 强制从Gitee获取更新
DisableSyslog bool // 将日志输出到stderr
}

var (
Expand All @@ -67,11 +68,7 @@ var (
var agentCmd = &cobra.Command{
Use: "agent",
Run: func(cmd *cobra.Command, args []string) {
if runtime.GOOS == "darwin" {
run() // https://github.com/golang/go/issues/59229
} else {
runService("", nil)
}
runService("", nil)
},
PreRun: preRun,
PersistentPreRun: persistPreRun,
Expand Down Expand Up @@ -155,9 +152,15 @@ func init() {
agentCmd.PersistentFlags().BoolVar(&agentConfig.GPU, "gpu", false, "启用GPU监控")
agentCmd.PersistentFlags().BoolVar(&agentConfig.Temperature, "temperature", false, "启用温度监控")
agentCmd.PersistentFlags().BoolVar(&agentCliParam.UseGiteeToUpgrade, "gitee", false, "使用Gitee获取更新")
agentCmd.PersistentFlags().BoolVar(&agentCliParam.DisableSyslog, "disable-syslog", false, "将日志输出到stderr")
agentCmd.PersistentFlags().Uint32VarP(&agentCliParam.IPReportPeriod, "ip-report-period", "u", 30*60, "本地IP更新间隔, 上报频率依旧取决于report-delay的值")
agentCmd.Flags().BoolVarP(&agentCliParam.Version, "version", "v", false, "查看当前版本号")

// https://github.com/golang/go/issues/59229
if runtime.GOOS == "darwin" {
agentCliParam.DisableSyslog = true
}

agentConfig.Read(filepath.Dir(ex) + "/config.yml")

monitor.InitConfig(&agentConfig)
Expand Down Expand Up @@ -324,9 +327,11 @@ func runService(action string, flags []string) {
prg.service = s

errs := make(chan error, 5)
util.Logger, err = s.Logger(errs)
if err != nil {
log.Fatal(err)
if !agentCliParam.DisableSyslog {
util.Logger, err = s.Logger(errs)
if err != nil {
log.Fatal(err)
}
}

go func() {
Expand Down
1 change: 1 addition & 0 deletions cmd/agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func serviceActions(cmd *cobra.Command, args []string) {
{agentCliParam.UseIPv6CountryCode, "--use-ipv6-countrycode", ""},
{agentConfig.GPU, "--gpu", ""},
{agentCliParam.UseGiteeToUpgrade, "--gitee", ""},
{agentCliParam.DisableSyslog, "--disable-syslog", ""},
{agentCliParam.IPReportPeriod != 30*60, "-u", fmt.Sprint(agentCliParam.IPReportPeriod)},
}

Expand Down
12 changes: 3 additions & 9 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package util
import (
"fmt"
"os"
"runtime"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/nezhahq/service"
)

var (
Json = jsoniter.ConfigCompatibleWithStandardLibrary
Logger service.Logger
Json = jsoniter.ConfigCompatibleWithStandardLibrary
Logger service.Logger = service.ConsoleLogger
)

func IsWindows() bool {
Expand All @@ -21,11 +20,6 @@ func IsWindows() bool {

func Println(enabled bool, v ...interface{}) {
if enabled {
if runtime.GOOS != "darwin" {
Logger.Infof("NEZHA@%s>> %v", time.Now().Format("2006-01-02 15:04:05"), fmt.Sprint(v...))
} else {
fmt.Printf("NEZHA@%s>> ", time.Now().Format("2006-01-02 15:04:05"))
fmt.Println(v...)
}
Logger.Infof("NEZHA@%s>> %v", time.Now().Format("2006-01-02 15:04:05"), fmt.Sprint(v...))
}
}

0 comments on commit ac1a34e

Please sign in to comment.