Skip to content

Commit

Permalink
Merge pull request #271 from fangyincheng/develop
Browse files Browse the repository at this point in the history
Add: add SetLoggerLevel
  • Loading branch information
fangyincheng authored Dec 6, 2019
2 parents 5e815ea + 88e3a7d commit e6d7c04
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
27 changes: 26 additions & 1 deletion common/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ var (
logger Logger
)

type DubboLogger struct {
Logger
dynamicLevel zap.AtomicLevel
}

type Logger interface {
Info(args ...interface{})
Warn(args ...interface{})
Expand Down Expand Up @@ -109,7 +114,8 @@ func InitLogger(conf *zap.Config) {
zapLoggerConfig = *conf
}
zapLogger, _ := zapLoggerConfig.Build(zap.AddCallerSkip(1))
logger = zapLogger.Sugar()
//logger = zapLogger.Sugar()
logger = &DubboLogger{Logger: zapLogger.Sugar(), dynamicLevel: zapLoggerConfig.Level}

// set getty log
getty.SetLogger(logger)
Expand All @@ -123,3 +129,22 @@ func SetLogger(log Logger) {
func GetLogger() Logger {
return logger
}

func SetLoggerLevel(level string) bool {
if l, ok := logger.(OpsLogger); ok {
l.SetLoggerLevel(level)
return true
}
return false
}

type OpsLogger interface {
Logger
SetLoggerLevel(level string)
}

func (dl *DubboLogger) SetLoggerLevel(level string) {
l := new(zapcore.Level)
l.Set(level)
dl.dynamicLevel.SetLevel(*l)
}
16 changes: 16 additions & 0 deletions common/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,19 @@ func TestInitLog(t *testing.T) {
Warnf("%s", "warn")
Errorf("%s", "error")
}

func TestSetLevel(t *testing.T) {
err := InitLog("./log.yml")
assert.NoError(t, err)
Debug("debug")
Info("info")

assert.True(t, SetLoggerLevel("info"))
Debug("debug")
Info("info")

SetLogger(GetLogger().(*DubboLogger).Logger)
assert.False(t, SetLoggerLevel("debug"))
Debug("debug")
Info("info")
}

0 comments on commit e6d7c04

Please sign in to comment.