forked from shortcut/logrus-stackdriver-formatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_test.go
64 lines (51 loc) · 2.7 KB
/
error_test.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package stackdriver_test
import (
"strconv"
"strings"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
stackdriver "github.com/jonklippenstein-io/logrus-stackdriver-formatter"
)
func TestLogError(t *testing.T) {
logger := logrus.New()
strBuilder := strings.Builder{}
logger.Out = &strBuilder
logger.Formatter = stackdriver.NewFormatter(
stackdriver.WithService("test-service"),
stackdriver.WithVersion("v0.1.0"),
)
logger.Info("application up and running")
_, err := strconv.ParseInt("text", 10, 64)
require.EqualError(t, err, "strconv.ParseInt: parsing \"text\": invalid syntax")
logger.WithError(err).Error("unable to parse integer")
res := strBuilder.String()
require.NotEmpty(t, res)
require.Contains(t, res, "application up and running")
require.Contains(t, res, "strconv.ParseInt: parsing \\\"text\\\": invalid syntax") // Note: Double-escaped, as it is stored as JSON.
require.Contains(t, res, "unable to parse integer")
// Output:
// {"timestamp":"2020-10-12T12:26:00Z","message":"application up and running","severity":"INFO","context":{}}
// {"timestamp":"2020-10-12T12:26:00Z","serviceContext":{"service":"test-service","version":"v0.1.0"},"message":"unable to parse integer: strconv.ParseInt: parsing \"text\": invalid syntax","severity":"ERROR","context":{"reportLocation":{"filePath":"github.com/jonklippenstein-io/logrus-stackdriver-formatter/example_test.go","lineNumber":26,"functionName":"TestLogError"}}}
}
func TestLogWarning(t *testing.T) {
logger := logrus.New()
strBuilder := strings.Builder{}
logger.Out = &strBuilder
logger.Formatter = stackdriver.NewFormatter(
stackdriver.WithService("test-service"),
stackdriver.WithVersion("v0.1.0"),
)
logger.Info("application up and running")
_, err := strconv.ParseInt("text", 10, 64)
require.EqualError(t, err, "strconv.ParseInt: parsing \"text\": invalid syntax")
logger.WithError(err).Warning("unable to parse integer")
res := strBuilder.String()
require.NotEmpty(t, res)
require.Contains(t, res, "application up and running")
require.Contains(t, res, "strconv.ParseInt: parsing \\\"text\\\": invalid syntax") // Note: Double-escaped, as it is stored as JSON.
require.Contains(t, res, "unable to parse integer")
// Output:
// {"timestamp":"2020-10-12T12:26:00Z","message":"application up and running","severity":"INFO","context":{}}
// {"timestamp":"2020-10-12T12:26:00Z","serviceContext":{"service":"test-service","version":"v0.1.0"},"message":"unable to parse integer: strconv.ParseInt: parsing \"text\": invalid syntax","severity":"WARNING","context":{"reportLocation":{"filePath":"github.com/jonklippenstein-io/logrus-stackdriver-formatter/example_test.go","lineNumber":26,"functionName":"TestLogError"}}}
}