Merge pull request #2 from christopherhein/feature/timestamps-flag

adding support for turning off timestamps
rc-0.2.0
Kris Nova 2018-11-27 15:58:38 -08:00 committed by GitHub
commit fd0d87064b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 23 deletions

View File

@ -21,9 +21,8 @@ import (
"strings" "strings"
"time" "time"
lol "github.com/kris-nova/lolgopher"
"github.com/fatih/color" "github.com/fatih/color"
lol "github.com/kris-nova/lolgopher"
) )
type Logger func(format string, a ...interface{}) type Logger func(format string, a ...interface{})
@ -44,6 +43,7 @@ var (
FabulousWriter = lol.NewLolWriter() FabulousWriter = lol.NewLolWriter()
FabulousTrueWriter = lol.NewTruecolorLolWriter() FabulousTrueWriter = lol.NewTruecolorLolWriter()
TestMode = false TestMode = false
Timestamps = true
) )
func Always(format string, a ...interface{}) { func Always(format string, a ...interface{}) {
@ -132,7 +132,6 @@ func Debug(format string, a ...interface{}) {
fmt.Fprintf(w, s) fmt.Fprintf(w, s)
} }
} }
@ -165,15 +164,29 @@ func extractLoggerArgs(format string, a ...interface{}) ([]interface{}, io.Write
} }
} }
return a, w return a, w
} }
func label(format, label string) string { 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() t := time.Now()
rfct := t.Format(time.RFC3339) rfct := t.Format(time.RFC3339)
if !strings.Contains(format, "\n") { if !strings.Contains(format, "\n") {
format = fmt.Sprintf("%s%s", format, "\n") format = fmt.Sprintf("%s%s", format, "\n")
} }
return fmt.Sprintf("%s [%s] %s", rfct, label, format) return fmt.Sprintf("%s [%s] %s", rfct, label, format)
} }
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)
}

View File

@ -18,13 +18,13 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"regexp" "regexp"
"strings"
"testing" "testing"
) )
const ( const (
format = "%v, %v, %v, 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!` 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 ( var (
@ -37,20 +37,6 @@ func TestMain(m *testing.M) {
m.Run() 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) { func TestAlways(t *testing.T) {
e, err := regexp.Compile(fmt.Sprintf(formatExp, AlwaysLabel)) e, err := regexp.Compile(fmt.Sprintf(formatExp, AlwaysLabel))
g := captureLoggerOutput(Always, format, a) 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 { func captureLoggerOutput(l Logger, format string, a []interface{}) string {
b := new(bytes.Buffer) b := new(bytes.Buffer)
l(format, append(a, b)...) l(format, append(a, b)...)
return b.String() return b.String()
} }