Skip to content

Commit

Permalink
Some CR
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Shah committed Jan 14, 2017
1 parent 1e2740d commit 12345bd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
6 changes: 4 additions & 2 deletions field.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ func Any(key string, value interface{}) zapcore.Field {
switch val := value.(type) {
case zapcore.ObjectMarshaler:
return Object(key, val)
case zapcore.ArrayMarshaler:
return Array(key, val)
case bool:
return Bool(key, val)
case float64:
Expand Down Expand Up @@ -194,12 +196,12 @@ func Any(key string, value interface{}) zapcore.Field {
return Uintptr(key, val)
case string:
return String(key, val)
case error:
return String(key, val.Error())
case time.Time:
return Time(key, val)
case time.Duration:
return Duration(key, val)
case error:
return String(key, val.Error())
case fmt.Stringer:
return Stringer(key, val)
default:
Expand Down
1 change: 1 addition & 0 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func TestFieldConstructors(t *testing.T) {
{"Reflect", zapcore.Field{Key: "k", Type: zapcore.ReflectType, Interface: ints}, Reflect("k", ints)},
{"Nest", zapcore.Field{Key: "k", Type: zapcore.ObjectMarshalerType, Interface: nested}, Nest("k", nested...)},
{"Any:ObjectMarshaler", Any("k", name), Object("k", name)},
{"Any:ArrayMarshaler", Any("k", bools([]bool{true})), Array("k", bools([]bool{true}))},
{"Any:Bool", Any("k", true), Bool("k", true)},
{"Any:Float64", Any("k", 3.14), Float64("k", 3.14)},
// TODO (v1.0): We could use some approximately-equal logic here, but it's
Expand Down
22 changes: 11 additions & 11 deletions sugar.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"go.uber.org/zap/zapcore"
)

const oddNumberErrMsg = "Passed an odd number of keys and values to SugaredLogger, ignoring last."
const _oddNumberErrMsg = "Passed an odd number of keys and values to SugaredLogger, ignoring last."

// A SugaredLogger wraps the core Logger functionality in a slower, but less
// verbose, API.
Expand Down Expand Up @@ -66,7 +66,7 @@ func Desugar(s *SugaredLogger) Logger {
// Object("user", User{name: "alice"}),
// )
func (s *SugaredLogger) With(args ...interface{}) *SugaredLogger {
return s.WithFields(sweetenFields(args, s.core)...)
return s.WithFields(s.sweetenFields(args)...)
}

// WithFields adds structured fields to the logger's context, just like the
Expand All @@ -79,7 +79,7 @@ func (s *SugaredLogger) WithFields(fs ...zapcore.Field) *SugaredLogger {
// are treated as they are in the With method.
func (s *SugaredLogger) Debug(msg interface{}, keysAndValues ...interface{}) {
if ce := s.core.Check(DebugLevel, sweetenMsg(msg)); ce != nil {
ce.Write(sweetenFields(keysAndValues, s.core)...)
ce.Write(s.sweetenFields(keysAndValues)...)
}
}

Expand All @@ -95,7 +95,7 @@ func (s *SugaredLogger) Debugf(template string, args ...interface{}) {
// are treated as they are in the With method.
func (s *SugaredLogger) Info(msg interface{}, keysAndValues ...interface{}) {
if ce := s.core.Check(InfoLevel, sweetenMsg(msg)); ce != nil {
ce.Write(sweetenFields(keysAndValues, s.core)...)
ce.Write(s.sweetenFields(keysAndValues)...)
}
}

Expand All @@ -111,7 +111,7 @@ func (s *SugaredLogger) Infof(template string, args ...interface{}) {
// are treated as they are in the With method.
func (s *SugaredLogger) Warn(msg interface{}, keysAndValues ...interface{}) {
if ce := s.core.Check(WarnLevel, sweetenMsg(msg)); ce != nil {
ce.Write(sweetenFields(keysAndValues, s.core)...)
ce.Write(s.sweetenFields(keysAndValues)...)
}
}

Expand All @@ -127,7 +127,7 @@ func (s *SugaredLogger) Warnf(template string, args ...interface{}) {
// are treated as they are in the With method.
func (s *SugaredLogger) Error(msg interface{}, keysAndValues ...interface{}) {
if ce := s.core.Check(ErrorLevel, sweetenMsg(msg)); ce != nil {
ce.Write(sweetenFields(keysAndValues, s.core)...)
ce.Write(s.sweetenFields(keysAndValues)...)
}
}

Expand All @@ -144,7 +144,7 @@ func (s *SugaredLogger) Errorf(template string, args ...interface{}) {
// method. (See Logger.DPanic for details.)
func (s *SugaredLogger) DPanic(msg interface{}, keysAndValues ...interface{}) {
if ce := s.core.Check(DPanicLevel, sweetenMsg(msg)); ce != nil {
ce.Write(sweetenFields(keysAndValues, s.core)...)
ce.Write(s.sweetenFields(keysAndValues)...)
}
}

Expand All @@ -161,7 +161,7 @@ func (s *SugaredLogger) DPanicf(template string, args ...interface{}) {
// Keys and values are treated as they are in the With method.
func (s *SugaredLogger) Panic(msg interface{}, keysAndValues ...interface{}) {
if ce := s.core.Check(PanicLevel, sweetenMsg(msg)); ce != nil {
ce.Write(sweetenFields(keysAndValues, s.core)...)
ce.Write(s.sweetenFields(keysAndValues)...)
}
}

Expand All @@ -177,7 +177,7 @@ func (s *SugaredLogger) Panicf(template string, args ...interface{}) {
// os.Exit(1). Keys and values are treated as they are in the With method.
func (s *SugaredLogger) Fatal(msg interface{}, keysAndValues ...interface{}) {
if ce := s.core.Check(FatalLevel, sweetenMsg(msg)); ce != nil {
ce.Write(sweetenFields(keysAndValues, s.core)...)
ce.Write(s.sweetenFields(keysAndValues)...)
}
}

Expand All @@ -190,12 +190,12 @@ func (s *SugaredLogger) Fatalf(template string, args ...interface{}) {
}
}

func sweetenFields(args []interface{}, errLogger Logger) []zapcore.Field {
func (s *SugaredLogger) sweetenFields(args []interface{}) []zapcore.Field {
if len(args) == 0 {
return nil
}
if len(args)%2 == 1 {
errLogger.DPanic(oddNumberErrMsg, Any("ignored", args[len(args)-1]))
s.core.DPanic(_oddNumberErrMsg, Any("ignored", args[len(args)-1]))
}

fields := make([]zapcore.Field, len(args)/2)
Expand Down
2 changes: 1 addition & 1 deletion sugar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

func TestSugarWith(t *testing.T) {
ignored := observer.LoggedEntry{
Entry: zapcore.Entry{Level: DPanicLevel, Message: oddNumberErrMsg},
Entry: zapcore.Entry{Level: DPanicLevel, Message: _oddNumberErrMsg},
Context: []zapcore.Field{Any("ignored", "should ignore")},
}

Expand Down

0 comments on commit 12345bd

Please sign in to comment.