TextMarshaler and TextUnmarshaler implementation for Level

This commit is contained in:
Maksim Naumov
2015-09-12 22:41:06 +02:00
parent 418b41d23a
commit 277d0cb562
2 changed files with 58 additions and 0 deletions
+18
View File
@@ -31,6 +31,24 @@ func (level Level) String() string {
return "unknown"
}
// UnmarshalText decodes text to the level.
func (level *Level) UnmarshalText(text []byte) error {
if len(text) == 0 {
return nil
}
parsed, err := ParseLevel(string(text))
if err != nil {
return err
}
*level = parsed
return nil
}
// MarshalText encodes the level into UTF-8-encoded text and returns the result.
func (level Level) MarshalText() (text []byte, err error) {
return []byte(level.String()), nil
}
// ParseLevel takes a string level and returns the Logrus log level constant.
func ParseLevel(lvl string) (Level, error) {
switch lvl {