|
@@ -6,18 +6,21 @@ import (
|
|
"os"
|
|
"os"
|
|
"runtime"
|
|
"runtime"
|
|
"strings"
|
|
"strings"
|
|
|
|
+ "time"
|
|
|
|
+
|
|
|
|
+ "github.com/docker/docker/pkg/timeutils"
|
|
)
|
|
)
|
|
|
|
|
|
type priority int
|
|
type priority int
|
|
|
|
|
|
const (
|
|
const (
|
|
- errorFormat = "[%s] %s:%d %s\n"
|
|
|
|
- logFormat = "[%s] %s\n"
|
|
|
|
|
|
+ errorFormat = "[%s] [%s] %s:%d %s\n"
|
|
|
|
+ logFormat = "[%s] [%s] %s\n"
|
|
|
|
|
|
- fatal priority = iota
|
|
|
|
- error
|
|
|
|
- info
|
|
|
|
- debug
|
|
|
|
|
|
+ fatalPriority priority = iota
|
|
|
|
+ errorPriority
|
|
|
|
+ infoPriority
|
|
|
|
+ debugPriority
|
|
)
|
|
)
|
|
|
|
|
|
// A common interface to access the Fatal method of
|
|
// A common interface to access the Fatal method of
|
|
@@ -28,44 +31,72 @@ type Fataler interface {
|
|
|
|
|
|
func (p priority) String() string {
|
|
func (p priority) String() string {
|
|
switch p {
|
|
switch p {
|
|
- case fatal:
|
|
|
|
|
|
+ case fatalPriority:
|
|
return "fatal"
|
|
return "fatal"
|
|
- case error:
|
|
|
|
|
|
+ case errorPriority:
|
|
return "error"
|
|
return "error"
|
|
- case info:
|
|
|
|
|
|
+ case infoPriority:
|
|
return "info"
|
|
return "info"
|
|
- case debug:
|
|
|
|
|
|
+ case debugPriority:
|
|
return "debug"
|
|
return "debug"
|
|
}
|
|
}
|
|
|
|
|
|
return ""
|
|
return ""
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+var DefaultLogger = Logger{Out: os.Stdout, Err: os.Stderr}
|
|
|
|
+
|
|
// Debug function, if the debug flag is set, then display. Do nothing otherwise
|
|
// 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
|
|
// If Docker is in damon mode, also send the debug info on the socket
|
|
-func Debugf(format string, a ...interface{}) {
|
|
|
|
|
|
+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") != "" {
|
|
if os.Getenv("DEBUG") != "" {
|
|
- logf(os.Stderr, debug, format, a...)
|
|
|
|
|
|
+ return l.logf(l.Err, debugPriority, format, a...)
|
|
}
|
|
}
|
|
|
|
+ return 0, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func Infof(format string, a ...interface{}) {
|
|
|
|
- logf(os.Stdout, info, format, a...)
|
|
|
|
|
|
+func (l Logger) Infof(format string, a ...interface{}) (int, error) {
|
|
|
|
+ return l.logf(l.Out, infoPriority, format, a...)
|
|
}
|
|
}
|
|
|
|
|
|
-func Errorf(format string, a ...interface{}) {
|
|
|
|
- logf(os.Stderr, error, format, a...)
|
|
|
|
|
|
+func (l Logger) Errorf(format string, a ...interface{}) (int, error) {
|
|
|
|
+ return l.logf(l.Err, errorPriority, format, a...)
|
|
}
|
|
}
|
|
|
|
|
|
-func Fatalf(format string, a ...interface{}) {
|
|
|
|
- logf(os.Stderr, fatal, format, a...)
|
|
|
|
|
|
+func (l Logger) Fatalf(format string, a ...interface{}) {
|
|
|
|
+ l.logf(l.Err, fatalPriority, format, a...)
|
|
os.Exit(1)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
|
|
-func logf(stream io.Writer, level priority, format string, a ...interface{}) {
|
|
|
|
|
|
+func (l Logger) logf(stream io.Writer, level priority, format string, a ...interface{}) (int, error) {
|
|
var prefix string
|
|
var prefix string
|
|
|
|
|
|
- if level <= error || level == debug {
|
|
|
|
|
|
+ if level <= errorPriority || level == debugPriority {
|
|
// Retrieve the stack infos
|
|
// Retrieve the stack infos
|
|
_, file, line, ok := runtime.Caller(2)
|
|
_, file, line, ok := runtime.Caller(2)
|
|
if !ok {
|
|
if !ok {
|
|
@@ -74,10 +105,10 @@ func logf(stream io.Writer, level priority, format string, a ...interface{}) {
|
|
} else {
|
|
} else {
|
|
file = file[strings.LastIndex(file, "/")+1:]
|
|
file = file[strings.LastIndex(file, "/")+1:]
|
|
}
|
|
}
|
|
- prefix = fmt.Sprintf(errorFormat, level.String(), file, line, format)
|
|
|
|
|
|
+ prefix = fmt.Sprintf(errorFormat, time.Now().Format(timeutils.RFC3339NanoFixed), level.String(), file, line, format)
|
|
} else {
|
|
} else {
|
|
- prefix = fmt.Sprintf(logFormat, level.String(), format)
|
|
|
|
|
|
+ prefix = fmt.Sprintf(logFormat, time.Now().Format(timeutils.RFC3339NanoFixed), level.String(), format)
|
|
}
|
|
}
|
|
|
|
|
|
- fmt.Fprintf(stream, prefix, a...)
|
|
|
|
|
|
+ return fmt.Fprintf(stream, prefix, a...)
|
|
}
|
|
}
|