Use logrus

Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
Jean-Laurent de Morlhon 2015-12-04 12:19:04 +01:00
parent 6fe3b5581e
commit 010a1c76e2
8 changed files with 59 additions and 136 deletions

View File

@ -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)
}
}

View File

@ -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()

View File

@ -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)

View File

@ -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")

View File

@ -1,6 +1,6 @@
package log
type Logger interface {
type MachineLogger interface {
Debug(...interface{})
Debugf(string, ...interface{})

View File

@ -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
}
}

View File

@ -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
}

View File

@ -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...)
}