logger_others.go 746 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // +build !windows
  2. package logger
  3. import (
  4. "fmt"
  5. "go.uber.org/zap/zapcore"
  6. "os"
  7. "os/signal"
  8. "syscall"
  9. )
  10. func logLevelSignal() {
  11. c := make(chan os.Signal)
  12. signal.Notify(c, syscall.SIGUSR1, syscall.SIGUSR2)
  13. fmt.Println("notify receive signal")
  14. go func() {
  15. for s := range c {
  16. fmt.Println("receive signal ", s.String())
  17. switch s {
  18. case syscall.SIGUSR1:
  19. cur := atomicLevel.Level()
  20. if (cur - 1) >= zapcore.DebugLevel {
  21. atomicLevel.SetLevel(zapcore.Level(cur - 1))
  22. }
  23. case syscall.SIGUSR2:
  24. cur := atomicLevel.Level()
  25. if (cur + 1) <= zapcore.FatalLevel {
  26. atomicLevel.SetLevel(zapcore.Level(cur + 1))
  27. }
  28. default:
  29. }
  30. fmt.Println("debug level change to ", atomicLevel.String())
  31. }
  32. }()
  33. }