mirror of https://github.com/sirupsen/logrus.git
Merge pull request #100 from crquan/patch-1
make sure no trailing spaces
This commit is contained in:
commit
c6a969a0de
|
@ -59,17 +59,17 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
printColored(b, entry, keys)
|
printColored(b, entry, keys)
|
||||||
} else {
|
} else {
|
||||||
if !f.DisableTimestamp {
|
if !f.DisableTimestamp {
|
||||||
f.appendKeyValue(b, "time", entry.Time.Format(time.RFC3339))
|
printKeyValue(b, "time", entry.Time.Format(time.RFC3339))
|
||||||
}
|
}
|
||||||
f.appendKeyValue(b, "level", entry.Level.String())
|
printKeyValue(b, "level", entry.Level.String())
|
||||||
f.appendKeyValue(b, "msg", entry.Message)
|
printKeyValue(b, "msg", entry.Message)
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
f.appendKeyValue(b, key, entry.Data[key])
|
printKeyValue(b, key, entry.Data[key])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
b.WriteByte('\n')
|
b.WriteByte('\n')
|
||||||
return b.Bytes(), nil
|
return b.Bytes()[1:], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func printColored(b *bytes.Buffer, entry *Entry, keys []string) {
|
func printColored(b *bytes.Buffer, entry *Entry, keys []string) {
|
||||||
|
@ -85,7 +85,7 @@ func printColored(b *bytes.Buffer, entry *Entry, keys []string) {
|
||||||
|
|
||||||
levelText := strings.ToUpper(entry.Level.String())[0:4]
|
levelText := strings.ToUpper(entry.Level.String())[0:4]
|
||||||
|
|
||||||
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Message)
|
fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m[%04d] %-44s", levelColor, levelText, miniTS(), entry.Message)
|
||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
v := entry.Data[k]
|
v := entry.Data[k]
|
||||||
fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=%v", levelColor, k, v)
|
fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=%v", levelColor, k, v)
|
||||||
|
@ -104,21 +104,19 @@ func needsQuoting(text string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key, value interface{}) {
|
func printKeyValue(b *bytes.Buffer, key, value interface{}) {
|
||||||
switch value.(type) {
|
switch value.(type) {
|
||||||
case string:
|
case string:
|
||||||
if needsQuoting(value.(string)) {
|
break
|
||||||
fmt.Fprintf(b, "%v=%s ", key, value)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(b, "%v=%q ", key, value)
|
|
||||||
}
|
|
||||||
case error:
|
case error:
|
||||||
if needsQuoting(value.(error).Error()) {
|
value = value.(error).Error()
|
||||||
fmt.Fprintf(b, "%v=%s ", key, value)
|
|
||||||
} else {
|
|
||||||
fmt.Fprintf(b, "%v=%q ", key, value)
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
fmt.Fprintf(b, "%v=%v ", key, value)
|
fmt.Fprintf(b, " %v=%v", key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
if needsQuoting(value.(string)) {
|
||||||
|
fmt.Fprintf(b, " %v=%s", key, value)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(b, " %v=%q", key, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,3 +31,30 @@ func TestQuoting(t *testing.T) {
|
||||||
checkQuoting(false, errors.New("invalid"))
|
checkQuoting(false, errors.New("invalid"))
|
||||||
checkQuoting(true, errors.New("invalid argument"))
|
checkQuoting(true, errors.New("invalid argument"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTextPrint(t *testing.T) {
|
||||||
|
tf := &TextFormatter{DisableColors: true}
|
||||||
|
byts, _ := tf.Format(&Entry{Message: "msg content"})
|
||||||
|
|
||||||
|
// make sure no leading or trailing spaces
|
||||||
|
if string(byts) !=
|
||||||
|
"time=\"0001-01-01T00:00:00Z\" level=panic msg=\"msg content\"\n" {
|
||||||
|
t.Errorf("not expected: %q", string(byts))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestColorPrint(t *testing.T) {
|
||||||
|
tf := &TextFormatter{ForceColors: true}
|
||||||
|
entry := WithField("testkey", "value")
|
||||||
|
entry.Message = "msg content"
|
||||||
|
byts, _ := tf.Format(entry)
|
||||||
|
|
||||||
|
// make sure no leading or trailing spaces
|
||||||
|
if string(byts) !=
|
||||||
|
"\x1b[31mPANI\x1b[0m[0000] " +
|
||||||
|
// length 44 plus one space
|
||||||
|
"msg content " +
|
||||||
|
"\x1b[31mtestkey\x1b[0m=value\n" {
|
||||||
|
t.Errorf("not expected: %q", string(byts))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue