2015-03-09 18:15:08 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2016-08-15 21:04:25 +03:00
|
|
|
"strings"
|
2015-03-09 18:15:08 +03:00
|
|
|
"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")
|
|
|
|
}
|
|
|
|
}
|
2015-03-09 18:30:43 +03:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
2016-08-15 21:04:25 +03:00
|
|
|
|
|
|
|
func TestJSONMessageKey(t *testing.T) {
|
2016-11-18 02:16:46 +03:00
|
|
|
formatter := &JSONFormatter{
|
|
|
|
FieldMap: FieldMap{
|
|
|
|
FieldKeyMsg: "message",
|
|
|
|
},
|
|
|
|
}
|
2016-08-15 21:04:25 +03:00
|
|
|
|
|
|
|
b, err := formatter.Format(&Entry{Message: "oh hai"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to format entry: ", err)
|
|
|
|
}
|
|
|
|
s := string(b)
|
|
|
|
if !(strings.Contains(s, "message") && strings.Contains(s, "oh hai")) {
|
|
|
|
t.Fatal("Expected JSON to format message key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONLevelKey(t *testing.T) {
|
2016-11-18 02:16:46 +03:00
|
|
|
formatter := &JSONFormatter{
|
|
|
|
FieldMap: FieldMap{
|
|
|
|
FieldKeyLevel: "somelevel",
|
|
|
|
},
|
|
|
|
}
|
2016-08-15 21:04:25 +03:00
|
|
|
|
|
|
|
b, err := formatter.Format(WithField("level", "something"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to format entry: ", err)
|
|
|
|
}
|
|
|
|
s := string(b)
|
|
|
|
if !strings.Contains(s, "somelevel") {
|
|
|
|
t.Fatal("Expected JSON to format level key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONTimeKey(t *testing.T) {
|
2016-11-18 02:16:46 +03:00
|
|
|
formatter := &JSONFormatter{
|
|
|
|
FieldMap: FieldMap{
|
|
|
|
FieldKeyTime: "timeywimey",
|
|
|
|
},
|
|
|
|
}
|
2016-08-15 21:04:25 +03:00
|
|
|
|
|
|
|
b, err := formatter.Format(WithField("level", "something"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to format entry: ", err)
|
|
|
|
}
|
|
|
|
s := string(b)
|
|
|
|
if !strings.Contains(s, "timeywimey") {
|
|
|
|
t.Fatal("Expected JSON to format time key")
|
|
|
|
}
|
|
|
|
}
|
2016-11-26 06:02:56 +03:00
|
|
|
|
2016-11-29 20:35:34 +03:00
|
|
|
func TestFieldDoesNotClashWithCaller(t *testing.T) {
|
|
|
|
SetReportCaller(false)
|
2016-11-26 06:02:56 +03:00
|
|
|
formatter := &JSONFormatter{}
|
|
|
|
|
2016-12-06 23:53:21 +03:00
|
|
|
b, err := formatter.Format(WithField("func", "howdy pardner"))
|
2016-11-26 06:02:56 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-12-06 23:53:21 +03:00
|
|
|
if entry["func"] != "howdy pardner" {
|
|
|
|
t.Fatal("func field replaced when ReportCaller=false")
|
2016-11-26 06:02:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:35:34 +03:00
|
|
|
func TestFieldClashWithCaller(t *testing.T) {
|
|
|
|
SetReportCaller(true)
|
2016-11-26 06:02:56 +03:00
|
|
|
formatter := &JSONFormatter{}
|
2016-12-06 23:53:21 +03:00
|
|
|
e := WithField("func", "howdy pardner")
|
2016-12-01 02:15:38 +03:00
|
|
|
e.Caller = "somefunc"
|
|
|
|
b, err := formatter.Format(e)
|
2016-11-26 06:02:56 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-12-06 23:53:21 +03:00
|
|
|
if entry["fields.func"] != "howdy pardner" {
|
|
|
|
t.Fatalf("fields.func not set to original func field when ReportCaller=true (got '%s')",
|
|
|
|
entry["fields.func"])
|
2016-11-26 06:02:56 +03:00
|
|
|
}
|
|
|
|
|
2016-12-06 23:53:21 +03:00
|
|
|
if entry["func"] != "somefunc" {
|
|
|
|
t.Fatalf("func not set as expected when ReportCaller=true (got '%s')",
|
|
|
|
entry["func"])
|
2016-11-26 06:02:56 +03:00
|
|
|
}
|
|
|
|
|
2016-11-29 20:35:34 +03:00
|
|
|
SetReportCaller(false) // return to default value
|
2016-11-26 06:02:56 +03:00
|
|
|
}
|
2016-12-02 20:38:12 +03:00
|
|
|
|
2016-11-21 21:09:59 +03:00
|
|
|
func TestJSONDisableTimestamp(t *testing.T) {
|
|
|
|
formatter := &JSONFormatter{
|
|
|
|
DisableTimestamp: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := formatter.Format(WithField("level", "something"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to format entry: ", err)
|
|
|
|
}
|
|
|
|
s := string(b)
|
2016-11-21 21:16:24 +03:00
|
|
|
if strings.Contains(s, FieldKeyTime) {
|
2016-11-21 21:09:59 +03:00
|
|
|
t.Error("Did not prevent timestamp", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJSONEnableTimestamp(t *testing.T) {
|
|
|
|
formatter := &JSONFormatter{}
|
|
|
|
|
|
|
|
b, err := formatter.Format(WithField("level", "something"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to format entry: ", err)
|
|
|
|
}
|
|
|
|
s := string(b)
|
2016-11-21 21:16:24 +03:00
|
|
|
if !strings.Contains(s, FieldKeyTime) {
|
2016-11-21 21:09:59 +03:00
|
|
|
t.Error("Timestamp not present", s)
|
|
|
|
}
|
|
|
|
}
|