forked from mirror/logrus
Merge pull request #407 from roganartu/master
Added option to disable level text truncation in default text formatter
This commit is contained in:
commit
778f2e774c
|
@ -372,6 +372,8 @@ The built-in logging formatters are:
|
||||||
field to `true`. To force no colored output even if there is a TTY set the
|
field to `true`. To force no colored output even if there is a TTY set the
|
||||||
`DisableColors` field to `true`. For Windows, see
|
`DisableColors` field to `true`. For Windows, see
|
||||||
[github.com/mattn/go-colorable](https://github.com/mattn/go-colorable).
|
[github.com/mattn/go-colorable](https://github.com/mattn/go-colorable).
|
||||||
|
* When colors are enabled, levels are truncated to 4 characters by default. To disable
|
||||||
|
truncation set the `DisableLevelTruncation` field to `true`.
|
||||||
* All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#TextFormatter).
|
* All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#TextFormatter).
|
||||||
* `logrus.JSONFormatter`. Logs fields as JSON.
|
* `logrus.JSONFormatter`. Logs fields as JSON.
|
||||||
* All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#JSONFormatter).
|
* All options are listed in the [generated docs](https://godoc.org/github.com/sirupsen/logrus#JSONFormatter).
|
||||||
|
|
|
@ -51,6 +51,10 @@ type TextFormatter struct {
|
||||||
// be desired.
|
// be desired.
|
||||||
DisableSorting bool
|
DisableSorting bool
|
||||||
|
|
||||||
|
|
||||||
|
// Disables the truncation of the level text to 4 characters.
|
||||||
|
DisableLevelTruncation bool
|
||||||
|
|
||||||
// QuoteEmptyFields will wrap empty fields in quotes if true
|
// QuoteEmptyFields will wrap empty fields in quotes if true
|
||||||
QuoteEmptyFields bool
|
QuoteEmptyFields bool
|
||||||
|
|
||||||
|
@ -125,7 +129,10 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin
|
||||||
levelColor = blue
|
levelColor = blue
|
||||||
}
|
}
|
||||||
|
|
||||||
levelText := strings.ToUpper(entry.Level.String())[0:4]
|
levelText := strings.ToUpper(entry.Level.String())
|
||||||
|
if !f.DisableLevelTruncation {
|
||||||
|
levelText = levelText[0:4]
|
||||||
|
}
|
||||||
|
|
||||||
if f.DisableTimestamp {
|
if f.DisableTimestamp {
|
||||||
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m %-44s ", levelColor, levelText, entry.Message)
|
fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m %-44s ", levelColor, levelText, entry.Message)
|
||||||
|
|
|
@ -128,6 +128,44 @@ func TestTimestampFormat(t *testing.T) {
|
||||||
checkTimeStr("")
|
checkTimeStr("")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
func TestDisableTimestampWithColoredOutput(t *testing.T) {
|
func TestDisableTimestampWithColoredOutput(t *testing.T) {
|
||||||
tf := &TextFormatter{DisableTimestamp: true, ForceColors: true}
|
tf := &TextFormatter{DisableTimestamp: true, ForceColors: true}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue