From 712a6554cec44a7947db37ca2572aed6921a74ac Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Fri, 24 Oct 2014 10:12:35 -0700 Subject: [PATCH 1/3] Use logrus everywhere for logging Fixed #8761 Signed-off-by: Alexandr Morozov --- archive/archive.go | 2 +- archive/changes.go | 2 +- broadcastwriter/broadcastwriter.go | 2 +- fileutils/fileutils.go | 2 +- httputils/resumablerequestreader.go | 2 +- iptables/iptables.go | 2 +- log/log.go | 9 +++++++++ signal/trap.go | 2 +- stdcopy/stdcopy.go | 2 +- tarsum/tarsum.go | 2 +- 10 files changed, 18 insertions(+), 9 deletions(-) diff --git a/archive/archive.go b/archive/archive.go index 9814916..e4db63a 100644 --- a/archive/archive.go +++ b/archive/archive.go @@ -19,7 +19,7 @@ import ( "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" "github.com/docker/docker/pkg/fileutils" - "github.com/docker/docker/pkg/log" + log "github.com/Sirupsen/logrus" "github.com/docker/docker/pkg/pools" "github.com/docker/docker/pkg/promise" "github.com/docker/docker/pkg/system" diff --git a/archive/changes.go b/archive/changes.go index 5fbdcc9..557b5db 100644 --- a/archive/changes.go +++ b/archive/changes.go @@ -12,7 +12,7 @@ import ( "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" - "github.com/docker/docker/pkg/log" + log "github.com/Sirupsen/logrus" "github.com/docker/docker/pkg/pools" "github.com/docker/docker/pkg/system" ) diff --git a/broadcastwriter/broadcastwriter.go b/broadcastwriter/broadcastwriter.go index 1898302..a9ae104 100644 --- a/broadcastwriter/broadcastwriter.go +++ b/broadcastwriter/broadcastwriter.go @@ -7,7 +7,7 @@ import ( "time" "github.com/docker/docker/pkg/jsonlog" - "github.com/docker/docker/pkg/log" + log "github.com/Sirupsen/logrus" ) // BroadcastWriter accumulate multiple io.WriteCloser by stream. diff --git a/fileutils/fileutils.go b/fileutils/fileutils.go index acc27f5..4e4a91b 100644 --- a/fileutils/fileutils.go +++ b/fileutils/fileutils.go @@ -1,7 +1,7 @@ package fileutils import ( - "github.com/docker/docker/pkg/log" + log "github.com/Sirupsen/logrus" "path/filepath" ) diff --git a/httputils/resumablerequestreader.go b/httputils/resumablerequestreader.go index 3cd1f49..10edd43 100644 --- a/httputils/resumablerequestreader.go +++ b/httputils/resumablerequestreader.go @@ -6,7 +6,7 @@ import ( "net/http" "time" - "github.com/docker/docker/pkg/log" + log "github.com/Sirupsen/logrus" ) type resumableRequestReader struct { diff --git a/iptables/iptables.go b/iptables/iptables.go index b8d9e56..53e6e14 100644 --- a/iptables/iptables.go +++ b/iptables/iptables.go @@ -9,7 +9,7 @@ import ( "strconv" "strings" - "github.com/docker/docker/pkg/log" + log "github.com/Sirupsen/logrus" ) type Action string diff --git a/log/log.go b/log/log.go index b06d958..d636f76 100644 --- a/log/log.go +++ b/log/log.go @@ -8,9 +8,18 @@ import ( "strings" "time" + log "github.com/Sirupsen/logrus" "github.com/docker/docker/pkg/timeutils" ) +func init() { + log.SetOutput(os.Stderr) + log.SetLevel(log.InfoLevel) + if os.Getenv("DEBUG") != "" { + log.SetLevel(log.DebugLevel) + } +} + type priority int const ( diff --git a/signal/trap.go b/signal/trap.go index 42ddb4d..9be8267 100644 --- a/signal/trap.go +++ b/signal/trap.go @@ -6,7 +6,7 @@ import ( "sync/atomic" "syscall" - "github.com/docker/docker/pkg/log" + log "github.com/Sirupsen/logrus" ) // Trap sets up a simplified signal "trap", appropriate for common diff --git a/stdcopy/stdcopy.go b/stdcopy/stdcopy.go index 79e15bc..a61779c 100644 --- a/stdcopy/stdcopy.go +++ b/stdcopy/stdcopy.go @@ -5,7 +5,7 @@ import ( "errors" "io" - "github.com/docker/docker/pkg/log" + log "github.com/Sirupsen/logrus" ) const ( diff --git a/tarsum/tarsum.go b/tarsum/tarsum.go index 6581f3f..88d603c 100644 --- a/tarsum/tarsum.go +++ b/tarsum/tarsum.go @@ -13,7 +13,7 @@ import ( "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" - "github.com/docker/docker/pkg/log" + log "github.com/Sirupsen/logrus" ) const ( From 7dae3e3de3ac9160607a98109d6339eead7a86b0 Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Fri, 24 Oct 2014 13:47:52 -0700 Subject: [PATCH 2/3] Remove pkg/log Signed-off-by: Alexandr Morozov --- log/log.go | 123 ------------------------------------------------ log/log_test.go | 39 --------------- 2 files changed, 162 deletions(-) delete mode 100644 log/log.go delete mode 100644 log/log_test.go diff --git a/log/log.go b/log/log.go deleted file mode 100644 index d636f76..0000000 --- a/log/log.go +++ /dev/null @@ -1,123 +0,0 @@ -package log - -import ( - "fmt" - "io" - "os" - "runtime" - "strings" - "time" - - log "github.com/Sirupsen/logrus" - "github.com/docker/docker/pkg/timeutils" -) - -func init() { - log.SetOutput(os.Stderr) - log.SetLevel(log.InfoLevel) - if os.Getenv("DEBUG") != "" { - log.SetLevel(log.DebugLevel) - } -} - -type priority int - -const ( - errorFormat = "[%s] [%s] %s:%d %s\n" - logFormat = "[%s] [%s] %s\n" - - fatalPriority priority = iota - errorPriority - infoPriority - debugPriority -) - -// A common interface to access the Fatal method of -// both testing.B and testing.T. -type Fataler interface { - Fatal(args ...interface{}) -} - -func (p priority) String() string { - switch p { - case fatalPriority: - return "fatal" - case errorPriority: - return "error" - case infoPriority: - return "info" - case debugPriority: - return "debug" - } - - return "" -} - -var DefaultLogger = Logger{Out: os.Stdout, Err: os.Stderr} - -// Debug function, if the debug flag is set, then display. Do nothing otherwise -// If Docker is in damon mode, also send the debug info on the socket -func Debugf(format string, a ...interface{}) (int, error) { - return DefaultLogger.Debugf(format, a...) -} - -func Infof(format string, a ...interface{}) (int, error) { - return DefaultLogger.Infof(format, a...) -} - -func Errorf(format string, a ...interface{}) (int, error) { - return DefaultLogger.Errorf(format, a...) -} - -func Fatal(a ...interface{}) { - DefaultLogger.Fatalf("%s", a...) -} - -func Fatalf(format string, a ...interface{}) { - DefaultLogger.Fatalf(format, a...) -} - -type Logger struct { - Err io.Writer - Out io.Writer -} - -func (l Logger) Debugf(format string, a ...interface{}) (int, error) { - if os.Getenv("DEBUG") != "" { - return l.logf(l.Err, debugPriority, format, a...) - } - return 0, nil -} - -func (l Logger) Infof(format string, a ...interface{}) (int, error) { - return l.logf(l.Out, infoPriority, format, a...) -} - -func (l Logger) Errorf(format string, a ...interface{}) (int, error) { - return l.logf(l.Err, errorPriority, format, a...) -} - -func (l Logger) Fatalf(format string, a ...interface{}) { - l.logf(l.Err, fatalPriority, format, a...) - os.Exit(1) -} - -func (l Logger) logf(stream io.Writer, level priority, format string, a ...interface{}) (int, error) { - var prefix string - - if level <= errorPriority || level == debugPriority { - // Retrieve the stack infos - _, file, line, ok := runtime.Caller(2) - if !ok { - file = "" - line = -1 - } else { - file = file[strings.LastIndex(file, "/")+1:] - } - prefix = fmt.Sprintf(errorFormat, time.Now().Format(timeutils.RFC3339NanoFixed), level.String(), file, line, format) - } else { - prefix = fmt.Sprintf(logFormat, time.Now().Format(timeutils.RFC3339NanoFixed), level.String(), format) - } - - return fmt.Fprintf(stream, prefix, a...) -} diff --git a/log/log_test.go b/log/log_test.go deleted file mode 100644 index 4f5b3f8..0000000 --- a/log/log_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package log - -import ( - "bytes" - "regexp" - - "testing" -) - -var reRFC3339NanoFixed = "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{9}.([0-9]{2}:[0-9]{2})?" - -func TestLogFatalf(t *testing.T) { - var output *bytes.Buffer - - tests := []struct { - Level priority - Format string - Values []interface{} - ExpectedPattern string - }{ - {fatalPriority, "%d + %d = %d", []interface{}{1, 1, 2}, "\\[" + reRFC3339NanoFixed + "\\] \\[fatal\\] testing.go:\\d+ 1 \\+ 1 = 2"}, - {errorPriority, "%d + %d = %d", []interface{}{1, 1, 2}, "\\[" + reRFC3339NanoFixed + "\\] \\[error\\] testing.go:\\d+ 1 \\+ 1 = 2"}, - {infoPriority, "%d + %d = %d", []interface{}{1, 1, 2}, "\\[" + reRFC3339NanoFixed + "\\] \\[info\\] 1 \\+ 1 = 2"}, - {debugPriority, "%d + %d = %d", []interface{}{1, 1, 2}, "\\[" + reRFC3339NanoFixed + "\\] \\[debug\\] testing.go:\\d+ 1 \\+ 1 = 2"}, - } - - for i, test := range tests { - output = &bytes.Buffer{} - DefaultLogger.logf(output, test.Level, test.Format, test.Values...) - - expected := regexp.MustCompile(test.ExpectedPattern) - if !expected.MatchString(output.String()) { - t.Errorf("[%d] Log output does not match expected pattern:\n\tExpected: %s\n\tOutput: %s", - i, - expected.String(), - output.String()) - } - } -} From 015f966a1be210fa9a67adb49a94bed55dc2d0b7 Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Fri, 24 Oct 2014 15:11:48 -0700 Subject: [PATCH 3/3] Mass gofmt Signed-off-by: Alexandr Morozov --- archive/archive.go | 2 +- broadcastwriter/broadcastwriter.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/archive/archive.go b/archive/archive.go index e4db63a..9c4d881 100644 --- a/archive/archive.go +++ b/archive/archive.go @@ -18,8 +18,8 @@ import ( "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" - "github.com/docker/docker/pkg/fileutils" log "github.com/Sirupsen/logrus" + "github.com/docker/docker/pkg/fileutils" "github.com/docker/docker/pkg/pools" "github.com/docker/docker/pkg/promise" "github.com/docker/docker/pkg/system" diff --git a/broadcastwriter/broadcastwriter.go b/broadcastwriter/broadcastwriter.go index a9ae104..232cf3d 100644 --- a/broadcastwriter/broadcastwriter.go +++ b/broadcastwriter/broadcastwriter.go @@ -6,8 +6,8 @@ import ( "sync" "time" - "github.com/docker/docker/pkg/jsonlog" log "github.com/Sirupsen/logrus" + "github.com/docker/docker/pkg/jsonlog" ) // BroadcastWriter accumulate multiple io.WriteCloser by stream.