diff --git a/cmd/client/main.go b/cmd/client/main.go index 1eb1cf2..3231280 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -3,7 +3,6 @@ package main import ( "fmt" "io" - "log" "net" "os" @@ -63,9 +62,7 @@ func Runner(f func(net.Conn, *config.Instance, []string) error) func(*cobra.Comm logLevel = instanceCnf.LogLevel } - if err := ylogger.UpdateZeroLogLevel(logLevel); err != nil { - log.Printf("failed to update log level: %s\n", err) - } + ylogger.ReloadLogger(instanceCnf.LogPath, logLevel) defer func() { _ = con.Close() diff --git a/cmd/yproxy/main.go b/cmd/yproxy/main.go index 37fbd30..ea64dbd 100644 --- a/cmd/yproxy/main.go +++ b/cmd/yproxy/main.go @@ -1,8 +1,6 @@ package main import ( - "log" - "github.com/spf13/cobra" "github.com/yezzey-gp/yproxy/config" @@ -30,14 +28,12 @@ var rootCmd = &cobra.Command{ instance := core.NewInstance() - if instanceCnf.LogPath != "" { - ylogger.ReloadLogger(instanceCnf.LogPath) - } if logLevel == "" { logLevel = instanceCnf.LogLevel } - if err := ylogger.UpdateZeroLogLevel(logLevel); err != nil { - log.Printf("failed to update log level: %s\n", err) + + if instanceCnf.LogPath != "" { + ylogger.ReloadLogger(instanceCnf.LogPath, logLevel) } return instance.Run(instanceCnf) diff --git a/config/instance.go b/config/instance.go index 702ac69..146d343 100644 --- a/config/instance.go +++ b/config/instance.go @@ -121,7 +121,22 @@ func EmbedDefaults(cfgInstance *Instance) { cfgInstance.YezzeyRestoreParanoid = false } +var ( + bootstrapCfgPath = "" +) + +func ReloadInstanceConfig() (*Instance, error) { + if err := LoadInstanceConfig(bootstrapCfgPath); err != nil { + return nil, err + } + return InstanceConfig(), nil +} + func LoadInstanceConfig(cfgPath string) (err error) { + if bootstrapCfgPath != "" && bootstrapCfgPath != cfgPath { + return fmt.Errorf("bootstrap config path already set") + } + bootstrapCfgPath = cfgPath cfgInstance, err = ReadInstanceConfig(cfgPath) if err != nil { return diff --git a/pkg/core/core.go b/pkg/core/core.go index a4a7a38..d8afb01 100644 --- a/pkg/core/core.go +++ b/pkg/core/core.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "log" "net" "os" "os/signal" @@ -80,7 +81,15 @@ func (i *Instance) Run(instanceCnf *config.Instance) error { switch s { case syscall.SIGUSR1: - ylogger.ReloadLogger(instanceCnf.LogPath) + instanceCnf, err := config.ReloadInstanceConfig() + + if err != nil { + log.Printf("failed to update log level: %s\n", err) + continue + } + + ylogger.ReloadLogger(instanceCnf.LogPath, instanceCnf.LogLevel) + case syscall.SIGHUP: if dws != nil { err := dws.ServeFor(time.Duration(instanceCnf.DebugMinutes) * time.Minute) diff --git a/pkg/ylogger/logger.go b/pkg/ylogger/logger.go index 38a1be3..00eae33 100644 --- a/pkg/ylogger/logger.go +++ b/pkg/ylogger/logger.go @@ -33,18 +33,9 @@ func NewZeroLogger(filepath string) *zerolog.Logger { return &logger } -func UpdateZeroLogLevel(logLevel string) error { +func ReloadLogger(filepath string, logLevel string) { level := parseLevel(logLevel) - zeroLogger := Zero.With().Logger().Level(level) - Zero = &zeroLogger - return nil -} - -func ReloadLogger(filepath string) { - if filepath == "" { // - return // this means os.Stdout, so no need to open new file - } - newLogger := NewZeroLogger(filepath).Level(Zero.GetLevel()) + newLogger := NewZeroLogger(filepath).Level(level) Zero = &newLogger }