-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlogentrus_test.go
79 lines (66 loc) · 2.72 KB
/
logentrus_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
package logentrus_test
import (
"os"
"testing"
"github.com/jonathan-robertson/logentrus"
"github.com/sirupsen/logrus"
)
func init() {
logrus.SetLevel(logrus.DebugLevel) // This will effect your stdout level, but not the level for LogentriesHook. You specify that priority on creation
logrus.SetFormatter(&logrus.TextFormatter{}) // You an use any formatter; LogentriesHook will always format as JSON without interfering with your other hooks
hook, err := logentrus.New(
os.Getenv("LOGENTRIESTOKEN"), // fetching token from env vars here. You can make a token in your logentries account and are expected to have 1 token for each application
&logentrus.Opts{ // include options (set to nil if options not necessary)
Priority: logrus.InfoLevel, // log level is inclusive. Setting to logrus.ErrorLevel, for example, would include errors, panics, and fatals, but not info or debug.
TimestampFormat: "Jan 2 15:04:05", // timeFormat could be an empty string instead; doing so will default to logrus's typically time format.
EncTLSConfig: nil, // setting config to nil means that conn will use root certs already set up on local system
UnencryptedTCP: true, // disable encryption, but still use TCP
UnencryptedUDP: false, // disable encryption and use UDP
UnencryptedPort: 514, // omitting will result in port 514 usage; valid options are 80, 514, and 10000
},
)
if err != nil {
panic(err)
}
logrus.AddHook(hook)
}
func TestDebug(t *testing.T) {
logrus.Debug("This is a debug entry that should *not* show in logentries") // This won't appear in logentries due to the priority we set
}
func TestInfo(t *testing.T) {
logrus.WithField("anotherField", "hi there!").Info("This is an info entry that should show up in logentries")
}
func TestError(t *testing.T) {
logrus.WithField("the rent", "is too dang high").Error("This is an error entry that should also appear in logentries")
}
func TestHandlePanic(t *testing.T) {
defer func() {
err := recover()
if err != nil {
logrus.WithFields(logrus.Fields{
"omg": true,
"err": err,
"number": 100,
}).Error("The ice breaks! (recovered from panic)")
}
}()
logrus.WithFields(logrus.Fields{
"animal": "walrus",
"number": 8,
}).Debug("Started observing beach")
logrus.WithFields(logrus.Fields{
"animal": "walrus",
"size": 10,
}).Info("A group of walrus emerges from the ocean")
logrus.WithFields(logrus.Fields{
"omg": true,
"number": 122,
}).Warn("The group's number increased tremendously!")
logrus.WithFields(logrus.Fields{
"temperature": -4,
}).Debug("Temperature changes")
logrus.WithFields(logrus.Fields{
"animal": "orca",
"size": 9009,
}).Panic("It's over 9000!")
}