mirror of https://github.com/docker/docs.git
Merge pull request #1965 from pragkent/master
Fix filenames of loggers.
This commit is contained in:
commit
0ce345a8b5
|
@ -2,18 +2,18 @@ package log
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestTerminalLoggerWithFields(t *testing.T) {
|
func TestStandardLoggerWithFields(t *testing.T) {
|
||||||
logger := TerminalLogger{}
|
logger := StandardLogger{}
|
||||||
withFieldsLogger := logger.WithFields(Fields{
|
withFieldsLogger := logger.WithFields(Fields{
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
"spam": "eggs",
|
"spam": "eggs",
|
||||||
})
|
})
|
||||||
withFieldsTerminalLogger, ok := withFieldsLogger.(TerminalLogger)
|
withFieldsStandardLogger, ok := withFieldsLogger.(StandardLogger)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("Type assertion to TerminalLogger failed")
|
t.Fatal("Type assertion to StandardLogger failed")
|
||||||
}
|
}
|
||||||
expectedOutFields := "\t\t foo=bar spam=eggs"
|
expectedOutFields := "\t\t foo=bar spam=eggs"
|
||||||
if withFieldsTerminalLogger.fieldOut != expectedOutFields {
|
if withFieldsStandardLogger.fieldOut != expectedOutFields {
|
||||||
t.Fatalf("Expected %q, got %q", expectedOutFields, withFieldsTerminalLogger.fieldOut)
|
t.Fatalf("Expected %q, got %q", expectedOutFields, withFieldsStandardLogger.fieldOut)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,94 +2,106 @@ package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TerminalLogger struct {
|
type StandardLogger struct {
|
||||||
// fieldOut is used to do log.WithFields correctly
|
// fieldOut is used to do log.WithFields correctly
|
||||||
fieldOut string
|
fieldOut string
|
||||||
|
OutWriter io.Writer
|
||||||
|
ErrWriter io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) log(args ...interface{}) {
|
func (t StandardLogger) log(args ...interface{}) {
|
||||||
fmt.Print(args...)
|
fmt.Fprint(t.OutWriter, args...)
|
||||||
fmt.Print(t.fieldOut, "\n")
|
fmt.Fprint(t.OutWriter, t.fieldOut, "\n")
|
||||||
t.fieldOut = ""
|
t.fieldOut = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) logf(fmtString string, args ...interface{}) {
|
func (t StandardLogger) logf(fmtString string, args ...interface{}) {
|
||||||
fmt.Printf(fmtString, args...)
|
fmt.Fprintf(t.OutWriter, fmtString, args...)
|
||||||
fmt.Print(t.fieldOut, "\n")
|
fmt.Fprint(t.OutWriter, "\n")
|
||||||
t.fieldOut = ""
|
t.fieldOut = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) err(args ...interface{}) {
|
func (t StandardLogger) err(args ...interface{}) {
|
||||||
fmt.Fprint(os.Stderr, args...)
|
fmt.Fprint(t.ErrWriter, args...)
|
||||||
fmt.Fprint(os.Stderr, t.fieldOut, "\n")
|
fmt.Fprint(t.ErrWriter, t.fieldOut, "\n")
|
||||||
t.fieldOut = ""
|
t.fieldOut = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) errf(fmtString string, args ...interface{}) {
|
func (t StandardLogger) errf(fmtString string, args ...interface{}) {
|
||||||
fmt.Fprintf(os.Stderr, fmtString, args...)
|
fmt.Fprintf(t.ErrWriter, fmtString, args...)
|
||||||
fmt.Fprint(os.Stderr, t.fieldOut, "\n")
|
fmt.Fprint(t.ErrWriter, t.fieldOut, "\n")
|
||||||
t.fieldOut = ""
|
t.fieldOut = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Debug(args ...interface{}) {
|
func (t StandardLogger) Debug(args ...interface{}) {
|
||||||
if IsDebug {
|
if IsDebug {
|
||||||
t.log(args...)
|
t.err(args...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Debugf(fmtString string, args ...interface{}) {
|
func (t StandardLogger) Debugf(fmtString string, args ...interface{}) {
|
||||||
if IsDebug {
|
if IsDebug {
|
||||||
t.logf(fmtString, args...)
|
t.errf(fmtString, args...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Error(args ...interface{}) {
|
func (t StandardLogger) Error(args ...interface{}) {
|
||||||
t.err(args...)
|
t.err(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Errorf(fmtString string, args ...interface{}) {
|
func (t StandardLogger) Errorf(fmtString string, args ...interface{}) {
|
||||||
t.errf(fmtString, args...)
|
t.errf(fmtString, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Info(args ...interface{}) {
|
func (t StandardLogger) Errorln(args ...interface{}) {
|
||||||
|
|
||||||
|
t.err(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t StandardLogger) Info(args ...interface{}) {
|
||||||
t.log(args...)
|
t.log(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Infof(fmtString string, args ...interface{}) {
|
func (t StandardLogger) Infof(fmtString string, args ...interface{}) {
|
||||||
t.logf(fmtString, args...)
|
t.logf(fmtString, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Fatal(args ...interface{}) {
|
func (t StandardLogger) Infoln(args ...interface{}) {
|
||||||
|
t.log(args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t StandardLogger) Fatal(args ...interface{}) {
|
||||||
t.err(args...)
|
t.err(args...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Fatalf(fmtString string, args ...interface{}) {
|
func (t StandardLogger) Fatalf(fmtString string, args ...interface{}) {
|
||||||
t.errf(fmtString, args...)
|
t.errf(fmtString, args...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Print(args ...interface{}) {
|
func (t StandardLogger) Print(args ...interface{}) {
|
||||||
t.log(args...)
|
t.log(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Printf(fmtString string, args ...interface{}) {
|
func (t StandardLogger) Printf(fmtString string, args ...interface{}) {
|
||||||
t.logf(fmtString, args...)
|
t.logf(fmtString, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Warn(args ...interface{}) {
|
func (t StandardLogger) Warn(args ...interface{}) {
|
||||||
t.log(args...)
|
t.log(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) Warnf(fmtString string, args ...interface{}) {
|
func (t StandardLogger) Warnf(fmtString string, args ...interface{}) {
|
||||||
t.logf(fmtString, args...)
|
t.logf(fmtString, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t TerminalLogger) WithFields(fields Fields) Logger {
|
func (t StandardLogger) WithFields(fields Fields) Logger {
|
||||||
// When the user calls WithFields, we make a string which gets appended
|
// When the user calls WithFields, we make a string which gets appended
|
||||||
// to the output of the final [Info|Warn|Error] call for the
|
// to the output of the final [Info|Warn|Error] call for the
|
||||||
// descriptive fields. Because WithFields returns the proper Logger
|
// descriptive fields. Because WithFields returns the proper Logger
|
||||||
|
|
|
@ -1,133 +0,0 @@
|
||||||
package log
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"sort"
|
|
||||||
)
|
|
||||||
|
|
||||||
type StandardLogger struct {
|
|
||||||
// fieldOut is used to do log.WithFields correctly
|
|
||||||
fieldOut string
|
|
||||||
OutWriter io.Writer
|
|
||||||
ErrWriter io.Writer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) log(args ...interface{}) {
|
|
||||||
fmt.Fprint(t.OutWriter, args...)
|
|
||||||
fmt.Fprint(t.OutWriter, t.fieldOut, "\n")
|
|
||||||
t.fieldOut = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) logf(fmtString string, args ...interface{}) {
|
|
||||||
fmt.Fprintf(t.OutWriter, fmtString, args...)
|
|
||||||
fmt.Fprint(t.OutWriter, "\n")
|
|
||||||
t.fieldOut = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) err(args ...interface{}) {
|
|
||||||
fmt.Fprint(t.ErrWriter, args...)
|
|
||||||
fmt.Fprint(t.ErrWriter, t.fieldOut, "\n")
|
|
||||||
t.fieldOut = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) errf(fmtString string, args ...interface{}) {
|
|
||||||
fmt.Fprintf(t.ErrWriter, fmtString, args...)
|
|
||||||
fmt.Fprint(t.ErrWriter, t.fieldOut, "\n")
|
|
||||||
t.fieldOut = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Debug(args ...interface{}) {
|
|
||||||
if IsDebug {
|
|
||||||
t.err(args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Debugf(fmtString string, args ...interface{}) {
|
|
||||||
if IsDebug {
|
|
||||||
t.errf(fmtString, args...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Error(args ...interface{}) {
|
|
||||||
t.err(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Errorf(fmtString string, args ...interface{}) {
|
|
||||||
t.errf(fmtString, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Errorln(args ...interface{}) {
|
|
||||||
|
|
||||||
t.err(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Info(args ...interface{}) {
|
|
||||||
t.log(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Infof(fmtString string, args ...interface{}) {
|
|
||||||
t.logf(fmtString, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Infoln(args ...interface{}) {
|
|
||||||
t.log(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Fatal(args ...interface{}) {
|
|
||||||
t.err(args...)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Fatalf(fmtString string, args ...interface{}) {
|
|
||||||
t.errf(fmtString, args...)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Print(args ...interface{}) {
|
|
||||||
t.log(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Printf(fmtString string, args ...interface{}) {
|
|
||||||
t.logf(fmtString, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Warn(args ...interface{}) {
|
|
||||||
t.log(args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) Warnf(fmtString string, args ...interface{}) {
|
|
||||||
t.logf(fmtString, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t StandardLogger) 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
|
|
||||||
}
|
|
Loading…
Reference in New Issue