From 7dae3e3de3ac9160607a98109d6339eead7a86b0 Mon Sep 17 00:00:00 2001 From: Alexandr Morozov Date: Fri, 24 Oct 2014 13:47:52 -0700 Subject: [PATCH] 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()) - } - } -}