support SetLevelByName

This commit is contained in:
siddontang 2015-12-27 12:49:29 +08:00
parent b151716326
commit 354e14e6c0
2 changed files with 30 additions and 0 deletions

View File

@ -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)
}

View File

@ -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)
}