From c20103e9811461029db216ca151be9ab86144286 Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Wed, 3 Dec 2014 13:28:16 -0500 Subject: [PATCH 1/3] The default backend was printing the date and time before the message This means that the default formatter was printing the date instead of just the message as documented, and setting a custom format for the default log would have the time prepended to the message. --- logger.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/logger.go b/logger.go index b3a57a8..73f72c8 100644 --- a/logger.go +++ b/logger.go @@ -10,7 +10,6 @@ package logging import ( "bytes" "fmt" - "log" "os" "strings" "sync/atomic" @@ -111,7 +110,7 @@ func Reset() { // if there's no backends at all configured, we could use some tricks to // automatically setup backends based if we have a TTY or not. sequenceNo = 0 - b := SetBackend(NewLogBackend(os.Stderr, "", log.LstdFlags)) + b := SetBackend(NewLogBackend(os.Stderr, "", 0)) b.SetLevel(DEBUG, "") SetFormatter(DefaultFormatter) timeNow = time.Now From ffa6b12fbfda8df326408deb00a9ec9ae35838a7 Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Sat, 20 Dec 2014 14:43:30 -0500 Subject: [PATCH 2/3] Default log format is now date, time and message. --- format.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/format.go b/format.go index 99b1ddb..69475f7 100644 --- a/format.go +++ b/format.go @@ -114,8 +114,8 @@ func getFormatter() Formatter { } var ( - // DefaultFormatter is the default formatter used and is only the message. - DefaultFormatter Formatter = MustStringFormatter("%{message}") + // DefaultFormatter is the default formatter used and is the date, time and the message. + DefaultFormatter Formatter = MustStringFormatter("%{time:2006-01-02 15:04:05.999} %{message}") // Glog format GlogFormatter Formatter = MustStringFormatter("%{level:.1s}%{time:0102 15:04:05.999999} %{pid} %{shortfile}] %{message}") From 1188f75794c1b9d710c49826c7c77b15cc7fd890 Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Sat, 20 Dec 2014 15:00:25 -0500 Subject: [PATCH 3/3] Updated unit tests based on new default message format --- example_test.go | 4 ++-- format_test.go | 2 +- logger_test.go | 2 +- memory_test.go | 12 ++++++------ multi_test.go | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/example_test.go b/example_test.go index deb8900..172e6d5 100644 --- a/example_test.go +++ b/example_test.go @@ -34,7 +34,7 @@ func Example() { log.Error("error") // Output: - // debug arg - // error + // 1970-01-01 00:00:00 debug arg + // 1970-01-01 00:00:00 error // 00:00:00.000 Example E error } diff --git a/format_test.go b/format_test.go index c008e9e..092e46e 100644 --- a/format_test.go +++ b/format_test.go @@ -156,7 +156,7 @@ func TestBackendFormatter(t *testing.T) { log := MustGetLogger("module") log.Info("foo") - if "foo" != getLastLine(b1) { + if "1970-01-01 00:00:00 foo" != getLastLine(b1) { t.Errorf("Unexpected line: %s", getLastLine(b1)) } if "INFO foo" != getLastLine(b2) { diff --git a/logger_test.go b/logger_test.go index acf2498..a43e928 100644 --- a/logger_test.go +++ b/logger_test.go @@ -30,7 +30,7 @@ func TestRedact(t *testing.T) { password := Password("123456") log := MustGetLogger("test") log.Debug("foo %s", password) - if "foo ******" != MemoryRecordN(backend, 0).Formatted(0) { + if "1970-01-01 00:00:00 foo ******" != MemoryRecordN(backend, 0).Formatted(0) { t.Errorf("redacted line: %v", MemoryRecordN(backend, 0)) } } diff --git a/memory_test.go b/memory_test.go index fe5a82e..301f1e9 100644 --- a/memory_test.go +++ b/memory_test.go @@ -58,17 +58,17 @@ func TestMemoryBackend(t *testing.T) { t.Errorf("record length: %d", backend.size) } record := MemoryRecordN(backend, 0) - if "5" != record.Formatted(0) { + if "1970-01-01 00:00:00 5" != record.Formatted(0) { t.Errorf("unexpected start: %s", record.Formatted(0)) } for i := 0; i < 8; i++ { record = MemoryRecordN(backend, i) - if strconv.Itoa(i+5) != record.Formatted(0) { + if "1970-01-01 00:00:00 " + strconv.Itoa(i+5) != record.Formatted(0) { t.Errorf("unexpected record: %v", record.Formatted(0)) } } record = MemoryRecordN(backend, 7) - if "12" != record.Formatted(0) { + if "1970-01-01 00:00:00 12" != record.Formatted(0) { t.Errorf("unexpected end: %s", record.Formatted(0)) } record = MemoryRecordN(backend, 8) @@ -97,17 +97,17 @@ func TestChannelMemoryBackend(t *testing.T) { t.Errorf("record length: %d", backend.size) } record := ChannelMemoryRecordN(backend, 0) - if "5" != record.Formatted(0) { + if "1970-01-01 00:00:00 5" != record.Formatted(0) { t.Errorf("unexpected start: %s", record.Formatted(0)) } for i := 0; i < 8; i++ { record = ChannelMemoryRecordN(backend, i) - if strconv.Itoa(i+5) != record.Formatted(0) { + if "1970-01-01 00:00:00 " + strconv.Itoa(i+5) != record.Formatted(0) { t.Errorf("unexpected record: %v", record.Formatted(0)) } } record = ChannelMemoryRecordN(backend, 7) - if "12" != record.Formatted(0) { + if "1970-01-01 00:00:00 12" != record.Formatted(0) { t.Errorf("unexpected end: %s", record.Formatted(0)) } record = ChannelMemoryRecordN(backend, 8) diff --git a/multi_test.go b/multi_test.go index b6ecf5b..54602c5 100644 --- a/multi_test.go +++ b/multi_test.go @@ -14,10 +14,10 @@ func TestMultiLogger(t *testing.T) { log := MustGetLogger("test") log.Debug("log") - if "log" != MemoryRecordN(log1, 0).Formatted(0) { + if "1970-01-01 00:00:00 log" != MemoryRecordN(log1, 0).Formatted(0) { t.Errorf("log1: %v", MemoryRecordN(log1, 0).Formatted(0)) } - if "log" != MemoryRecordN(log2, 0).Formatted(0) { + if "1970-01-01 00:00:00 log" != MemoryRecordN(log2, 0).Formatted(0) { t.Errorf("log2: %v", MemoryRecordN(log2, 0).Formatted(0)) } } @@ -42,7 +42,7 @@ func TestMultiLoggerLevel(t *testing.T) { leveled1.SetLevel(DEBUG, "test") log.Notice("log") - if "log" != MemoryRecordN(log1, 0).Formatted(0) { + if "1970-01-01 00:00:00 log" != MemoryRecordN(log1, 0).Formatted(0) { t.Errorf("log1 not receieved") } if nil != MemoryRecordN(log2, 0) {