Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: add SetLoggerLevel #271

Merged
merged 1 commit into from
Dec 6, 2019
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
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")
}