Merge pull request #452 from cjellick/readme-update
Update readme example for switching output
diff --git a/README.md b/README.md
index 77cc72a..206c746 100644
--- a/README.md
+++ b/README.md
@@ -240,6 +240,7 @@
| [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
diff --git a/json_formatter.go b/json_formatter.go
index f3729bf..266554e 100644
--- a/json_formatter.go
+++ b/json_formatter.go
@@ -26,6 +26,9 @@
// TimestampFormat sets the format used for marshaling timestamps.
TimestampFormat string
+ // DisableTimestamp allows disabling automatic timestamps in output
+ DisableTimestamp bool
+
// FieldMap allows users to customize the names of keys for various fields.
// As an example:
// formatter := &JSONFormatter{
@@ -57,7 +60,9 @@
timestampFormat = DefaultTimestampFormat
}
- data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat)
+ if !f.DisableTimestamp {
+ data[f.FieldMap.resolve(FieldKeyTime)] = entry.Time.Format(timestampFormat)
+ }
data[f.FieldMap.resolve(FieldKeyMsg)] = entry.Message
data[f.FieldMap.resolve(FieldKeyLevel)] = entry.Level.String()
diff --git a/json_formatter_test.go b/json_formatter_test.go
index 5baa93e..51093a7 100644
--- a/json_formatter_test.go
+++ b/json_formatter_test.go
@@ -169,3 +169,31 @@
t.Fatal("Expected JSON to format time key")
}
}
+
+func TestJSONDisableTimestamp(t *testing.T) {
+ formatter := &JSONFormatter{
+ DisableTimestamp: true,
+ }
+
+ b, err := formatter.Format(WithField("level", "something"))
+ if err != nil {
+ t.Fatal("Unable to format entry: ", err)
+ }
+ s := string(b)
+ if strings.Contains(s, FieldKeyTime) {
+ t.Error("Did not prevent timestamp", s)
+ }
+}
+
+func TestJSONEnableTimestamp(t *testing.T) {
+ formatter := &JSONFormatter{}
+
+ b, err := formatter.Format(WithField("level", "something"))
+ if err != nil {
+ t.Fatal("Unable to format entry: ", err)
+ }
+ s := string(b)
+ if !strings.Contains(s, FieldKeyTime) {
+ t.Error("Timestamp not present", s)
+ }
+}
diff --git a/text_formatter.go b/text_formatter.go
index 9114b3c..20f2d7e 100644
--- a/text_formatter.go
+++ b/text_formatter.go
@@ -115,7 +115,9 @@
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 @@
"errors"
"testing"
"time"
+ "strings"
)
func TestQuoting(t *testing.T) {
@@ -57,5 +58,14 @@
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.