From 010a1c76e203fee1d90e1d3112d5c1706e0061fd Mon Sep 17 00:00:00 2001 From: Jean-Laurent de Morlhon Date: Fri, 4 Dec 2015 12:19:04 +0100 Subject: [PATCH] Use logrus Signed-off-by: Jean-Laurent de Morlhon --- cmd/machine.go | 4 +- .../drivers/plugin/localbinary/plugin_test.go | 13 ++- libmachine/drivers/plugin/register_driver.go | 2 +- libmachine/examples/vbox_create.go | 5 +- libmachine/log/interface.go | 2 +- libmachine/log/log.go | 52 +++++----- libmachine/log/machineFormatter.go | 19 ++++ libmachine/log/standard.go | 98 ------------------- 8 files changed, 59 insertions(+), 136 deletions(-) create mode 100644 libmachine/log/machineFormatter.go delete mode 100644 libmachine/log/standard.go diff --git a/cmd/machine.go b/cmd/machine.go index 9c55a60445..c330d5d10c 100644 --- a/cmd/machine.go +++ b/cmd/machine.go @@ -66,7 +66,7 @@ func setDebugOutputLevel() { // use -v / --verbose TBQH for _, f := range os.Args { if f == "-D" || f == "--debug" || f == "-debug" { - log.IsDebug = true + log.SetDebug(true) } } @@ -77,7 +77,7 @@ func setDebugOutputLevel() { fmt.Fprintf(os.Stderr, "Error parsing boolean value from MACHINE_DEBUG: %s\n", err) os.Exit(1) } - log.IsDebug = showDebug + log.SetDebug(showDebug) } } diff --git a/libmachine/drivers/plugin/localbinary/plugin_test.go b/libmachine/drivers/plugin/localbinary/plugin_test.go index 2c7f194532..6a838a1287 100644 --- a/libmachine/drivers/plugin/localbinary/plugin_test.go +++ b/libmachine/drivers/plugin/localbinary/plugin_test.go @@ -4,10 +4,11 @@ import ( "bufio" "fmt" "io" - "os" "testing" "time" + "os" + "github.com/docker/machine/libmachine/log" ) @@ -77,18 +78,16 @@ func TestLocalBinaryPluginClose(t *testing.T) { } func TestExecServer(t *testing.T) { - log.IsDebug = true + log.SetDebug(true) machineName := "test" logReader, logWriter := io.Pipe() - log.GetStandardLogger().OutWriter = logWriter - log.GetStandardLogger().ErrWriter = logWriter + log.GetStandardLogger().Out = logWriter defer func() { - log.IsDebug = false - log.GetStandardLogger().OutWriter = os.Stdout - log.GetStandardLogger().ErrWriter = os.Stderr + log.SetDebug(false) + log.GetStandardLogger().Out = os.Stderr }() stdoutReader, stdoutWriter := io.Pipe() diff --git a/libmachine/drivers/plugin/register_driver.go b/libmachine/drivers/plugin/register_driver.go index 7cbb811cdb..268bde5fe3 100644 --- a/libmachine/drivers/plugin/register_driver.go +++ b/libmachine/drivers/plugin/register_driver.go @@ -29,7 +29,7 @@ Please use this plugin through the main 'docker-machine' binary. os.Exit(1) } - log.IsDebug = true + log.SetDebug(true) rpcd := rpcdriver.NewRPCServerDriver(d) rpc.RegisterName(rpcdriver.RPCServiceNameV0, rpcd) diff --git a/libmachine/examples/vbox_create.go b/libmachine/examples/vbox_create.go index caad8a1e2c..96b67f38b8 100644 --- a/libmachine/examples/vbox_create.go +++ b/libmachine/examples/vbox_create.go @@ -8,13 +8,10 @@ import ( "github.com/docker/machine/drivers/virtualbox" "github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine/log" - "os" ) func main() { - log.IsDebug = true - log.GetStandardLogger().OutWriter = os.Stdout - log.GetStandardLogger().ErrWriter = os.Stderr + log.SetDebug(true) client := libmachine.NewClient("/tmp/automatic") diff --git a/libmachine/log/interface.go b/libmachine/log/interface.go index f8c1120e12..19597c4649 100644 --- a/libmachine/log/interface.go +++ b/libmachine/log/interface.go @@ -1,6 +1,6 @@ package log -type Logger interface { +type MachineLogger interface { Debug(...interface{}) Debugf(string, ...interface{}) diff --git a/libmachine/log/log.go b/libmachine/log/log.go index b85da5faa4..d969ac985f 100644 --- a/libmachine/log/log.go +++ b/libmachine/log/log.go @@ -1,64 +1,70 @@ package log import ( - "os" - "sync" + "github.com/Sirupsen/logrus" ) -var ( - l = StandardLogger{ - OutWriter: os.Stdout, - ErrWriter: os.Stderr, - mu: &sync.Mutex{}, - } - IsDebug = false -) +var logger *logrus.Logger + +func init() { + logger = logrus.New() + logger.Level = logrus.InfoLevel + logger.Formatter = new(machineFormatter) +} // RedirectStdOutToStdErr prevents any log from corrupting the output func RedirectStdOutToStdErr() { - l.OutWriter = l.ErrWriter + logger.Level = logrus.ErrorLevel } func Debug(args ...interface{}) { - l.Debug(args...) + logger.Debug(args...) } func Debugf(fmtString string, args ...interface{}) { - l.Debugf(fmtString, args...) + logger.Debugf(fmtString, args...) } func Error(args ...interface{}) { - l.Error(args...) + logger.Error(args...) } func Errorf(fmtString string, args ...interface{}) { - l.Errorf(fmtString, args...) + logger.Errorf(fmtString, args...) } func Info(args ...interface{}) { - l.Info(args...) + logger.Info(args...) } func Infof(fmtString string, args ...interface{}) { - l.Infof(fmtString, args...) + logger.Infof(fmtString, args...) } func Fatal(args ...interface{}) { - l.Fatal(args...) + logger.Fatal(args...) } func Fatalf(fmtString string, args ...interface{}) { - l.Fatalf(fmtString, args...) + logger.Fatalf(fmtString, args...) } func Warn(args ...interface{}) { - l.Warn(args...) + logger.Warn(args...) } func Warnf(fmtString string, args ...interface{}) { - l.Warnf(fmtString, args...) + logger.Warnf(fmtString, args...) } -func GetStandardLogger() *StandardLogger { - return &l +func GetStandardLogger() *logrus.Logger { + return logger +} + +func SetDebug(debug bool) { + if debug { + logger.Level = logrus.DebugLevel + } else { + logger.Level = logrus.InfoLevel + } } diff --git a/libmachine/log/machineFormatter.go b/libmachine/log/machineFormatter.go new file mode 100644 index 0000000000..21970efd5d --- /dev/null +++ b/libmachine/log/machineFormatter.go @@ -0,0 +1,19 @@ +package log + +import ( + "bytes" + + "github.com/Sirupsen/logrus" +) + +type machineFormatter struct { +} + +func (d *machineFormatter) Format(entry *logrus.Entry) ([]byte, error) { + b := &bytes.Buffer{} + + b.WriteString(entry.Message) + b.WriteByte('\n') + + return b.Bytes(), nil +} diff --git a/libmachine/log/standard.go b/libmachine/log/standard.go deleted file mode 100644 index 308c94a0e6..0000000000 --- a/libmachine/log/standard.go +++ /dev/null @@ -1,98 +0,0 @@ -package log - -import ( - "fmt" - "io" - "os" - "sync" -) - -type StandardLogger struct { - OutWriter io.Writer - ErrWriter io.Writer - mu *sync.Mutex -} - -func (t StandardLogger) log(args ...interface{}) { - defer t.mu.Unlock() - t.mu.Lock() - fmt.Fprint(t.OutWriter, args...) - fmt.Fprint(t.OutWriter, "\n") -} - -func (t StandardLogger) logf(fmtString string, args ...interface{}) { - defer t.mu.Unlock() - t.mu.Lock() - fmt.Fprintf(t.OutWriter, fmtString, args...) - fmt.Fprint(t.OutWriter, "\n") -} - -func (t StandardLogger) err(args ...interface{}) { - defer t.mu.Unlock() - t.mu.Lock() - fmt.Fprint(t.ErrWriter, args...) - fmt.Fprint(t.ErrWriter, "\n") -} - -func (t StandardLogger) errf(fmtString string, args ...interface{}) { - defer t.mu.Unlock() - t.mu.Lock() - fmt.Fprintf(t.ErrWriter, fmtString, args...) - fmt.Fprint(t.ErrWriter, "\n") -} - -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) Warn(args ...interface{}) { - fmt.Print("WARNING >>> ") - t.log(args...) -} - -func (t StandardLogger) Warnf(fmtString string, args ...interface{}) { - fmt.Print("WARNING >>> ") - t.logf(fmtString, args...) -}