diff --git a/commands/config.go b/commands/config.go index 0b01f418b0..0303136b69 100644 --- a/commands/config.go +++ b/commands/config.go @@ -2,7 +2,6 @@ package commands import ( "fmt" - "os" "github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine/check" @@ -12,7 +11,7 @@ import ( func cmdConfig(c CommandLine, api libmachine.API) error { // Ensure that log messages always go to stderr when this command is // being run (it is intended to be run in a subshell) - log.SetOutWriter(os.Stderr) + log.RedirectStdOutToStdErr() if len(c.Args()) != 1 { return ErrExpectedOneMachine diff --git a/commands/env.go b/commands/env.go index 23bd402fd1..bd5e79a8cc 100644 --- a/commands/env.go +++ b/commands/env.go @@ -50,7 +50,7 @@ func cmdEnv(c CommandLine, api libmachine.API) error { // Ensure that log messages always go to stderr when this command is // being run (it is intended to be run in a subshell) - log.SetOutWriter(os.Stderr) + log.RedirectStdOutToStdErr() if c.Bool("unset") { shellCfg, err = shellCfgUnset(c, api) diff --git a/libmachine/drivers/plugin/localbinary/plugin_test.go b/libmachine/drivers/plugin/localbinary/plugin_test.go index 2ba47b850e..2c7f194532 100644 --- a/libmachine/drivers/plugin/localbinary/plugin_test.go +++ b/libmachine/drivers/plugin/localbinary/plugin_test.go @@ -82,13 +82,13 @@ func TestExecServer(t *testing.T) { logReader, logWriter := io.Pipe() - log.SetOutWriter(logWriter) - log.SetErrWriter(logWriter) + log.GetStandardLogger().OutWriter = logWriter + log.GetStandardLogger().ErrWriter = logWriter defer func() { log.IsDebug = false - log.SetOutWriter(os.Stdout) - log.SetErrWriter(os.Stderr) + log.GetStandardLogger().OutWriter = os.Stdout + log.GetStandardLogger().ErrWriter = os.Stderr }() stdoutReader, stdoutWriter := io.Pipe() diff --git a/libmachine/examples/vbox_create.go b/libmachine/examples/vbox_create.go index a2c4e878df..caad8a1e2c 100644 --- a/libmachine/examples/vbox_create.go +++ b/libmachine/examples/vbox_create.go @@ -4,17 +4,17 @@ package main import ( "encoding/json" "fmt" - "os" "github.com/docker/machine/drivers/virtualbox" "github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine/log" + "os" ) func main() { log.IsDebug = true - log.SetOutWriter(os.Stdout) - log.SetErrWriter(os.Stderr) + log.GetStandardLogger().OutWriter = os.Stdout + log.GetStandardLogger().ErrWriter = os.Stderr client := libmachine.NewClient("/tmp/automatic") diff --git a/libmachine/log/log.go b/libmachine/log/log.go index d2897ea7c2..9f1d13303f 100644 --- a/libmachine/log/log.go +++ b/libmachine/log/log.go @@ -1,33 +1,24 @@ package log import ( - "io" "os" "sync" ) var ( l = StandardLogger{ - mu: &sync.Mutex{}, + OutWriter: os.Stdout, + ErrWriter: os.Stderr, + mu: &sync.Mutex{}, } IsDebug = false ) type Fields map[string]interface{} -func init() { - // TODO: Is this really the best approach? I worry that it will create - // implicit behavior which may be problmatic for users of the lib. - SetOutWriter(os.Stdout) - SetErrWriter(os.Stderr) -} - -func SetOutWriter(w io.Writer) { - l.OutWriter = w -} - -func SetErrWriter(w io.Writer) { - l.ErrWriter = w +// RedirectStdOutToStdErr prevents any log from corrupting the output +func RedirectStdOutToStdErr() { + l.OutWriter = l.ErrWriter } func Debug(args ...interface{}) { @@ -54,7 +45,6 @@ func Infof(fmtString string, args ...interface{}) { l.Infof(fmtString, args...) } - func Fatal(args ...interface{}) { l.Fatal(args...) } @@ -79,4 +69,6 @@ func WithField(fieldName string, field interface{}) Logger { func WithFields(fields Fields) Logger { return l.WithFields(fields) +func GetStandardLogger() *StandardLogger { + return &l }