mirror of
https://github.com/docker/docs.git
synced 2026-11-05 00:14:17 +03:00
Logrus Machine Logger
Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
@@ -244,7 +244,7 @@ func detectShell() (string, error) {
|
||||
if shell == "" {
|
||||
// check for windows env and not bash (i.e. msysgit, etc)
|
||||
if runtime.GOOS == "windows" {
|
||||
log.Infof("On Windows, please specify either 'cmd' or 'powershell' with the --shell flag.\n\n")
|
||||
log.Info("On Windows, please specify either 'cmd' or 'powershell' with the --shell flag.\n\n")
|
||||
}
|
||||
|
||||
return "", ErrUnknownShell
|
||||
|
||||
@@ -83,11 +83,11 @@ func TestExecServer(t *testing.T) {
|
||||
|
||||
logReader, logWriter := io.Pipe()
|
||||
|
||||
log.GetStandardLogger().Out = logWriter
|
||||
log.SetOutput(logWriter)
|
||||
|
||||
defer func() {
|
||||
log.SetDebug(false)
|
||||
log.GetStandardLogger().Out = os.Stderr
|
||||
log.SetOutput(os.Stderr)
|
||||
}()
|
||||
|
||||
stdoutReader, stdoutWriter := io.Pipe()
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package log
|
||||
|
||||
type MachineLogger interface {
|
||||
Debug(...interface{})
|
||||
Debugf(string, ...interface{})
|
||||
|
||||
Info(...interface{})
|
||||
Infof(string, ...interface{})
|
||||
|
||||
Warn(...interface{})
|
||||
Warnf(string, ...interface{})
|
||||
|
||||
Error(...interface{})
|
||||
Errorf(string, ...interface{})
|
||||
|
||||
Fatal(...interface{})
|
||||
Fatalf(string, ...interface{})
|
||||
}
|
||||
@@ -1,20 +1,16 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
import "io"
|
||||
|
||||
var logger *logrus.Logger
|
||||
var logger MachineLogger
|
||||
|
||||
func init() {
|
||||
logger = logrus.New()
|
||||
logger.Level = logrus.InfoLevel
|
||||
logger.Formatter = new(machineFormatter)
|
||||
logger = NewMachineLogger()
|
||||
}
|
||||
|
||||
// RedirectStdOutToStdErr prevents any log from corrupting the output
|
||||
func RedirectStdOutToStdErr() {
|
||||
logger.Level = logrus.ErrorLevel
|
||||
logger.RedirectStdOutToStdErr()
|
||||
}
|
||||
|
||||
func Debug(args ...interface{}) {
|
||||
@@ -57,14 +53,14 @@ func Warnf(fmtString string, args ...interface{}) {
|
||||
logger.Warnf(fmtString, args...)
|
||||
}
|
||||
|
||||
func GetStandardLogger() *logrus.Logger {
|
||||
func Logger() interface{} {
|
||||
return logger
|
||||
}
|
||||
|
||||
func SetDebug(debug bool) {
|
||||
if debug {
|
||||
logger.Level = logrus.DebugLevel
|
||||
} else {
|
||||
logger.Level = logrus.InfoLevel
|
||||
}
|
||||
logger.SetDebug(debug)
|
||||
}
|
||||
|
||||
func SetOutput(out io.Writer) {
|
||||
logger.SetOutput(out)
|
||||
}
|
||||
|
||||
9
libmachine/log/log_test.go
Normal file
9
libmachine/log/log_test.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFormatterIsSet(t *testing.T) {
|
||||
|
||||
}
|
||||
79
libmachine/log/logrus_machine_logger.go
Normal file
79
libmachine/log/logrus_machine_logger.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
type LogrusMachineLogger struct {
|
||||
logger *logrus.Logger
|
||||
}
|
||||
|
||||
func NewMachineLogger() MachineLogger {
|
||||
logrusLogger := logrus.New()
|
||||
logrusLogger.Level = logrus.InfoLevel
|
||||
logrusLogger.Formatter = new(MachineFormatter)
|
||||
return LogrusMachineLogger{logrusLogger}
|
||||
}
|
||||
|
||||
// RedirectStdOutToStdErr prevents any log from corrupting the output
|
||||
func (ml LogrusMachineLogger) RedirectStdOutToStdErr() {
|
||||
ml.logger.Level = logrus.ErrorLevel
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) SetDebug(debug bool) {
|
||||
if debug {
|
||||
ml.logger.Level = logrus.DebugLevel
|
||||
} else {
|
||||
ml.logger.Level = logrus.InfoLevel
|
||||
}
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) SetOutput(out io.Writer) {
|
||||
ml.logger.Out = out
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) Logger() interface{} {
|
||||
return ml.logger
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) Debug(args ...interface{}) {
|
||||
ml.logger.Debug(args...)
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) Debugf(fmtString string, args ...interface{}) {
|
||||
ml.logger.Debugf(fmtString, args...)
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) Error(args ...interface{}) {
|
||||
ml.logger.Error(args...)
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) Errorf(fmtString string, args ...interface{}) {
|
||||
ml.logger.Errorf(fmtString, args...)
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) Info(args ...interface{}) {
|
||||
ml.logger.Info(args...)
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) Infof(fmtString string, args ...interface{}) {
|
||||
ml.logger.Infof(fmtString, args...)
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) Fatal(args ...interface{}) {
|
||||
ml.logger.Fatal(args...)
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) Fatalf(fmtString string, args ...interface{}) {
|
||||
ml.logger.Fatalf(fmtString, args...)
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) Warn(args ...interface{}) {
|
||||
ml.logger.Warn(args...)
|
||||
}
|
||||
|
||||
func (ml LogrusMachineLogger) Warnf(fmtString string, args ...interface{}) {
|
||||
ml.logger.Warnf(fmtString, args...)
|
||||
}
|
||||
77
libmachine/log/logrus_machine_logger_test.go
Normal file
77
libmachine/log/logrus_machine_logger_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"bufio"
|
||||
"io"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDefaultLevelIsInfo(t *testing.T) {
|
||||
testLogger := NewMachineLogger()
|
||||
assert.Equal(t, testLogger.Logger().(*logrus.Logger).Level, logrus.InfoLevel)
|
||||
}
|
||||
|
||||
func TestSetDebugToTrue(t *testing.T) {
|
||||
testLogger := NewMachineLogger()
|
||||
testLogger.SetDebug(true)
|
||||
assert.Equal(t, testLogger.Logger().(*logrus.Logger).Level, logrus.DebugLevel)
|
||||
}
|
||||
|
||||
func TestSetDebugToFalse(t *testing.T) {
|
||||
testLogger := NewMachineLogger()
|
||||
testLogger.SetDebug(true)
|
||||
testLogger.SetDebug(false)
|
||||
assert.Equal(t, testLogger.Logger().(*logrus.Logger).Level, logrus.InfoLevel)
|
||||
}
|
||||
|
||||
func TestSetSilenceOutput(t *testing.T) {
|
||||
testLogger := NewMachineLogger()
|
||||
testLogger.RedirectStdOutToStdErr()
|
||||
assert.Equal(t, testLogger.Logger().(*logrus.Logger).Level, logrus.ErrorLevel)
|
||||
}
|
||||
|
||||
func TestDebug(t *testing.T) {
|
||||
testLogger := NewMachineLogger()
|
||||
testLogger.SetDebug(true)
|
||||
|
||||
result := captureOutput(testLogger, func() { testLogger.Debug("debug") })
|
||||
|
||||
assert.Equal(t, result, "debug")
|
||||
}
|
||||
|
||||
func TestInfo(t *testing.T) {
|
||||
testLogger := NewMachineLogger()
|
||||
|
||||
result := captureOutput(testLogger, func() { testLogger.Info("info") })
|
||||
|
||||
assert.Equal(t, result, "info")
|
||||
}
|
||||
|
||||
func TestWarn(t *testing.T) {
|
||||
testLogger := NewMachineLogger()
|
||||
|
||||
result := captureOutput(testLogger, func() { testLogger.Warn("warn") })
|
||||
|
||||
assert.Equal(t, result, "warn")
|
||||
}
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
testLogger := NewMachineLogger()
|
||||
|
||||
result := captureOutput(testLogger, func() { testLogger.Error("error") })
|
||||
|
||||
assert.Equal(t, result, "error")
|
||||
}
|
||||
|
||||
func captureOutput(testLogger MachineLogger, lambda func()) string {
|
||||
pipeReader, pipeWriter := io.Pipe()
|
||||
scanner := bufio.NewScanner(pipeReader)
|
||||
testLogger.SetOutput(pipeWriter)
|
||||
go lambda()
|
||||
scanner.Scan()
|
||||
return scanner.Text()
|
||||
}
|
||||
@@ -6,10 +6,10 @@ import (
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
type machineFormatter struct {
|
||||
type MachineFormatter struct {
|
||||
}
|
||||
|
||||
func (d *machineFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
func (d *MachineFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
b := &bytes.Buffer{}
|
||||
|
||||
b.WriteString(entry.Message)
|
||||
17
libmachine/log/machine_formatter_test.go
Normal file
17
libmachine/log/machine_formatter_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
entry := logrus.NewEntry(logrus.New())
|
||||
entry.Message = "foobar"
|
||||
formatter := MachineFormatter{}
|
||||
bytes, err := formatter.Format(entry)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, string(bytes[:]), "foobar\n")
|
||||
}
|
||||
28
libmachine/log/machine_logger.go
Normal file
28
libmachine/log/machine_logger.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package log
|
||||
|
||||
import "io"
|
||||
|
||||
type MachineLogger interface {
|
||||
RedirectStdOutToStdErr()
|
||||
|
||||
SetDebug(debug bool)
|
||||
|
||||
SetOutput(io.Writer)
|
||||
|
||||
Debug(args ...interface{})
|
||||
Debugf(fmtString string, args ...interface{})
|
||||
|
||||
Error(args ...interface{})
|
||||
Errorf(fmtString string, args ...interface{})
|
||||
|
||||
Info(args ...interface{})
|
||||
Infof(fmtString string, args ...interface{})
|
||||
|
||||
Fatal(args ...interface{})
|
||||
Fatalf(fmtString string, args ...interface{})
|
||||
|
||||
Warn(args ...interface{})
|
||||
Warnf(fmtString string, args ...interface{})
|
||||
|
||||
Logger() interface{}
|
||||
}
|
||||
Reference in New Issue
Block a user