adding support for turning off timestamps
Signed-off-by: Christopher Hein <me@christopherhein.com>rc-0.2.0
parent
2e4c213f0a
commit
82f2950078
21
logger.go
21
logger.go
|
@ -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,11 +164,18 @@ 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") {
|
||||||
|
@ -177,3 +183,10 @@ func label(format, label string) string {
|
||||||
}
|
}
|
||||||
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)
|
||||||
|
}
|
||||||
|
|
|
@ -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,6 +125,20 @@ 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)...)
|
||||||
|
|
Loading…
Reference in New Issue