From 354e14e6c093c661abb29fd28403b3c19cff5514 Mon Sep 17 00:00:00 2001 From: siddontang Date: Sun, 27 Dec 2015 12:49:29 +0800 Subject: [PATCH] support SetLevelByName --- log/log.go | 25 +++++++++++++++++++++++++ log/log_test.go | 5 +++++ 2 files changed, 30 insertions(+) diff --git a/log/log.go b/log/log.go index 74cd76a..f2e5ef2 100644 --- a/log/log.go +++ b/log/log.go @@ -5,6 +5,7 @@ import ( "os" "runtime" "strconv" + "strings" "sync" "sync/atomic" "time" @@ -155,6 +156,25 @@ func (l *Logger) SetLevel(level int) { l.level.Set(level) } +// name can be in ["trace", "debug", "info", "warn", "error", "fatal"] +func (l *Logger) SetLevelByName(name string) { + name = strings.ToLower(name) + switch name { + case "trace": + l.SetLevel(LevelTrace) + case "debug": + l.SetLevel(LevelDebug) + case "info": + l.SetLevel(LevelInfo) + case "warn": + l.SetLevel(LevelWarn) + case "error": + l.SetLevel(LevelError) + case "fatal": + l.SetLevel(LevelFatal) + } +} + func (l *Logger) SetHandler(h Handler) { if l.closed.Get() == 1 { return @@ -288,6 +308,11 @@ func SetLevel(level int) { std.SetLevel(level) } +// name can be in ["trace", "debug", "info", "warn", "error", "fatal"] +func SetLevelByName(name string) { + std.SetLevelByName(name) +} + func SetHandler(h Handler) { std.SetHandler(h) } diff --git a/log/log_test.go b/log/log_test.go index 2e29b31..bc5ffa1 100644 --- a/log/log_test.go +++ b/log/log_test.go @@ -23,6 +23,11 @@ func TestStdStreamLog(t *testing.T) { SetLevel(LevelError) Infof("%s %d", "Hello", 123) + + SetLevelByName("info") + + Infof("%s %d", "Hello", 123) + Fatalf("%s %d", "Hello", 123) }