From 98b74aac5b7a98855050754bab2631a4a8ea92e3 Mon Sep 17 00:00:00 2001 From: Carolyn Van Slyck Date: Wed, 24 Aug 2016 16:59:41 -0500 Subject: [PATCH 1/5] Allow disabling timestamps with colored output --- text_formatter.go | 4 +++- text_formatter_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/text_formatter.go b/text_formatter.go index cce61f2..b282470 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -115,7 +115,9 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin levelText := strings.ToUpper(entry.Level.String())[0:4] - if !f.FullTimestamp { + if f.DisableTimestamp { + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m %-44s ", levelColor, levelText, entry.Message) + } else if !f.FullTimestamp { fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d] %-44s ", levelColor, levelText, miniTS(), entry.Message) } else { fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), entry.Message) diff --git a/text_formatter_test.go b/text_formatter_test.go index e25a44f..107703f 100644 --- a/text_formatter_test.go +++ b/text_formatter_test.go @@ -5,6 +5,7 @@ import ( "errors" "testing" "time" + "strings" ) func TestQuoting(t *testing.T) { @@ -57,5 +58,14 @@ func TestTimestampFormat(t *testing.T) { checkTimeStr("") } +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") + } +} + // TODO add tests for sorting etc., this requires a parser for the text // formatter output. From 7d228b51ce25de4afcfa60e312d4a199c327cbb2 Mon Sep 17 00:00:00 2001 From: Craig Jellick Date: Thu, 1 Dec 2016 17:34:47 -0700 Subject: [PATCH 2/5] Update readme example for switching output Clarifies that stderr is the default, not stdout. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 05d678a..77cc72a 100644 --- a/README.md +++ b/README.md @@ -87,8 +87,8 @@ func init() { // Log as JSON instead of the default ASCII formatter. log.SetFormatter(&log.JSONFormatter{}) - // Output to stderr instead of stdout, could also be a file. - log.SetOutput(os.Stderr) + // Output to stdout instead of the default stderr, could also be a file. + log.SetOutput(os.Stdout) // Only log the warning severity or above. log.SetLevel(log.WarnLevel) From cf456d321e052c9c72e2a0f805c58da75e303ce0 Mon Sep 17 00:00:00 2001 From: puddingfactory Date: Sat, 10 Dec 2016 15:34:44 -0600 Subject: [PATCH 3/5] Add Logentrus, hook for Logentries, to list --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 05d678a..0e22574 100644 --- a/README.md +++ b/README.md @@ -240,6 +240,7 @@ Note: Syslog hook also support connecting to local syslog (Ex. "/dev/log" or "/v | [Logmatic.io](https://github.com/logmatic/logmatic-go) | Hook for logging to [Logmatic.io](http://logmatic.io/) | | [Pushover](https://github.com/toorop/logrus_pushover) | Send error via [Pushover](https://pushover.net) | | [PostgreSQL](https://github.com/gemnasium/logrus-postgresql-hook) | Send logs to [PostgreSQL](http://postgresql.org) | +| [Logentrus](https://github.com/puddingfactory/logentrus) | Hook for logging to [Logentries](https://logentries.com/) | #### Level logging From 5ed3e7dc937463e459202a4a318d5c4daada50a4 Mon Sep 17 00:00:00 2001 From: at15 Date: Tue, 10 Jan 2017 21:43:36 -0800 Subject: [PATCH 4/5] Remove miniTS in TextFormatter - Related issues: https://github.com/sirupsen/logrus/issues/457 - miniTS use current time instead of time the log function is called, which is inaccurate when hook takes a long time - `miniTS` is removed and replaced by `int(entry.Time.Sub(baseTimestamp)/time.Second)` in `printColored` which is the only usage of `miniTS` --- text_formatter.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/text_formatter.go b/text_formatter.go index 20f2d7e..dd86caa 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -28,10 +28,6 @@ func init() { isTerminal = IsTerminal() } -func miniTS() int { - return int(time.Since(baseTimestamp) / time.Second) -} - type TextFormatter struct { // Set to true to bypass checking for a TTY before outputting colors. ForceColors bool @@ -118,7 +114,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []strin if f.DisableTimestamp { fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m %-44s ", levelColor, levelText, entry.Message) } else if !f.FullTimestamp { - 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, int(entry.Time.Sub(baseTimestamp)/time.Second), entry.Message) } else { fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), entry.Message) } From f761cee9106ce559af64826f9c66a73f38e51c27 Mon Sep 17 00:00:00 2001 From: Ben Gadbois Date: Wed, 11 Jan 2017 19:19:12 +0100 Subject: [PATCH 5/5] Small var declaration cleanup --- text_formatter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/text_formatter.go b/text_formatter.go index 9114b3c..3fcd7f0 100644 --- a/text_formatter.go +++ b/text_formatter.go @@ -58,7 +58,7 @@ type TextFormatter struct { func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { var b *bytes.Buffer - var keys []string = make([]string, 0, len(entry.Data)) + keys := make([]string, 0, len(entry.Data)) for k := range entry.Data { keys = append(keys, k) }