Remove TerminalLogger.

Signed-off-by: Kent Wang <pragkent@gmail.com>
This commit is contained in:
Kent Wang 2015-10-13 11:29:50 +08:00
parent 6f3648735c
commit da26cab450
2 changed files with 6 additions and 127 deletions

View File

@ -2,18 +2,18 @@ package log
import "testing"
func TestTerminalLoggerWithFields(t *testing.T) {
logger := TerminalLogger{}
func TestStandardLoggerWithFields(t *testing.T) {
logger := StandardLogger{}
withFieldsLogger := logger.WithFields(Fields{
"foo": "bar",
"spam": "eggs",
})
withFieldsTerminalLogger, ok := withFieldsLogger.(TerminalLogger)
withFieldsStandardLogger, ok := withFieldsLogger.(StandardLogger)
if !ok {
t.Fatal("Type assertion to TerminalLogger failed")
t.Fatal("Type assertion to StandardLogger failed")
}
expectedOutFields := "\t\t foo=bar spam=eggs"
if withFieldsTerminalLogger.fieldOut != expectedOutFields {
t.Fatalf("Expected %q, got %q", expectedOutFields, withFieldsTerminalLogger.fieldOut)
if withFieldsStandardLogger.fieldOut != expectedOutFields {
t.Fatalf("Expected %q, got %q", expectedOutFields, withFieldsStandardLogger.fieldOut)
}
}

View File

@ -1,121 +0,0 @@
package log
import (
"fmt"
"os"
"sort"
)
type TerminalLogger struct {
// fieldOut is used to do log.WithFields correctly
fieldOut string
}
func (t TerminalLogger) log(args ...interface{}) {
fmt.Print(args...)
fmt.Print(t.fieldOut, "\n")
t.fieldOut = ""
}
func (t TerminalLogger) logf(fmtString string, args ...interface{}) {
fmt.Printf(fmtString, args...)
fmt.Print(t.fieldOut, "\n")
t.fieldOut = ""
}
func (t TerminalLogger) err(args ...interface{}) {
fmt.Fprint(os.Stderr, args...)
fmt.Fprint(os.Stderr, t.fieldOut, "\n")
t.fieldOut = ""
}
func (t TerminalLogger) errf(fmtString string, args ...interface{}) {
fmt.Fprintf(os.Stderr, fmtString, args...)
fmt.Fprint(os.Stderr, t.fieldOut, "\n")
t.fieldOut = ""
}
func (t TerminalLogger) Debug(args ...interface{}) {
if IsDebug {
t.log(args...)
}
}
func (t TerminalLogger) Debugf(fmtString string, args ...interface{}) {
if IsDebug {
t.logf(fmtString, args...)
}
}
func (t TerminalLogger) Error(args ...interface{}) {
t.err(args...)
}
func (t TerminalLogger) Errorf(fmtString string, args ...interface{}) {
t.errf(fmtString, args...)
}
func (t TerminalLogger) Info(args ...interface{}) {
t.log(args...)
}
func (t TerminalLogger) Infof(fmtString string, args ...interface{}) {
t.logf(fmtString, args...)
}
func (t TerminalLogger) Fatal(args ...interface{}) {
t.err(args...)
os.Exit(1)
}
func (t TerminalLogger) Fatalf(fmtString string, args ...interface{}) {
t.errf(fmtString, args...)
os.Exit(1)
}
func (t TerminalLogger) Print(args ...interface{}) {
t.log(args...)
}
func (t TerminalLogger) Printf(fmtString string, args ...interface{}) {
t.logf(fmtString, args...)
}
func (t TerminalLogger) Warn(args ...interface{}) {
t.log(args...)
}
func (t TerminalLogger) Warnf(fmtString string, args ...interface{}) {
t.logf(fmtString, args...)
}
func (t TerminalLogger) WithFields(fields Fields) Logger {
// When the user calls WithFields, we make a string which gets appended
// to the output of the final [Info|Warn|Error] call for the
// descriptive fields. Because WithFields returns the proper Logger
// (with the fieldOut string set correctly), the logrus syntax will
// still work.
kvpairs := []string{}
// Why the string slice song and dance? Because Go's map iteration
// order is random, we will get inconsistent results if we don't sort
// the fields (or their resulting string K/V pairs, like we have here).
// Otherwise, we couldn't test this reliably.
for k, v := range fields {
kvpairs = append(kvpairs, fmt.Sprintf("%s=%v", k, v))
}
sort.Strings(kvpairs)
// TODO:
// 1. Is this thread-safe?
// 2. Add more tabs?
t.fieldOut = "\t\t"
for _, s := range kvpairs {
// TODO: Is %v the correct format string here?
t.fieldOut = fmt.Sprintf("%s %s", t.fieldOut, s)
}
return t
}