/
level.go
41 lines (34 loc) · 1.06 KB
/
level.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package logger
import (
"fmt"
"strings"
"go.uber.org/zap/zapcore"
)
// LevelsMap is a map to Logrus log levels
var LevelsMap = map[string]zapcore.Level{
"EMERGENCY": zapcore.PanicLevel,
"ALERT": zapcore.FatalLevel,
"CRITICAL": zapcore.FatalLevel,
"ERROR": zapcore.ErrorLevel,
"WARNING": zapcore.WarnLevel,
"NOTICE": zapcore.InfoLevel,
"INFO": zapcore.InfoLevel,
"DEBUG": zapcore.DebugLevel,
}
// Level implements the UnmarshalText Interface to be able to be load into the config.
// It is mainly used for parsing configuration with github.com/kelseyhightower/envconfig.
type Level zapcore.Level
// UnmarshalText unmarshal a text to a level.
// Returns an error if no level match.
func (l *Level) UnmarshalText(text []byte) error {
parsedLevel, found := LevelsMap[strings.ToUpper(string(text))]
if !found {
return fmt.Errorf("unable to parse level %s", text)
}
*l = Level(parsedLevel)
return nil
}
// String returns a lower-case ASCII representation of the log level.
func (l Level) String() string {
return zapcore.Level(l).String()
}