From 82f2950078ce6b5708d90b07114300cd793cd8d7 Mon Sep 17 00:00:00 2001 From: Christopher Hein Date: Tue, 27 Nov 2018 23:23:56 +0000 Subject: [PATCH] adding support for turning off timestamps Signed-off-by: Christopher Hein --- logger.go | 23 ++++++++++++++++++----- logger_test.go | 36 ++++++++++++++++++------------------ 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/logger.go b/logger.go index 9e7423e..35fcb91 100644 --- a/logger.go +++ b/logger.go @@ -21,9 +21,8 @@ import ( "strings" "time" - lol "github.com/kris-nova/lolgopher" - "github.com/fatih/color" + lol "github.com/kris-nova/lolgopher" ) type Logger func(format string, a ...interface{}) @@ -44,6 +43,7 @@ var ( FabulousWriter = lol.NewLolWriter() FabulousTrueWriter = lol.NewTruecolorLolWriter() TestMode = false + Timestamps = true ) func Always(format string, a ...interface{}) { @@ -132,7 +132,6 @@ func Debug(format string, a ...interface{}) { fmt.Fprintf(w, s) - } } @@ -165,15 +164,29 @@ func extractLoggerArgs(format string, a ...interface{}) ([]interface{}, io.Write } } - return a, w } func label(format, label string) string { + if Timestamps { + return labelWithTime(format, label) + } else { + return labelWithoutTime(format, label) + } +} + +func labelWithTime(format, label string) string { t := time.Now() rfct := t.Format(time.RFC3339) if !strings.Contains(format, "\n") { format = fmt.Sprintf("%s%s", format, "\n") } return fmt.Sprintf("%s [%s] %s", rfct, label, format) -} \ No newline at end of file +} + +func labelWithoutTime(format, label string) string { + if !strings.Contains(format, "\n") { + format = fmt.Sprintf("%s%s", format, "\n") + } + return fmt.Sprintf("[%s] %s", label, format) +} diff --git a/logger_test.go b/logger_test.go index 7b5c18b..eb4861f 100644 --- a/logger_test.go +++ b/logger_test.go @@ -18,13 +18,13 @@ import ( "bytes" "fmt" "regexp" - "strings" "testing" ) const ( - format = "%v, %v, %v, all eyes on me!" - formatExp = `^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.* \[%s\] \d, \d, \d, all eyes on me!` + format = "%v, %v, %v, all eyes on me!" + formatExp = `^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.* \[%s\] \d, \d, \d, all eyes on me!` + formatWOTimeExp = `\[%s\] \d, \d, \d, all eyes on me!` ) var ( @@ -37,20 +37,6 @@ func TestMain(m *testing.M) { m.Run() } -func ExampleTestLog() { - Log(format, a...) - // Output: 1, 2, 3, all eyes on me! -} - -func TestLog(t *testing.T) { - e := fmt.Sprintf(format, a...) - g := captureLoggerOutput(Log, format, a) - - if strings.Compare(e, g) != 0 { - t.Fatalf("Log should produce '%v' but produces: %v", e, g) - } -} - func TestAlways(t *testing.T) { e, err := regexp.Compile(fmt.Sprintf(formatExp, AlwaysLabel)) g := captureLoggerOutput(Always, format, a) @@ -139,8 +125,22 @@ func TestWarning(t *testing.T) { } } +func TestWithoutTimestamps(t *testing.T) { + Timestamps = false + e, err := regexp.Compile(fmt.Sprintf(formatWOTimeExp, AlwaysLabel)) + g := captureLoggerOutput(Always, format, a) + + if err != nil { + t.Fatalf("Failed to compile regexp '%v': %v", e.String(), err) + } + + if !e.MatchString(g) { + t.Fatalf("Always should produce a pattern '%v' but produces: %v", e.String(), g) + } +} + func captureLoggerOutput(l Logger, format string, a []interface{}) string { b := new(bytes.Buffer) l(format, append(a, b)...) return b.String() -} \ No newline at end of file +}