Merge pull request #8770 from LK4D4/logrus_support

Logrus support
This commit is contained in:
Jessie Frazelle 2014-10-27 09:05:24 -07:00
commit 89f1e164e4
11 changed files with 9 additions and 162 deletions

View file

@ -18,8 +18,8 @@ import (
"github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/fileutils"
"github.com/docker/docker/pkg/log"
"github.com/docker/docker/pkg/pools"
"github.com/docker/docker/pkg/promise"
"github.com/docker/docker/pkg/system"

View file

@ -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"
)

View file

@ -6,8 +6,8 @@ import (
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/jsonlog"
"github.com/docker/docker/pkg/log"
)
// BroadcastWriter accumulate multiple io.WriteCloser by stream.

View file

@ -1,7 +1,7 @@
package fileutils
import (
"github.com/docker/docker/pkg/log"
log "github.com/Sirupsen/logrus"
"path/filepath"
)

View file

@ -6,7 +6,7 @@ import (
"net/http"
"time"
"github.com/docker/docker/pkg/log"
log "github.com/Sirupsen/logrus"
)
type resumableRequestReader struct {

View file

@ -9,7 +9,7 @@ import (
"strconv"
"strings"
"github.com/docker/docker/pkg/log"
log "github.com/Sirupsen/logrus"
)
type Action string

View file

@ -1,114 +0,0 @@
package log
import (
"fmt"
"io"
"os"
"runtime"
"strings"
"time"
"github.com/docker/docker/pkg/timeutils"
)
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())
}
}
}

View file

@ -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

View file

@ -5,7 +5,7 @@ import (
"errors"
"io"
"github.com/docker/docker/pkg/log"
log "github.com/Sirupsen/logrus"
)
const (

View file

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