Skip to content

Commit 4792177

Browse files
committed
Logrus Machine Logger
Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
1 parent 010a1c7 commit 4792177

File tree

10 files changed

+225
-37
lines changed

10 files changed

+225
-37
lines changed

commands/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func detectShell() (string, error) {
244244
if shell == "" {
245245
// check for windows env and not bash (i.e. msysgit, etc)
246246
if runtime.GOOS == "windows" {
247-
log.Infof("On Windows, please specify either 'cmd' or 'powershell' with the --shell flag.\n\n")
247+
log.Info("On Windows, please specify either 'cmd' or 'powershell' with the --shell flag.\n\n")
248248
}
249249

250250
return "", ErrUnknownShell

libmachine/drivers/plugin/localbinary/plugin_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ func TestExecServer(t *testing.T) {
8383

8484
logReader, logWriter := io.Pipe()
8585

86-
log.GetStandardLogger().Out = logWriter
86+
log.SetOutput(logWriter)
8787

8888
defer func() {
8989
log.SetDebug(false)
90-
log.GetStandardLogger().Out = os.Stderr
90+
log.SetOutput(os.Stderr)
9191
}()
9292

9393
stdoutReader, stdoutWriter := io.Pipe()

libmachine/log/interface.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

libmachine/log/log.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
package log
22

3-
import (
4-
"github.com/Sirupsen/logrus"
5-
)
3+
import "io"
64

7-
var logger *logrus.Logger
5+
var logger MachineLogger
86

97
func init() {
10-
logger = logrus.New()
11-
logger.Level = logrus.InfoLevel
12-
logger.Formatter = new(machineFormatter)
8+
logger = NewMachineLogger()
139
}
1410

1511
// RedirectStdOutToStdErr prevents any log from corrupting the output
1612
func RedirectStdOutToStdErr() {
17-
logger.Level = logrus.ErrorLevel
13+
logger.RedirectStdOutToStdErr()
1814
}
1915

2016
func Debug(args ...interface{}) {
@@ -57,14 +53,14 @@ func Warnf(fmtString string, args ...interface{}) {
5753
logger.Warnf(fmtString, args...)
5854
}
5955

60-
func GetStandardLogger() *logrus.Logger {
56+
func Logger() interface{} {
6157
return logger
6258
}
6359

6460
func SetDebug(debug bool) {
65-
if debug {
66-
logger.Level = logrus.DebugLevel
67-
} else {
68-
logger.Level = logrus.InfoLevel
69-
}
61+
logger.SetDebug(debug)
62+
}
63+
64+
func SetOutput(out io.Writer) {
65+
logger.SetOutput(out)
7066
}

libmachine/log/log_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package log
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestFormatterIsSet(t *testing.T) {
8+
9+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package log
2+
3+
import (
4+
"io"
5+
6+
"github.com/Sirupsen/logrus"
7+
)
8+
9+
type LogrusMachineLogger struct {
10+
logger *logrus.Logger
11+
}
12+
13+
func NewMachineLogger() MachineLogger {
14+
logrusLogger := logrus.New()
15+
logrusLogger.Level = logrus.InfoLevel
16+
logrusLogger.Formatter = new(MachineFormatter)
17+
return LogrusMachineLogger{logrusLogger}
18+
}
19+
20+
// RedirectStdOutToStdErr prevents any log from corrupting the output
21+
func (ml LogrusMachineLogger) RedirectStdOutToStdErr() {
22+
ml.logger.Level = logrus.ErrorLevel
23+
}
24+
25+
func (ml LogrusMachineLogger) SetDebug(debug bool) {
26+
if debug {
27+
ml.logger.Level = logrus.DebugLevel
28+
} else {
29+
ml.logger.Level = logrus.InfoLevel
30+
}
31+
}
32+
33+
func (ml LogrusMachineLogger) SetOutput(out io.Writer) {
34+
ml.logger.Out = out
35+
}
36+
37+
func (ml LogrusMachineLogger) Logger() interface{} {
38+
return ml.logger
39+
}
40+
41+
func (ml LogrusMachineLogger) Debug(args ...interface{}) {
42+
ml.logger.Debug(args...)
43+
}
44+
45+
func (ml LogrusMachineLogger) Debugf(fmtString string, args ...interface{}) {
46+
ml.logger.Debugf(fmtString, args...)
47+
}
48+
49+
func (ml LogrusMachineLogger) Error(args ...interface{}) {
50+
ml.logger.Error(args...)
51+
}
52+
53+
func (ml LogrusMachineLogger) Errorf(fmtString string, args ...interface{}) {
54+
ml.logger.Errorf(fmtString, args...)
55+
}
56+
57+
func (ml LogrusMachineLogger) Info(args ...interface{}) {
58+
ml.logger.Info(args...)
59+
}
60+
61+
func (ml LogrusMachineLogger) Infof(fmtString string, args ...interface{}) {
62+
ml.logger.Infof(fmtString, args...)
63+
}
64+
65+
func (ml LogrusMachineLogger) Fatal(args ...interface{}) {
66+
ml.logger.Fatal(args...)
67+
}
68+
69+
func (ml LogrusMachineLogger) Fatalf(fmtString string, args ...interface{}) {
70+
ml.logger.Fatalf(fmtString, args...)
71+
}
72+
73+
func (ml LogrusMachineLogger) Warn(args ...interface{}) {
74+
ml.logger.Warn(args...)
75+
}
76+
77+
func (ml LogrusMachineLogger) Warnf(fmtString string, args ...interface{}) {
78+
ml.logger.Warnf(fmtString, args...)
79+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package log
2+
3+
import (
4+
"testing"
5+
6+
"bufio"
7+
"io"
8+
9+
"github.com/Sirupsen/logrus"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestDefaultLevelIsInfo(t *testing.T) {
14+
testLogger := NewMachineLogger()
15+
assert.Equal(t, testLogger.Logger().(*logrus.Logger).Level, logrus.InfoLevel)
16+
}
17+
18+
func TestSetDebugToTrue(t *testing.T) {
19+
testLogger := NewMachineLogger()
20+
testLogger.SetDebug(true)
21+
assert.Equal(t, testLogger.Logger().(*logrus.Logger).Level, logrus.DebugLevel)
22+
}
23+
24+
func TestSetDebugToFalse(t *testing.T) {
25+
testLogger := NewMachineLogger()
26+
testLogger.SetDebug(true)
27+
testLogger.SetDebug(false)
28+
assert.Equal(t, testLogger.Logger().(*logrus.Logger).Level, logrus.InfoLevel)
29+
}
30+
31+
func TestSetSilenceOutput(t *testing.T) {
32+
testLogger := NewMachineLogger()
33+
testLogger.RedirectStdOutToStdErr()
34+
assert.Equal(t, testLogger.Logger().(*logrus.Logger).Level, logrus.ErrorLevel)
35+
}
36+
37+
func TestDebug(t *testing.T) {
38+
testLogger := NewMachineLogger()
39+
testLogger.SetDebug(true)
40+
41+
result := captureOutput(testLogger, func() { testLogger.Debug("debug") })
42+
43+
assert.Equal(t, result, "debug")
44+
}
45+
46+
func TestInfo(t *testing.T) {
47+
testLogger := NewMachineLogger()
48+
49+
result := captureOutput(testLogger, func() { testLogger.Info("info") })
50+
51+
assert.Equal(t, result, "info")
52+
}
53+
54+
func TestWarn(t *testing.T) {
55+
testLogger := NewMachineLogger()
56+
57+
result := captureOutput(testLogger, func() { testLogger.Warn("warn") })
58+
59+
assert.Equal(t, result, "warn")
60+
}
61+
62+
func TestError(t *testing.T) {
63+
testLogger := NewMachineLogger()
64+
65+
result := captureOutput(testLogger, func() { testLogger.Error("error") })
66+
67+
assert.Equal(t, result, "error")
68+
}
69+
70+
func captureOutput(testLogger MachineLogger, lambda func()) string {
71+
pipeReader, pipeWriter := io.Pipe()
72+
scanner := bufio.NewScanner(pipeReader)
73+
testLogger.SetOutput(pipeWriter)
74+
go lambda()
75+
scanner.Scan()
76+
return scanner.Text()
77+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"github.com/Sirupsen/logrus"
77
)
88

9-
type machineFormatter struct {
9+
type MachineFormatter struct {
1010
}
1111

12-
func (d *machineFormatter) Format(entry *logrus.Entry) ([]byte, error) {
12+
func (d *MachineFormatter) Format(entry *logrus.Entry) ([]byte, error) {
1313
b := &bytes.Buffer{}
1414

1515
b.WriteString(entry.Message)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package log
2+
3+
import (
4+
"testing"
5+
6+
"github.com/Sirupsen/logrus"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestWrite(t *testing.T) {
11+
entry := logrus.NewEntry(logrus.New())
12+
entry.Message = "foobar"
13+
formatter := MachineFormatter{}
14+
bytes, err := formatter.Format(entry)
15+
assert.Nil(t, err)
16+
assert.Equal(t, string(bytes[:]), "foobar\n")
17+
}

libmachine/log/machine_logger.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package log
2+
3+
import "io"
4+
5+
type MachineLogger interface {
6+
RedirectStdOutToStdErr()
7+
8+
SetDebug(debug bool)
9+
10+
SetOutput(io.Writer)
11+
12+
Debug(args ...interface{})
13+
Debugf(fmtString string, args ...interface{})
14+
15+
Error(args ...interface{})
16+
Errorf(fmtString string, args ...interface{})
17+
18+
Info(args ...interface{})
19+
Infof(fmtString string, args ...interface{})
20+
21+
Fatal(args ...interface{})
22+
Fatalf(fmtString string, args ...interface{})
23+
24+
Warn(args ...interface{})
25+
Warnf(fmtString string, args ...interface{})
26+
27+
Logger() interface{}
28+
}

0 commit comments

Comments
 (0)