update level #7

Merged
andrey merged 2 commits from level into master 11 months ago
  1. 60
      level/level.go
  2. 4
      level/level_string.go
  3. 59
      level/level_test.go

60
level/level.go

@ -1,16 +1,20 @@
package level package level
import ( import (
"encoding"
"encoding/json" "encoding/json"
"fmt"
"strings" "strings"
) )
//go:generate stringer -type=Level -linecomment //go:generate stringer -type=Level -linecomment
var ( var (
_ json.Marshaler = Level(0) _ json.Marshaler = Level(0)
_ json.Unmarshaler = (*Level)(nil) _ json.Unmarshaler = (*Level)(nil)
_ encoding.TextMarshaler = Level(0)
_ encoding.TextUnmarshaler = (*Level)(nil)
_ encoding.BinaryMarshaler = Level(0)
_ encoding.BinaryUnmarshaler = (*Level)(nil)
) )
// Level log. // Level log.
@ -18,9 +22,9 @@ type Level uint32
// available log levels. // available log levels.
const ( const (
Emergency Level = iota // emergency Emergency Level = iota // emerg
Alert // alert Alert // alert
Critical // critical Critical // crit
Error // error Error // error
Warning // warning Warning // warning
Notice // notice Notice // notice
@ -28,15 +32,6 @@ const (
Debug // debug Debug // debug
) )
func (l Level) MarshalJSON() ([]byte, error) {
b, err := json.Marshal(l.String())
if err != nil {
return nil, fmt.Errorf("marshal err: %w", err)
}
return b, nil
}
func (l Level) Is(level Level) bool { func (l Level) Is(level Level) bool {
return level == l return level == l
} }
@ -45,13 +40,34 @@ func (l Level) Enabled(level Level) bool {
return l <= level return l <= level
} }
func (l Level) MarshalJSON() ([]byte, error) {
return []byte("\"" + l.String() + "\""), nil
}
func (l *Level) UnmarshalJSON(in []byte) error { func (l *Level) UnmarshalJSON(in []byte) error {
var v string lvl := Parse(string(in[1 : len(in)-1]))
if err := json.Unmarshal(in, &v); err != nil { *l = lvl
return fmt.Errorf("unmarshal err: %w", err)
} return nil
}
func (l Level) MarshalText() ([]byte, error) {
return []byte(l.String()), nil
}
func (l *Level) UnmarshalText(in []byte) error {
lvl := Parse(string(in))
*l = lvl
return nil
}
func (l Level) MarshalBinary() ([]byte, error) {
return []byte(l.String()), nil
}
lvl := Parse(v) func (l *Level) UnmarshalBinary(in []byte) error {
lvl := Parse(string(in))
*l = lvl *l = lvl
return nil return nil
@ -65,11 +81,11 @@ func Parse(lvl string) Level {
return Info return Info
case "notice", "Notice", "NOTICE": case "notice", "Notice", "NOTICE":
return Notice return Notice
case "warning", "Warning", "WARNING": case "warning", "Warning", "WARNING", "warm", "Warm", "WARN":
return Warning return Warning
case "error", "Error", "ERROR": case "error", "Error", "ERROR", "err", "Err", "ERR":
return Error return Error
case "critical", "Critical", "CRITICAL": case "critical", "Critical", "CRITICAL", "crit", "Crit", "CRIT":
return Critical return Critical
case "alert", "Alert", "ALERT": case "alert", "Alert", "ALERT":
return Alert return Alert

4
level/level_string.go

@ -18,9 +18,9 @@ func _() {
_ = x[Debug-7] _ = x[Debug-7]
} }
const _Level_name = "emergencyalertcriticalerrorwarningnoticeinfodebug" const _Level_name = "emergalertcriterrorwarningnoticeinfodebug"
var _Level_index = [...]uint8{0, 9, 14, 22, 27, 34, 40, 44, 49} var _Level_index = [...]uint8{0, 5, 10, 14, 19, 26, 32, 36, 41}
func (i Level) String() string { func (i Level) String() string {
if i >= Level(len(_Level_index)-1) { if i >= Level(len(_Level_index)-1) {

59
level/level_test.go

@ -0,0 +1,59 @@
package level_test
import (
"testing"
"gitoa.ru/go-4devs/log/level"
)
func TestMarshalJSON(t *testing.T) {
levels := map[level.Level]string{
level.Emergency: `"emerg"`,
level.Alert: `"alert"`,
level.Critical: `"crit"`,
level.Error: `"error"`,
level.Warning: `"warning"`,
level.Notice: `"notice"`,
level.Info: `"info"`,
level.Debug: `"debug"`,
}
for level, expect := range levels {
actual, err := level.MarshalJSON()
if err != nil {
t.Errorf("%s got err: %s", level, err)
continue
}
if string(actual) != expect {
t.Errorf("%s got: %s expect: %s", level, actual, expect)
}
}
}
func TestUnmarshalJSON(t *testing.T) {
levels := map[level.Level][]string{
level.Emergency: {`"emerg"`, `"Emerg"`},
level.Alert: {`"alert"`, `"ALERT"`},
level.Critical: {`"crit"`, `"critical"`},
level.Error: {`"error"`, `"ERR"`},
level.Warning: {`"warning"`, `"Warning"`},
level.Notice: {`"notice"`},
level.Info: {`"info"`},
level.Debug: {`"debug"`, `"DEBUG"`},
}
for expect, actuals := range levels {
for _, actual := range actuals {
var level level.Level
if err := level.UnmarshalJSON([]byte(actual)); err != nil {
t.Errorf("%s got err: %s", level, err)
continue
}
if !level.Is(expect) {
t.Errorf("%s got: %s expect: %s", actual, level, expect)
}
}
}
}
Loading…
Cancel
Save