Skip to content

Commit

Permalink
GoPathCallerEncoder (#304)
Browse files Browse the repository at this point in the history
Add GoPathCallerEncoder that trims caller file to compile time $GOPATH.
Use it by default.
$GOPATH trim logic based on go-stack/stack which is under Apache license, so can't be put in repo.
So required logic have been moved to another repo, and exported.
  • Loading branch information
skipor committed Feb 19, 2017
1 parent b6b7b16 commit 9bce468
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package: go.uber.org/zap
license: MIT
import:
- package: go.uber.org/atomic
- package: github.com/skipor/goenv
testImport:
- package: github.com/satori/go.uuid
- package: github.com/sirupsen/logrus
Expand Down
24 changes: 22 additions & 2 deletions zapcore/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import (
"strings"
"time"

"github.com/skipor/goenv"

"go.uber.org/zap/buffer"
"go.uber.org/zap/internal/bufferpool"
)

// A LevelEncoder serializes a Level to a primitive type.
Expand Down Expand Up @@ -146,13 +149,30 @@ func FullPathCallerEncoder(caller EntryCaller, enc PrimitiveArrayEncoder) {
enc.AppendString(caller.String())
}

func GoPathCallerEncoder(caller EntryCaller, enc PrimitiveArrayEncoder) {
if !caller.Defined {
enc.AppendString("")
return
}
buf := bufferpool.Get()
buf.AppendString(goenv.TrimGoPathSrc(caller.File))
buf.AppendByte(':')
buf.AppendInt(int64(caller.Line))
// OPTIMIZE: after adding AppendBytes to PrimitiveArrayEncoder just copy bytes
// from buffer to not allocate string.
str := buf.String()
bufferpool.Put(buf)
enc.AppendString(str)
}

// UnmarshalText unmarshals text to a CallerEncoder.
// Anything is unmarshaled to FullPathCallerEncoder at that moment.
func (e *CallerEncoder) UnmarshalText(text []byte) error {
switch string(text) {
//case "gopath": // TODO
default:
case "full":
*e = FullPathCallerEncoder
default:
*e = GoPathCallerEncoder
}
return nil
}
Expand Down
10 changes: 6 additions & 4 deletions zapcore/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/skipor/goenv"
. "go.uber.org/zap/zapcore"
)

Expand Down Expand Up @@ -468,13 +469,14 @@ func TestDurationEncoders(t *testing.T) {
}

func TestCallerEncoders(t *testing.T) {
caller := _testEntry.Caller
caller := EntryCaller{Defined: true, File: goenv.GoPathSrc() + "a/b/foo.go", Line: 42}
tests := []struct {
name string
expected interface{} // output of serializing caller
}{
{"", "foo.go:42"},
{"something-random", "foo.go:42"},
{"", "a/b/foo.go:42"},
{"something-random", "a/b/foo.go:42"},
{"full", caller.File + ":42"},
}

for _, tt := range tests {
Expand All @@ -484,7 +486,7 @@ func TestCallerEncoders(t *testing.T) {
t,
tt.expected,
func(arr ArrayEncoder) { ce(caller, arr) },
"Unexpected output serializing InfoLevel with %q.", tt.name,
"Unexpected output serializing file name as %v with %q.", tt.expected, tt.name,
)
}
}
Expand Down

0 comments on commit 9bce468

Please sign in to comment.