Add a function to retrieve a log level by name (useful for config)
This commit is contained in:
parent
606fd37e7d
commit
ac19778676
2 changed files with 25 additions and 0 deletions
10
logging.go
10
logging.go
|
@ -1,6 +1,7 @@
|
||||||
package logging
|
package logging
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -25,6 +26,15 @@ func (l Level) Name() string {
|
||||||
return levelNames[l]
|
return levelNames[l]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LevelByName(l string) (Level, error) {
|
||||||
|
for pos, name := range levelNames {
|
||||||
|
if name == l {
|
||||||
|
return Level(pos), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return DEBUG, errors.New(fmt.Sprintf("Invalid log level %s", l))
|
||||||
|
}
|
||||||
|
|
||||||
type Formatter func(*Record) string
|
type Formatter func(*Record) string
|
||||||
|
|
||||||
func GetLogger() (l *Logger) {
|
func GetLogger() (l *Logger) {
|
||||||
|
|
15
logging_test.go
Normal file
15
logging_test.go
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package logging
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_LevelByName(t *testing.T) {
|
||||||
|
for _, levelName := range levelNames {
|
||||||
|
l, e := LevelByName(levelName)
|
||||||
|
if e != nil {
|
||||||
|
t.Errorf("level %s not recognized", levelName)
|
||||||
|
}
|
||||||
|
if l.Name() != levelName {
|
||||||
|
t.Errorf("expected '%s', got '%s'", levelName, l.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue