Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"io"
"log"
"net"
"os"

Expand Down Expand Up @@ -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()
Expand Down
10 changes: 3 additions & 7 deletions cmd/yproxy/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"log"

"github.com/spf13/cobra"

"github.com/yezzey-gp/yproxy/config"
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions config/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion pkg/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"
"net"
"os"
"os/signal"
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 2 additions & 11 deletions pkg/ylogger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading