Merge pull request #247 from Sirupsen/revert-api-change

Revert api change
This commit is contained in:
Antoine Grondin 2015-09-07 21:03:30 -04:00
commit 82c8de1af2
3 changed files with 31 additions and 62 deletions

View File

@ -56,14 +56,6 @@ func (entry *Entry) String() (string, error) {
return reader.String(), err return reader.String(), err
} }
// ToError returns the field value of ErrorKey (nil)
func (entry *Entry) ToError() error {
if err, ok := entry.Data[ErrorKey].(error); ok {
return err
}
return nil
}
// Add an error as single field (using the key defined in ErrorKey) to the Entry. // Add an error as single field (using the key defined in ErrorKey) to the Entry.
func (entry *Entry) WithError(err error) *Entry { func (entry *Entry) WithError(err error) *Entry {
return entry.WithField(ErrorKey, err) return entry.WithField(ErrorKey, err)
@ -122,40 +114,36 @@ func (entry Entry) log(level Level, msg string) {
} }
} }
func (entry *Entry) Debug(args ...interface{}) *Entry { func (entry *Entry) Debug(args ...interface{}) {
if entry.Logger.Level >= DebugLevel { if entry.Logger.Level >= DebugLevel {
entry.log(DebugLevel, fmt.Sprint(args...)) entry.log(DebugLevel, fmt.Sprint(args...))
} }
return entry
} }
func (entry *Entry) Print(args ...interface{}) *Entry { func (entry *Entry) Print(args ...interface{}) {
return entry.Info(args...) entry.Info(args...)
} }
func (entry *Entry) Info(args ...interface{}) *Entry { func (entry *Entry) Info(args ...interface{}) {
if entry.Logger.Level >= InfoLevel { if entry.Logger.Level >= InfoLevel {
entry.log(InfoLevel, fmt.Sprint(args...)) entry.log(InfoLevel, fmt.Sprint(args...))
} }
return entry
} }
func (entry *Entry) Warn(args ...interface{}) *Entry { func (entry *Entry) Warn(args ...interface{}) {
if entry.Logger.Level >= WarnLevel { if entry.Logger.Level >= WarnLevel {
entry.log(WarnLevel, fmt.Sprint(args...)) entry.log(WarnLevel, fmt.Sprint(args...))
} }
return entry
} }
func (entry *Entry) Warning(args ...interface{}) *Entry { func (entry *Entry) Warning(args ...interface{}) {
return entry.Warn(args...) entry.Warn(args...)
} }
func (entry *Entry) Error(args ...interface{}) *Entry { func (entry *Entry) Error(args ...interface{}) {
if entry.Logger.Level >= ErrorLevel { if entry.Logger.Level >= ErrorLevel {
entry.log(ErrorLevel, fmt.Sprint(args...)) entry.log(ErrorLevel, fmt.Sprint(args...))
} }
return entry
} }
func (entry *Entry) Fatal(args ...interface{}) { func (entry *Entry) Fatal(args ...interface{}) {
@ -174,40 +162,36 @@ func (entry *Entry) Panic(args ...interface{}) {
// Entry Printf family functions // Entry Printf family functions
func (entry *Entry) Debugf(format string, args ...interface{}) *Entry { func (entry *Entry) Debugf(format string, args ...interface{}) {
if entry.Logger.Level >= DebugLevel { if entry.Logger.Level >= DebugLevel {
entry.Debug(fmt.Sprintf(format, args...)) entry.Debug(fmt.Sprintf(format, args...))
} }
return entry
} }
func (entry *Entry) Infof(format string, args ...interface{}) *Entry { func (entry *Entry) Infof(format string, args ...interface{}) {
if entry.Logger.Level >= InfoLevel { if entry.Logger.Level >= InfoLevel {
entry.Info(fmt.Sprintf(format, args...)) entry.Info(fmt.Sprintf(format, args...))
} }
return entry
} }
func (entry *Entry) Printf(format string, args ...interface{}) *Entry { func (entry *Entry) Printf(format string, args ...interface{}) {
return entry.Infof(format, args...) entry.Infof(format, args...)
} }
func (entry *Entry) Warnf(format string, args ...interface{}) *Entry { func (entry *Entry) Warnf(format string, args ...interface{}) {
if entry.Logger.Level >= WarnLevel { if entry.Logger.Level >= WarnLevel {
entry.Warn(fmt.Sprintf(format, args...)) entry.Warn(fmt.Sprintf(format, args...))
} }
return entry
} }
func (entry *Entry) Warningf(format string, args ...interface{}) *Entry { func (entry *Entry) Warningf(format string, args ...interface{}) {
return entry.Warnf(format, args...) entry.Warnf(format, args...)
} }
func (entry *Entry) Errorf(format string, args ...interface{}) *Entry { func (entry *Entry) Errorf(format string, args ...interface{}) {
if entry.Logger.Level >= ErrorLevel { if entry.Logger.Level >= ErrorLevel {
entry.Error(fmt.Sprintf(format, args...)) entry.Error(fmt.Sprintf(format, args...))
} }
return entry
} }
func (entry *Entry) Fatalf(format string, args ...interface{}) { func (entry *Entry) Fatalf(format string, args ...interface{}) {
@ -225,40 +209,36 @@ func (entry *Entry) Panicf(format string, args ...interface{}) {
// Entry Println family functions // Entry Println family functions
func (entry *Entry) Debugln(args ...interface{}) *Entry { func (entry *Entry) Debugln(args ...interface{}) {
if entry.Logger.Level >= DebugLevel { if entry.Logger.Level >= DebugLevel {
entry.Debug(entry.sprintlnn(args...)) entry.Debug(entry.sprintlnn(args...))
} }
return entry
} }
func (entry *Entry) Infoln(args ...interface{}) *Entry { func (entry *Entry) Infoln(args ...interface{}) {
if entry.Logger.Level >= InfoLevel { if entry.Logger.Level >= InfoLevel {
entry.Info(entry.sprintlnn(args...)) entry.Info(entry.sprintlnn(args...))
} }
return entry
} }
func (entry *Entry) Println(args ...interface{}) *Entry { func (entry *Entry) Println(args ...interface{}) {
return entry.Infoln(args...) entry.Infoln(args...)
} }
func (entry *Entry) Warnln(args ...interface{}) *Entry { func (entry *Entry) Warnln(args ...interface{}) {
if entry.Logger.Level >= WarnLevel { if entry.Logger.Level >= WarnLevel {
entry.Warn(entry.sprintlnn(args...)) entry.Warn(entry.sprintlnn(args...))
} }
return entry
} }
func (entry *Entry) Warningln(args ...interface{}) *Entry { func (entry *Entry) Warningln(args ...interface{}) {
return entry.Warnln(args...) entry.Warnln(args...)
} }
func (entry *Entry) Errorln(args ...interface{}) *Entry { func (entry *Entry) Errorln(args ...interface{}) {
if entry.Logger.Level >= ErrorLevel { if entry.Logger.Level >= ErrorLevel {
entry.Error(entry.sprintlnn(args...)) entry.Error(entry.sprintlnn(args...))
} }
return entry
} }
func (entry *Entry) Fatalln(args ...interface{}) { func (entry *Entry) Fatalln(args ...interface{}) {

View File

@ -8,23 +8,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
var err = fmt.Errorf("kaboom at layer %d", 4711)
func TestToError(t *testing.T) {
assert := assert.New(t)
ctx := WithField("foo", "bar")
assert.Equal(nil, ctx.Debug("Hello").ToError())
ctx.Data[ErrorKey] = "error"
assert.Equal(nil, ctx.Debug("Hello").ToError())
ctx = ctx.WithError(err)
assert.Equal(err, ctx.Debug("Hello").ToError())
}
func TestEntryWithError(t *testing.T) { func TestEntryWithError(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
@ -33,6 +16,8 @@ func TestEntryWithError(t *testing.T) {
ErrorKey = "error" ErrorKey = "error"
}() }()
err := fmt.Errorf("kaboom at layer %d", 4711)
assert.Equal(err, WithError(err).Data["error"]) assert.Equal(err, WithError(err).Data["error"])
logger := New() logger := New()

View File

@ -74,7 +74,11 @@ const (
) )
// Won't compile if StdLogger can't be realized by a log.Logger // Won't compile if StdLogger can't be realized by a log.Logger
var _ StdLogger = &log.Logger{} var (
_ StdLogger = &log.Logger{}
_ StdLogger = &Entry{}
_ StdLogger = &Logger{}
)
// StdLogger is what your logrus-enabled library should take, that way // StdLogger is what your logrus-enabled library should take, that way
// it'll accept a stdlib logger and a logrus logger. There's no standard // it'll accept a stdlib logger and a logrus logger. There's no standard