2014-12-15 22:20:33 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2017-07-13 14:46:35 +03:00
|
|
|
"fmt"
|
2017-02-14 13:53:03 +03:00
|
|
|
"strings"
|
2014-12-15 22:20:33 +03:00
|
|
|
"testing"
|
2015-03-27 02:04:45 +03:00
|
|
|
"time"
|
2017-11-22 06:43:47 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2014-12-15 22:20:33 +03:00
|
|
|
)
|
|
|
|
|
2017-07-21 17:06:23 +03:00
|
|
|
func TestFormatting(t *testing.T) {
|
|
|
|
tf := &TextFormatter{DisableColors: true}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
value string
|
|
|
|
expected string
|
|
|
|
}{
|
2017-07-21 17:14:28 +03:00
|
|
|
{`foo`, "time=\"0001-01-01T00:00:00Z\" level=panic test=foo\n"},
|
2017-07-21 17:06:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
b, _ := tf.Format(WithField("test", tc.value))
|
|
|
|
|
|
|
|
if string(b) != tc.expected {
|
|
|
|
t.Errorf("formatting expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 22:20:33 +03:00
|
|
|
func TestQuoting(t *testing.T) {
|
2014-12-18 09:59:41 +03:00
|
|
|
tf := &TextFormatter{DisableColors: true}
|
2014-12-15 22:20:33 +03:00
|
|
|
|
|
|
|
checkQuoting := func(q bool, value interface{}) {
|
|
|
|
b, _ := tf.Format(WithField("test", value))
|
2014-12-18 09:59:41 +03:00
|
|
|
idx := bytes.Index(b, ([]byte)("test="))
|
2017-07-13 14:46:35 +03:00
|
|
|
cont := bytes.Contains(b[idx+5:], []byte("\""))
|
2014-12-15 22:20:33 +03:00
|
|
|
if cont != q {
|
|
|
|
if q {
|
|
|
|
t.Errorf("quoting expected for: %#v", value)
|
|
|
|
} else {
|
|
|
|
t.Errorf("quoting not expected for: %#v", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-14 13:53:03 +03:00
|
|
|
checkQuoting(false, "")
|
2014-12-15 22:20:33 +03:00
|
|
|
checkQuoting(false, "abcd")
|
|
|
|
checkQuoting(false, "v1.0")
|
2015-03-04 17:04:50 +03:00
|
|
|
checkQuoting(false, "1234567890")
|
2017-06-18 16:48:52 +03:00
|
|
|
checkQuoting(false, "/foobar")
|
|
|
|
checkQuoting(false, "foo_bar")
|
|
|
|
checkQuoting(false, "foo@bar")
|
|
|
|
checkQuoting(false, "foobar^")
|
|
|
|
checkQuoting(false, "+/-_^@f.oobar")
|
|
|
|
checkQuoting(true, "foobar$")
|
|
|
|
checkQuoting(true, "&foobar")
|
2014-12-15 22:20:33 +03:00
|
|
|
checkQuoting(true, "x y")
|
|
|
|
checkQuoting(true, "x,y")
|
|
|
|
checkQuoting(false, errors.New("invalid"))
|
|
|
|
checkQuoting(true, errors.New("invalid argument"))
|
2017-02-14 13:53:03 +03:00
|
|
|
|
2017-02-14 14:51:23 +03:00
|
|
|
// Test for quoting empty fields.
|
2017-02-14 13:53:03 +03:00
|
|
|
tf.QuoteEmptyFields = true
|
|
|
|
checkQuoting(true, "")
|
2017-02-15 16:08:26 +03:00
|
|
|
checkQuoting(false, "abcd")
|
|
|
|
checkQuoting(true, errors.New("invalid argument"))
|
2014-12-15 22:20:33 +03:00
|
|
|
}
|
2015-03-09 18:19:51 +03:00
|
|
|
|
2017-07-13 14:46:35 +03:00
|
|
|
func TestEscaping(t *testing.T) {
|
2017-07-10 15:09:37 +03:00
|
|
|
tf := &TextFormatter{DisableColors: true}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
value string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{`ba"r`, `ba\"r`},
|
|
|
|
{`ba'r`, `ba'r`},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
b, _ := tf.Format(WithField("test", tc.value))
|
|
|
|
if !bytes.Contains(b, []byte(tc.expected)) {
|
|
|
|
t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 18:29:56 +03:00
|
|
|
func TestEscaping_Interface(t *testing.T) {
|
2017-07-12 18:15:13 +03:00
|
|
|
tf := &TextFormatter{DisableColors: true}
|
2017-07-12 18:29:56 +03:00
|
|
|
|
2017-07-12 18:15:13 +03:00
|
|
|
ts := time.Now()
|
|
|
|
|
2017-07-12 18:29:56 +03:00
|
|
|
testCases := []struct {
|
|
|
|
value interface{}
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{ts, fmt.Sprintf("\"%s\"", ts.String())},
|
|
|
|
{errors.New("error: something went wrong"), "\"error: something went wrong\""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
b, _ := tf.Format(WithField("test", tc.value))
|
|
|
|
if !bytes.Contains(b, []byte(tc.expected)) {
|
|
|
|
t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
|
|
|
|
}
|
2017-07-12 18:15:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 02:04:45 +03:00
|
|
|
func TestTimestampFormat(t *testing.T) {
|
|
|
|
checkTimeStr := func(format string) {
|
|
|
|
customFormatter := &TextFormatter{DisableColors: true, TimestampFormat: format}
|
|
|
|
customStr, _ := customFormatter.Format(WithField("test", "test"))
|
|
|
|
timeStart := bytes.Index(customStr, ([]byte)("time="))
|
|
|
|
timeEnd := bytes.Index(customStr, ([]byte)("level="))
|
2017-07-13 14:46:35 +03:00
|
|
|
timeStr := customStr[timeStart+5+len("\"") : timeEnd-1-len("\"")]
|
2015-03-27 02:04:45 +03:00
|
|
|
if format == "" {
|
|
|
|
format = time.RFC3339
|
|
|
|
}
|
|
|
|
_, e := time.Parse(format, (string)(timeStr))
|
|
|
|
if e != nil {
|
|
|
|
t.Errorf("time string \"%s\" did not match provided time format \"%s\": %s", timeStr, format, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkTimeStr("2006-01-02T15:04:05.000000000Z07:00")
|
|
|
|
checkTimeStr("Mon Jan _2 15:04:05 2006")
|
|
|
|
checkTimeStr("")
|
|
|
|
}
|
|
|
|
|
2016-08-31 17:28:23 +03:00
|
|
|
func TestDisableLevelTruncation(t *testing.T) {
|
|
|
|
entry := &Entry{
|
|
|
|
Time: time.Now(),
|
|
|
|
Message: "testing",
|
|
|
|
}
|
|
|
|
keys := []string{}
|
|
|
|
timestampFormat := "Mon Jan 2 15:04:05 -0700 MST 2006"
|
|
|
|
checkDisableTruncation := func(disabled bool, level Level) {
|
|
|
|
tf := &TextFormatter{DisableLevelTruncation: disabled}
|
|
|
|
var b bytes.Buffer
|
|
|
|
entry.Level = level
|
|
|
|
tf.printColored(&b, entry, keys, timestampFormat)
|
|
|
|
logLine := (&b).String()
|
|
|
|
if disabled {
|
|
|
|
expected := strings.ToUpper(level.String())
|
|
|
|
if !strings.Contains(logLine, expected) {
|
|
|
|
t.Errorf("level string expected to be %s when truncation disabled", expected)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
expected := strings.ToUpper(level.String())
|
|
|
|
if len(level.String()) > 4 {
|
|
|
|
if strings.Contains(logLine, expected) {
|
|
|
|
t.Errorf("level string %s expected to be truncated to %s when truncation is enabled", expected, expected[0:4])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !strings.Contains(logLine, expected) {
|
|
|
|
t.Errorf("level string expected to be %s when truncation is enabled and level string is below truncation threshold", expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDisableTruncation(true, DebugLevel)
|
|
|
|
checkDisableTruncation(true, InfoLevel)
|
|
|
|
checkDisableTruncation(false, ErrorLevel)
|
|
|
|
checkDisableTruncation(false, InfoLevel)
|
|
|
|
}
|
|
|
|
|
2016-08-25 00:59:41 +03:00
|
|
|
func TestDisableTimestampWithColoredOutput(t *testing.T) {
|
|
|
|
tf := &TextFormatter{DisableTimestamp: true, ForceColors: true}
|
|
|
|
|
|
|
|
b, _ := tf.Format(WithField("test", "test"))
|
|
|
|
if strings.Contains(string(b), "[0000]") {
|
|
|
|
t.Error("timestamp not expected when DisableTimestamp is true")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 06:43:47 +03:00
|
|
|
func TestTextFormatterFieldMap(t *testing.T) {
|
|
|
|
formatter := &TextFormatter{
|
|
|
|
DisableColors: true,
|
|
|
|
FieldMap: FieldMap{
|
|
|
|
FieldKeyMsg: "message",
|
|
|
|
FieldKeyLevel: "somelevel",
|
|
|
|
FieldKeyTime: "timeywimey",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
entry := &Entry{
|
|
|
|
Message: "oh hi",
|
|
|
|
Level: WarnLevel,
|
|
|
|
Time: time.Date(1981, time.February, 24, 4, 28, 3, 100, time.UTC),
|
2018-06-19 04:32:35 +03:00
|
|
|
Data: Fields{
|
|
|
|
"field1": "f1",
|
|
|
|
"message": "messagefield",
|
|
|
|
"somelevel": "levelfield",
|
|
|
|
"timeywimey": "timeywimeyfield",
|
|
|
|
},
|
2017-11-22 06:43:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := formatter.Format(entry)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unable to format entry: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t,
|
2018-06-19 04:32:35 +03:00
|
|
|
`timeywimey="1981-02-24T04:28:03Z" `+
|
|
|
|
`somelevel=warning `+
|
|
|
|
`message="oh hi" `+
|
|
|
|
`field1=f1 `+
|
|
|
|
`fields.message=messagefield `+
|
|
|
|
`fields.somelevel=levelfield `+
|
|
|
|
`fields.timeywimey=timeywimeyfield`+"\n",
|
2017-11-22 06:43:47 +03:00
|
|
|
string(b),
|
2018-06-19 04:39:53 +03:00
|
|
|
"Formatted output doesn't respect FieldMap")
|
2017-11-22 06:43:47 +03:00
|
|
|
}
|
|
|
|
|
2015-03-09 18:19:51 +03:00
|
|
|
// TODO add tests for sorting etc., this requires a parser for the text
|
|
|
|
// formatter output.
|