From 2e269bb6e74986a883659b8a50cf1b7b78b5c497 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 30 Nov 2018 18:50:08 -0500 Subject: [PATCH] mlog: refactor to use a Stringer for Message.Description --- mlog/mlog.go | 57 +++++++++++++++++++++++++++++++---------------- mlog/mlog_test.go | 26 ++++++++++----------- 2 files changed, 51 insertions(+), 32 deletions(-) diff --git a/mlog/mlog.go b/mlog/mlog.go index 79c78c5..311c6a8 100644 --- a/mlog/mlog.go +++ b/mlog/mlog.go @@ -169,12 +169,24 @@ 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 // KVer type Message struct { Level - Msg string - KV KV + Description Stringer + KV KV } func stringSlice(kv KV) [][2]string { @@ -206,7 +218,7 @@ func DefaultFormat(w io.Writer, msg Message) error { _, 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 { write(" --") 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 // 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. -func (l *Logger) Log(lvl Level, msgStr string, kvs ...KVer) { - if l.maxLevel < lvl.Uint() { +func (l *Logger) Log(msg Message) { + if l.maxLevel < msg.Level.Uint() { return } - m := Message{Level: lvl, Msg: msgStr, KV: Merge(kvs...).KV()} - if err := l.h(m); err != nil { - go l.Log(ErrorLevel, "Logger.Handler returned error", ErrKV(err)) + if err := l.h(msg); err != nil { + go l.Error("Logger.Handler returned error", ErrKV(err)) return } @@ -307,33 +318,41 @@ func (l *Logger) Log(lvl Level, msgStr string, kvs ...KVer) { l.testMsgWrittenCh <- struct{}{} } - if m.Level.Uint() == 0 { + if msg.Level.Uint() == 0 { 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 -func (l *Logger) Debug(msg string, kvs ...KVer) { - l.Log(DebugLevel, msg, kvs...) +func (l *Logger) Debug(descr string, kvs ...KVer) { + l.Log(mkMsg(DebugLevel, descr, kvs...)) } // Info logs a InfoLevel message, merging the KVers together first -func (l *Logger) Info(msg string, kvs ...KVer) { - l.Log(InfoLevel, msg, kvs...) +func (l *Logger) Info(descr string, kvs ...KVer) { + l.Log(mkMsg(InfoLevel, descr, kvs...)) } // Warn logs a WarnLevel message, merging the KVers together first -func (l *Logger) Warn(msg string, kvs ...KVer) { - l.Log(WarnLevel, msg, kvs...) +func (l *Logger) Warn(descr string, kvs ...KVer) { + l.Log(mkMsg(WarnLevel, descr, kvs...)) } // Error logs a ErrorLevel message, merging the KVers together first -func (l *Logger) Error(msg string, kvs ...KVer) { - l.Log(ErrorLevel, msg, kvs...) +func (l *Logger) Error(descr string, kvs ...KVer) { + l.Log(mkMsg(ErrorLevel, descr, kvs...)) } // Fatal logs a FatalLevel message, merging the KVers together first. A Fatal // message automatically stops the process with an os.Exit(1) -func (l *Logger) Fatal(msg string, kvs ...KVer) { - l.Log(FatalLevel, msg, kvs...) +func (l *Logger) Fatal(descr string, kvs ...KVer) { + l.Log(mkMsg(FatalLevel, descr, kvs...)) } diff --git a/mlog/mlog_test.go b/mlog/mlog_test.go index 071fb05..5456f7a 100644 --- a/mlog/mlog_test.go +++ b/mlog/mlog_test.go @@ -68,10 +68,10 @@ func TestLogger(t *T) { } // Default max level should be INFO - l.Log(DebugLevel, "foo") - l.Log(InfoLevel, "bar") - l.Log(WarnLevel, "baz") - l.Log(ErrorLevel, "buz") + l.Debug("foo") + l.Info("bar") + l.Warn("baz") + l.Error("buz") massert.Fatal(t, massert.All( assertOut("~ INFO -- bar\n"), assertOut("~ WARN -- baz\n"), @@ -79,10 +79,10 @@ func TestLogger(t *T) { )) l = l.WithMaxLevel(WarnLevel) - l.Log(DebugLevel, "foo") - l.Log(InfoLevel, "bar") - l.Log(WarnLevel, "baz") - l.Log(ErrorLevel, "buz", KV{"a": "b"}) + l.Debug("foo") + l.Info("bar") + l.Warn("baz") + l.Error("buz", KV{"a": "b"}) massert.Fatal(t, massert.All( assertOut("~ WARN -- baz\n"), assertOut("~ ERROR -- buz -- a=\"b\"\n"), @@ -90,12 +90,12 @@ func TestLogger(t *T) { l2 := l.WithMaxLevel(InfoLevel) l2 = l2.WithHandler(func(msg Message) error { - msg.Msg = strings.ToUpper(msg.Msg) + msg.Description = String(strings.ToUpper(msg.Description.String())) return h(msg) }) - l2.Log(InfoLevel, "bar") - l2.Log(WarnLevel, "baz") - l.Log(ErrorLevel, "buz") + l2.Info("bar") + l2.Warn("baz") + l.Error("buz") massert.Fatal(t, massert.All( assertOut("~ INFO -- BAR\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)) msg.KV = KV{}.KV()