-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_test.go
123 lines (95 loc) · 3.19 KB
/
logger_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package go_logger
import (
"bytes"
"errors"
"os"
"testing"
errorsPkg "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/uniplaces/go-logger/internal"
)
func TestNew(t *testing.T) {
assert.Nil(t, instance)
config := NewConfig("env", "warning")
Init(config)
assert.NotNil(t, instance)
resetInstance()
}
func TestLogWithFields(t *testing.T) {
var buffer bytes.Buffer
err := InitWithInstance(internal.NewLogrusLogger("error", "staging", &buffer, map[string]interface{}{}))
require.Nil(t, err)
Builder().
AddField("key", "value").
AddContextField("foo", "bar").
Error(errors.New("error test"))
assert.Contains(t, buffer.String(), "\"context\":{\"foo\":\"bar\"}")
assert.Contains(t, buffer.String(), "\"key\":\"value\"")
assert.Contains(t, buffer.String(), "\"stack_trace\"")
resetInstance()
}
func TestLogWithDefaultFields(t *testing.T) {
os.Setenv("APPID", "app_id")
os.Setenv("GOENV", "go_env")
os.Setenv("GITHASH", "git_hash")
var buffer bytes.Buffer
err := InitWithInstance(internal.NewLogrusLogger("info", "staging", &buffer, map[string]interface{}{}))
require.Nil(t, err)
AddDefaultField("test-field", "field_value", false)
AddDefaultField("test-context-field", "context_field_value", true)
expectedFields := map[string]interface{}{
"type": "app",
"env": "go_env",
"git-hash": "git_hash",
"app-id": "app_id",
"test-field": "field_value",
"key": "value",
"context": map[string]interface{}{
"foo": "bar",
"test-context-field": "context_field_value",
},
}
builder := Builder()
builder.
AddField("key", "value").
AddContextField("foo", "bar").
Info("info test")
assert.Equal(t, expectedFields, builder.getFields())
resetInstance()
}
func TestLogWithFieldsAndStacktrace(t *testing.T) {
var buffer bytes.Buffer
err := InitWithInstance(internal.NewLogrusLogger("error", "staging", &buffer, map[string]interface{}{}))
require.Nil(t, err)
errorWithStackTrace := justToShowUpInStackTrace()
Builder().
AddField("key", "value").
AddContextField("foo", "bar").
Error(errorWithStackTrace)
assert.Contains(t, buffer.String(), "\"context\":{\"foo\":\"bar\"}")
assert.Contains(t, buffer.String(), "\"key\":\"value\"")
assert.Contains(t, buffer.String(), "\"stack_trace\":")
// test stack trace strings
assert.Contains(t, buffer.String(), "github.com/uniplaces/go-logger.justToShowUpInStackTrace")
assert.Contains(t, buffer.String(), "github.com/uniplaces/go-logger.TestLogWithFieldsAndStacktrace")
assert.Contains(t, buffer.String(), "github.com/uniplaces/go-logger/logger_test.go:122")
resetInstance()
}
func TestLog(t *testing.T) {
var buffer bytes.Buffer
err := InitWithInstance(internal.NewLogrusLogger("error", "staging", &buffer, map[string]interface{}{}))
require.Nil(t, err)
Error(errors.New("test error"))
assert.NotContains(t, buffer.String(), "\"context\"")
assert.NotContains(t, buffer.String(), "\"key\":\"value\"")
assert.Contains(t, buffer.String(), "\"stack_trace\"")
resetInstance()
}
func resetInstance() {
instance = nil
defaultFields = []defaultField{}
}
func justToShowUpInStackTrace() error {
return errorsPkg.New("error test")
}