From 7a1f601cfd7604dd99fa6d6af0aa3e2a3f3de6b4 Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Wed, 31 Aug 2016 23:54:59 +1000 Subject: [PATCH 1/3] Added ability to disable level text truncation. Fixes #406 --- text_formatter.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/text_formatter.go b/text_formatter.go index cce61f2..a51bf19 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -54,6 +54,9 @@ type TextFormatter struct { // that log extremely frequently and don't use the JSON formatter this may not // be desired. DisableSorting bool + + // Disables the truncation of the level text to 4 characters. + DisableLevelTruncation bool } func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { @@ -113,7 +116,10 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin 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.FullTimestamp { fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Message) From e5b6713580618875560ec106fc60a2ac82e14c96 Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Thu, 1 Sep 2016 00:28:23 +1000 Subject: [PATCH 2/3] Added testing for DisableLevelTruncation --- text_formatter_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/text_formatter_test.go b/text_formatter_test.go index e25a44f..6b5353d 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -3,6 +3,7 @@ package logrus import ( "bytes" "errors" + "strings" "testing" "time" ) @@ -57,5 +58,43 @@ func TestTimestampFormat(t *testing.T) { 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) +} + // TODO add tests for sorting etc., this requires a parser for the text // formatter output. From 1f59c9ad125a4f3f3d172f2b3d67cee2fa217b74 Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Tue, 24 Jan 2017 23:05:25 +1100 Subject: [PATCH 3/3] Add DisableLevelTruncation description to README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 206c746..7622d31 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,8 @@ The built-in logging formatters are: * *Note:* to force colored output when there is no TTY, set the `ForceColors` field to `true`. To force no colored output even if there is a TTY set the `DisableColors` field to `true` + * When colors are enabled, levels are truncated to 4 characters by default. To disable + truncation set the `DisableLevelTruncation` field to `true`. * `logrus.JSONFormatter`. Logs fields as JSON. Third party logging formatters: