121 lines
2.7 KiB
Go
121 lines
2.7 KiB
Go
|
package logrus
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestErrorNotLost(t *testing.T) {
|
||
|
formatter := &JSONFormatter{}
|
||
|
|
||
|
b, err := formatter.Format(WithField("error", errors.New("wild walrus")))
|
||
|
if err != nil {
|
||
|
t.Fatal("Unable to format entry: ", err)
|
||
|
}
|
||
|
|
||
|
entry := make(map[string]interface{})
|
||
|
err = json.Unmarshal(b, &entry)
|
||
|
if err != nil {
|
||
|
t.Fatal("Unable to unmarshal formatted entry: ", err)
|
||
|
}
|
||
|
|
||
|
if entry["error"] != "wild walrus" {
|
||
|
t.Fatal("Error field not set")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestErrorNotLostOnFieldNotNamedError(t *testing.T) {
|
||
|
formatter := &JSONFormatter{}
|
||
|
|
||
|
b, err := formatter.Format(WithField("omg", errors.New("wild walrus")))
|
||
|
if err != nil {
|
||
|
t.Fatal("Unable to format entry: ", err)
|
||
|
}
|
||
|
|
||
|
entry := make(map[string]interface{})
|
||
|
err = json.Unmarshal(b, &entry)
|
||
|
if err != nil {
|
||
|
t.Fatal("Unable to unmarshal formatted entry: ", err)
|
||
|
}
|
||
|
|
||
|
if entry["omg"] != "wild walrus" {
|
||
|
t.Fatal("Error field not set")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestFieldClashWithTime(t *testing.T) {
|
||
|
formatter := &JSONFormatter{}
|
||
|
|
||
|
b, err := formatter.Format(WithField("time", "right now!"))
|
||
|
if err != nil {
|
||
|
t.Fatal("Unable to format entry: ", err)
|
||
|
}
|
||
|
|
||
|
entry := make(map[string]interface{})
|
||
|
err = json.Unmarshal(b, &entry)
|
||
|
if err != nil {
|
||
|
t.Fatal("Unable to unmarshal formatted entry: ", err)
|
||
|
}
|
||
|
|
||
|
if entry["fields.time"] != "right now!" {
|
||
|
t.Fatal("fields.time not set to original time field")
|
||
|
}
|
||
|
|
||
|
if entry["time"] != "0001-01-01T00:00:00Z" {
|
||
|
t.Fatal("time field not set to current time, was: ", entry["time"])
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestFieldClashWithMsg(t *testing.T) {
|
||
|
formatter := &JSONFormatter{}
|
||
|
|
||
|
b, err := formatter.Format(WithField("msg", "something"))
|
||
|
if err != nil {
|
||
|
t.Fatal("Unable to format entry: ", err)
|
||
|
}
|
||
|
|
||
|
entry := make(map[string]interface{})
|
||
|
err = json.Unmarshal(b, &entry)
|
||
|
if err != nil {
|
||
|
t.Fatal("Unable to unmarshal formatted entry: ", err)
|
||
|
}
|
||
|
|
||
|
if entry["fields.msg"] != "something" {
|
||
|
t.Fatal("fields.msg not set to original msg field")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestFieldClashWithLevel(t *testing.T) {
|
||
|
formatter := &JSONFormatter{}
|
||
|
|
||
|
b, err := formatter.Format(WithField("level", "something"))
|
||
|
if err != nil {
|
||
|
t.Fatal("Unable to format entry: ", err)
|
||
|
}
|
||
|
|
||
|
entry := make(map[string]interface{})
|
||
|
err = json.Unmarshal(b, &entry)
|
||
|
if err != nil {
|
||
|
t.Fatal("Unable to unmarshal formatted entry: ", err)
|
||
|
}
|
||
|
|
||
|
if entry["fields.level"] != "something" {
|
||
|
t.Fatal("fields.level not set to original level field")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestJSONEntryEndsWithNewline(t *testing.T) {
|
||
|
formatter := &JSONFormatter{}
|
||
|
|
||
|
b, err := formatter.Format(WithField("level", "something"))
|
||
|
if err != nil {
|
||
|
t.Fatal("Unable to format entry: ", err)
|
||
|
}
|
||
|
|
||
|
if b[len(b)-1] != '\n' {
|
||
|
t.Fatal("Expected JSON log entry to end with a newline")
|
||
|
}
|
||
|
}
|