Browse Source

Implement TextUnmarshaller interface for Level type

Since the implementation of the TextMarshaller interface
we could not unmarshal previously json marshalled Level value.
Fixes #873
tags/v1.3.0
David Bariod 9 months ago
parent
commit
ff695daa36
3 changed files with 87 additions and 30 deletions
  1. 62
    0
      level_test.go
  2. 25
    17
      logrus.go
  3. 0
    13
      logrus_test.go

+ 62
- 0
level_test.go View File

@@ -0,0 +1,62 @@
1
+package logrus_test
2
+
3
+import (
4
+	"bytes"
5
+	"encoding/json"
6
+	"testing"
7
+
8
+	"github.com/sirupsen/logrus"
9
+	"github.com/stretchr/testify/require"
10
+)
11
+
12
+func TestLevelJsonEncoding(t *testing.T) {
13
+	type X struct {
14
+		Level logrus.Level
15
+	}
16
+
17
+	var x X
18
+	x.Level = logrus.WarnLevel
19
+	var buf bytes.Buffer
20
+	enc := json.NewEncoder(&buf)
21
+	require.NoError(t, enc.Encode(x))
22
+	dec := json.NewDecoder(&buf)
23
+	var y X
24
+	require.NoError(t, dec.Decode(&y))
25
+}
26
+
27
+func TestLevelUnmarshalText(t *testing.T) {
28
+	var u logrus.Level
29
+	for _, level := range logrus.AllLevels {
30
+		t.Run(level.String(), func(t *testing.T) {
31
+			require.NoError(t, u.UnmarshalText([]byte(level.String())))
32
+			require.Equal(t, level, u)
33
+		})
34
+	}
35
+	t.Run("invalid", func(t *testing.T) {
36
+		require.Error(t, u.UnmarshalText([]byte("invalid")))
37
+	})
38
+}
39
+
40
+func TestLevelMarshalText(t *testing.T) {
41
+	levelStrings := []string{
42
+		"panic",
43
+		"fatal",
44
+		"error",
45
+		"warning",
46
+		"info",
47
+		"debug",
48
+		"trace",
49
+	}
50
+	for idx, val := range logrus.AllLevels {
51
+		level := val
52
+		t.Run(level.String(), func(t *testing.T) {
53
+			var cmp logrus.Level
54
+			b, err := level.MarshalText()
55
+			require.NoError(t, err)
56
+			require.Equal(t, levelStrings[idx], string(b))
57
+			err = cmp.UnmarshalText(b)
58
+			require.NoError(t, err)
59
+			require.Equal(t, level, cmp)
60
+		})
61
+	}
62
+}

+ 25
- 17
logrus.go View File

@@ -14,24 +14,11 @@ type Level uint32
14 14
 
15 15
 // Convert the Level to a string. E.g. PanicLevel becomes "panic".
16 16
 func (level Level) String() string {
17
-	switch level {
18
-	case TraceLevel:
19
-		return "trace"
20
-	case DebugLevel:
21
-		return "debug"
22
-	case InfoLevel:
23
-		return "info"
24
-	case WarnLevel:
25
-		return "warning"
26
-	case ErrorLevel:
27
-		return "error"
28
-	case FatalLevel:
29
-		return "fatal"
30
-	case PanicLevel:
31
-		return "panic"
17
+	if b, err := level.MarshalText(); err == nil {
18
+		return string(b)
19
+	} else {
20
+		return "unknown"
32 21
 	}
33
-
34
-	return "unknown"
35 22
 }
36 23
 
37 24
 // ParseLevel takes a string level and returns the Logrus log level constant.
@@ -69,6 +56,27 @@ func (level *Level) UnmarshalText(text []byte) error {
69 56
 	return nil
70 57
 }
71 58
 
59
+func (level Level) MarshalText() ([]byte, error) {
60
+	switch level {
61
+	case TraceLevel:
62
+		return []byte("trace"), nil
63
+	case DebugLevel:
64
+		return []byte("debug"), nil
65
+	case InfoLevel:
66
+		return []byte("info"), nil
67
+	case WarnLevel:
68
+		return []byte("warning"), nil
69
+	case ErrorLevel:
70
+		return []byte("error"), nil
71
+	case FatalLevel:
72
+		return []byte("fatal"), nil
73
+	case PanicLevel:
74
+		return []byte("panic"), nil
75
+	}
76
+
77
+	return nil, fmt.Errorf("not a valid lorus level %q", level)
78
+}
79
+
72 80
 // A constant exposing all logging levels
73 81
 var AllLevels = []Level{
74 82
 	PanicLevel,

+ 0
- 13
logrus_test.go View File

@@ -508,19 +508,6 @@ func TestParseLevel(t *testing.T) {
508 508
 	assert.Equal(t, "not a valid logrus Level: \"invalid\"", err.Error())
509 509
 }
510 510
 
511
-func TestUnmarshalText(t *testing.T) {
512
-	var u Level
513
-	for _, level := range AllLevels {
514
-		t.Run(level.String(), func(t *testing.T) {
515
-			assert.NoError(t, u.UnmarshalText([]byte(level.String())))
516
-			assert.Equal(t, level, u)
517
-		})
518
-	}
519
-	t.Run("invalid", func(t *testing.T) {
520
-		assert.Error(t, u.UnmarshalText([]byte("invalid")))
521
-	})
522
-}
523
-
524 511
 func TestGetSetLevelRace(t *testing.T) {
525 512
 	wg := sync.WaitGroup{}
526 513
 	for i := 0; i < 100; i++ {

Loading…
Cancel
Save