mlog: refactor to use a Stringer for Message.Description

This commit is contained in:
Brian Picciano 2018-11-30 18:50:08 -05:00
parent b5c676b5c8
commit 2e269bb6e7
2 changed files with 51 additions and 32 deletions

View File

@ -169,11 +169,23 @@ func Prefix(kv KVer, prefix string) KVer {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Stringer generates and returns a string.
type Stringer interface {
String() string
}
// String is simply a string which implements Stringer.
type String string
func (str String) String() string {
return string(str)
}
// Message describes a message to be logged, after having already resolved the // Message describes a message to be logged, after having already resolved the
// KVer // KVer
type Message struct { type Message struct {
Level Level
Msg string Description Stringer
KV KV KV KV
} }
@ -206,7 +218,7 @@ func DefaultFormat(w io.Writer, msg Message) error {
_, err = fmt.Fprintf(w, s, args...) _, err = fmt.Fprintf(w, s, args...)
} }
} }
write("~ %s -- %s", msg.Level.String(), msg.Msg) write("~ %s -- %s", msg.Level.String(), msg.Description.String())
if len(msg.KV) > 0 { if len(msg.KV) > 0 {
write(" --") write(" --")
for _, kve := range stringSlice(msg.KV) { for _, kve := range stringSlice(msg.KV) {
@ -292,14 +304,13 @@ func (l *Logger) WithHandler(h Handler) *Logger {
// Log can be used to manually log a message of some custom defined Level. kvs // Log can be used to manually log a message of some custom defined Level. kvs
// will be Merge'd automatically. If the Level is a fatal (Uint() == 0) then // will be Merge'd automatically. If the Level is a fatal (Uint() == 0) then
// calling this will never return, and the process will have os.Exit(1) called. // calling this will never return, and the process will have os.Exit(1) called.
func (l *Logger) Log(lvl Level, msgStr string, kvs ...KVer) { func (l *Logger) Log(msg Message) {
if l.maxLevel < lvl.Uint() { if l.maxLevel < msg.Level.Uint() {
return return
} }
m := Message{Level: lvl, Msg: msgStr, KV: Merge(kvs...).KV()} if err := l.h(msg); err != nil {
if err := l.h(m); err != nil { go l.Error("Logger.Handler returned error", ErrKV(err))
go l.Log(ErrorLevel, "Logger.Handler returned error", ErrKV(err))
return return
} }
@ -307,33 +318,41 @@ func (l *Logger) Log(lvl Level, msgStr string, kvs ...KVer) {
l.testMsgWrittenCh <- struct{}{} l.testMsgWrittenCh <- struct{}{}
} }
if m.Level.Uint() == 0 { if msg.Level.Uint() == 0 {
os.Exit(1) os.Exit(1)
} }
} }
func mkMsg(lvl Level, descr string, kvs ...KVer) Message {
return Message{
Level: lvl,
Description: String(descr),
KV: Merge(kvs...).KV(),
}
}
// Debug logs a DebugLevel message, merging the KVers together first // Debug logs a DebugLevel message, merging the KVers together first
func (l *Logger) Debug(msg string, kvs ...KVer) { func (l *Logger) Debug(descr string, kvs ...KVer) {
l.Log(DebugLevel, msg, kvs...) l.Log(mkMsg(DebugLevel, descr, kvs...))
} }
// Info logs a InfoLevel message, merging the KVers together first // Info logs a InfoLevel message, merging the KVers together first
func (l *Logger) Info(msg string, kvs ...KVer) { func (l *Logger) Info(descr string, kvs ...KVer) {
l.Log(InfoLevel, msg, kvs...) l.Log(mkMsg(InfoLevel, descr, kvs...))
} }
// Warn logs a WarnLevel message, merging the KVers together first // Warn logs a WarnLevel message, merging the KVers together first
func (l *Logger) Warn(msg string, kvs ...KVer) { func (l *Logger) Warn(descr string, kvs ...KVer) {
l.Log(WarnLevel, msg, kvs...) l.Log(mkMsg(WarnLevel, descr, kvs...))
} }
// Error logs a ErrorLevel message, merging the KVers together first // Error logs a ErrorLevel message, merging the KVers together first
func (l *Logger) Error(msg string, kvs ...KVer) { func (l *Logger) Error(descr string, kvs ...KVer) {
l.Log(ErrorLevel, msg, kvs...) l.Log(mkMsg(ErrorLevel, descr, kvs...))
} }
// Fatal logs a FatalLevel message, merging the KVers together first. A Fatal // Fatal logs a FatalLevel message, merging the KVers together first. A Fatal
// message automatically stops the process with an os.Exit(1) // message automatically stops the process with an os.Exit(1)
func (l *Logger) Fatal(msg string, kvs ...KVer) { func (l *Logger) Fatal(descr string, kvs ...KVer) {
l.Log(FatalLevel, msg, kvs...) l.Log(mkMsg(FatalLevel, descr, kvs...))
} }

View File

@ -68,10 +68,10 @@ func TestLogger(t *T) {
} }
// Default max level should be INFO // Default max level should be INFO
l.Log(DebugLevel, "foo") l.Debug("foo")
l.Log(InfoLevel, "bar") l.Info("bar")
l.Log(WarnLevel, "baz") l.Warn("baz")
l.Log(ErrorLevel, "buz") l.Error("buz")
massert.Fatal(t, massert.All( massert.Fatal(t, massert.All(
assertOut("~ INFO -- bar\n"), assertOut("~ INFO -- bar\n"),
assertOut("~ WARN -- baz\n"), assertOut("~ WARN -- baz\n"),
@ -79,10 +79,10 @@ func TestLogger(t *T) {
)) ))
l = l.WithMaxLevel(WarnLevel) l = l.WithMaxLevel(WarnLevel)
l.Log(DebugLevel, "foo") l.Debug("foo")
l.Log(InfoLevel, "bar") l.Info("bar")
l.Log(WarnLevel, "baz") l.Warn("baz")
l.Log(ErrorLevel, "buz", KV{"a": "b"}) l.Error("buz", KV{"a": "b"})
massert.Fatal(t, massert.All( massert.Fatal(t, massert.All(
assertOut("~ WARN -- baz\n"), assertOut("~ WARN -- baz\n"),
assertOut("~ ERROR -- buz -- a=\"b\"\n"), assertOut("~ ERROR -- buz -- a=\"b\"\n"),
@ -90,12 +90,12 @@ func TestLogger(t *T) {
l2 := l.WithMaxLevel(InfoLevel) l2 := l.WithMaxLevel(InfoLevel)
l2 = l2.WithHandler(func(msg Message) error { l2 = l2.WithHandler(func(msg Message) error {
msg.Msg = strings.ToUpper(msg.Msg) msg.Description = String(strings.ToUpper(msg.Description.String()))
return h(msg) return h(msg)
}) })
l2.Log(InfoLevel, "bar") l2.Info("bar")
l2.Log(WarnLevel, "baz") l2.Warn("baz")
l.Log(ErrorLevel, "buz") l.Error("buz")
massert.Fatal(t, massert.All( massert.Fatal(t, massert.All(
assertOut("~ INFO -- BAR\n"), assertOut("~ INFO -- BAR\n"),
assertOut("~ WARN -- BAZ\n"), assertOut("~ WARN -- BAZ\n"),
@ -119,7 +119,7 @@ func TestDefaultFormat(t *T) {
) )
} }
msg := Message{Level: InfoLevel, Msg: "this is a test"} msg := Message{Level: InfoLevel, Description: String("this is a test")}
massert.Fatal(t, assertFormat("INFO -- this is a test", msg)) massert.Fatal(t, assertFormat("INFO -- this is a test", msg))
msg.KV = KV{}.KV() msg.KV = KV{}.KV()