Cleanup stdout/stderr log initialization

Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
Jean-Laurent de Morlhon 2015-12-03 15:56:22 +01:00
parent 892e19c019
commit 2e79f67e93
5 changed files with 17 additions and 26 deletions

View File

@ -2,7 +2,6 @@ package commands
import ( import (
"fmt" "fmt"
"os"
"github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/check" "github.com/docker/machine/libmachine/check"
@ -12,7 +11,7 @@ import (
func cmdConfig(c CommandLine, api libmachine.API) error { func cmdConfig(c CommandLine, api libmachine.API) error {
// Ensure that log messages always go to stderr when this command is // Ensure that log messages always go to stderr when this command is
// being run (it is intended to be run in a subshell) // being run (it is intended to be run in a subshell)
log.SetOutWriter(os.Stderr) log.RedirectStdOutToStdErr()
if len(c.Args()) != 1 { if len(c.Args()) != 1 {
return ErrExpectedOneMachine return ErrExpectedOneMachine

View File

@ -50,7 +50,7 @@ func cmdEnv(c CommandLine, api libmachine.API) error {
// Ensure that log messages always go to stderr when this command is // Ensure that log messages always go to stderr when this command is
// being run (it is intended to be run in a subshell) // being run (it is intended to be run in a subshell)
log.SetOutWriter(os.Stderr) log.RedirectStdOutToStdErr()
if c.Bool("unset") { if c.Bool("unset") {
shellCfg, err = shellCfgUnset(c, api) shellCfg, err = shellCfgUnset(c, api)

View File

@ -82,13 +82,13 @@ func TestExecServer(t *testing.T) {
logReader, logWriter := io.Pipe() logReader, logWriter := io.Pipe()
log.SetOutWriter(logWriter) log.GetStandardLogger().OutWriter = logWriter
log.SetErrWriter(logWriter) log.GetStandardLogger().ErrWriter = logWriter
defer func() { defer func() {
log.IsDebug = false log.IsDebug = false
log.SetOutWriter(os.Stdout) log.GetStandardLogger().OutWriter = os.Stdout
log.SetErrWriter(os.Stderr) log.GetStandardLogger().ErrWriter = os.Stderr
}() }()
stdoutReader, stdoutWriter := io.Pipe() stdoutReader, stdoutWriter := io.Pipe()

View File

@ -4,17 +4,17 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"github.com/docker/machine/drivers/virtualbox" "github.com/docker/machine/drivers/virtualbox"
"github.com/docker/machine/libmachine" "github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/log"
"os"
) )
func main() { func main() {
log.IsDebug = true log.IsDebug = true
log.SetOutWriter(os.Stdout) log.GetStandardLogger().OutWriter = os.Stdout
log.SetErrWriter(os.Stderr) log.GetStandardLogger().ErrWriter = os.Stderr
client := libmachine.NewClient("/tmp/automatic") client := libmachine.NewClient("/tmp/automatic")

View File

@ -1,33 +1,24 @@
package log package log
import ( import (
"io"
"os" "os"
"sync" "sync"
) )
var ( var (
l = StandardLogger{ l = StandardLogger{
mu: &sync.Mutex{}, OutWriter: os.Stdout,
ErrWriter: os.Stderr,
mu: &sync.Mutex{},
} }
IsDebug = false IsDebug = false
) )
type Fields map[string]interface{} type Fields map[string]interface{}
func init() { // RedirectStdOutToStdErr prevents any log from corrupting the output
// TODO: Is this really the best approach? I worry that it will create func RedirectStdOutToStdErr() {
// implicit behavior which may be problmatic for users of the lib. l.OutWriter = l.ErrWriter
SetOutWriter(os.Stdout)
SetErrWriter(os.Stderr)
}
func SetOutWriter(w io.Writer) {
l.OutWriter = w
}
func SetErrWriter(w io.Writer) {
l.ErrWriter = w
} }
func Debug(args ...interface{}) { func Debug(args ...interface{}) {
@ -54,7 +45,6 @@ func Infof(fmtString string, args ...interface{}) {
l.Infof(fmtString, args...) l.Infof(fmtString, args...)
} }
func Fatal(args ...interface{}) { func Fatal(args ...interface{}) {
l.Fatal(args...) l.Fatal(args...)
} }
@ -79,4 +69,6 @@ func WithField(fieldName string, field interface{}) Logger {
func WithFields(fields Fields) Logger { func WithFields(fields Fields) Logger {
return l.WithFields(fields) return l.WithFields(fields)
func GetStandardLogger() *StandardLogger {
return &l
} }