Skip to content

Commit

Permalink
Clean up default implementation of EncodeCaller
Browse files Browse the repository at this point in the history
  • Loading branch information
Akshay Shah committed Mar 14, 2017
1 parent 4d79b06 commit 77a9fcc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
10 changes: 4 additions & 6 deletions zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,19 @@ func (e *DurationEncoder) UnmarshalText(text []byte) error {
return nil
}

// A CallerEncoder serializes a EntryCaller to a primitive type.
// A CallerEncoder serializes an EntryCaller to a primitive type.
type CallerEncoder func(EntryCaller, PrimitiveArrayEncoder)

// FullPathCallerEncoder serializes caller in /full/path/file:line format.
// FullPathCallerEncoder serializes a caller in /full/path/file:line format.
func FullPathCallerEncoder(caller EntryCaller, enc PrimitiveArrayEncoder) {
// OPTIMIZE: after adding AppendBytes to PrimitiveArrayEncoder just copy bytes
// from buffer to not allocate string.
// TODO: consider using a byte-oriented API to save an allocation.
enc.AppendString(caller.String())
}

// UnmarshalText unmarshals text to a CallerEncoder.
// Anything is unmarshaled to FullPathCallerEncoder at that moment.
// FIXME: Support more options.
func (e *CallerEncoder) UnmarshalText(text []byte) error {
switch string(text) {
//case "gopath": // TODO
default:
*e = FullPathCallerEncoder
}
Expand Down
23 changes: 20 additions & 3 deletions zapcore/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,23 @@ func TestEncoderConfiguration(t *testing.T) {
expectedJSON: `{"L":"info","T":0,"N":"main","C":"foo.go:42","M":"hello","S":"fake-stack"}`,
expectedConsole: "0\tmain@foo.go:42\thello\nfake-stack",
},
{
desc: "handle no-op EncodeCaller",
cfg: EncoderConfig{
LevelKey: "L",
TimeKey: "T",
MessageKey: "M",
NameKey: "N",
CallerKey: "C",
StacktraceKey: "S",
EncodeTime: base.EncodeTime,
EncodeDuration: base.EncodeDuration,
EncodeLevel: base.EncodeLevel,
EncodeCaller: func(EntryCaller, PrimitiveArrayEncoder) {},
},
expectedJSON: `{"L":"info","T":0,"N":"main","C":"foo.go:42","M":"hello","S":"fake-stack"}`,
expectedConsole: "0\tinfo\tmain@foo.go:42\thello\nfake-stack",
},
}

for i, tt := range tests {
Expand Down Expand Up @@ -468,13 +485,13 @@ func TestDurationEncoders(t *testing.T) {
}

func TestCallerEncoders(t *testing.T) {
caller := _testEntry.Caller
caller := EntryCaller{Defined: true, File: "/home/jack/src/github.com/foo/foo.go", Line: 42}
tests := []struct {
name string
expected interface{} // output of serializing caller
}{
{"", "foo.go:42"},
{"something-random", "foo.go:42"},
{"", "/home/jack/src/github.com/foo/foo.go:42"},
{"something-random", "/home/jack/src/github.com/foo/foo.go:42"},
}

for _, tt := range tests {
Expand Down
8 changes: 6 additions & 2 deletions zapcore/json_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,14 @@ func (enc *jsonEncoder) EncodeEntry(ent Entry, fields []Field) (*buffer.Buffer,
final.AppendString(ent.LoggerName)
}
if ent.Caller.Defined && final.CallerKey != "" {
// NOTE: we add the field here for parity compromise with text
// prepending, while not actually mutating the message string.
final.addKey(final.CallerKey)
cur := final.buf.Len()
final.EncodeCaller(ent.Caller, final)
if cur == final.buf.Len() {
// User-supplied EncodeCaller was a no-op. Fall back to strings to
// keep output JSON valid.
final.AppendString(ent.Caller.String())
}
}
if final.MessageKey != "" {
final.addKey(enc.MessageKey)
Expand Down

0 comments on commit 77a9fcc

Please sign in to comment.