Remove pkg/log

Signed-off-by: Alexandr Morozov <lk4d4@docker.com>
This commit is contained in:
Alexandr Morozov 2014-10-24 13:47:52 -07:00
parent 712a6554ce
commit 7dae3e3de3
2 changed files with 0 additions and 162 deletions

View file

@ -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 = "<unknown>"
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...)
}

View file

@ -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())
}
}
}