Skip to content

Commit

Permalink
Merge pull request #5 from blendle/nanoseconds-ts
Browse files Browse the repository at this point in the history
Use the correct format for the message timestamp
  • Loading branch information
JeanMertz authored Jun 9, 2018
2 parents af27c08 + 7d977c6 commit 2d60034
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
14 changes: 12 additions & 2 deletions encoder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package zapdriver

import "go.uber.org/zap/zapcore"
import (
"time"

"go.uber.org/zap/zapcore"
)

// logLevelSeverity maps the Zap log levels to the correct level names as
// defined by Stackdriver.
Expand Down Expand Up @@ -37,7 +41,7 @@ var encoderConfig = zapcore.EncoderConfig{
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: EncodeLevel,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeTime: RFC3339NanoTimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}
Expand All @@ -47,3 +51,9 @@ var encoderConfig = zapcore.EncoderConfig{
func EncodeLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(logLevelSeverity[l])
}

// RFC3339NanoTimeEncoder serializes a time.Time to an RFC3339Nano-formatted
// string with nanoseconds precision.
func RFC3339NanoTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format(time.RFC3339Nano))
}
13 changes: 13 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zapdriver_test

import (
"testing"
"time"

"github.com/blendle/zapdriver"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -35,3 +36,15 @@ func TestEncodeLevel(t *testing.T) {
})
}
}

func TestRFC3339NanoTimeEncoder(t *testing.T) {
t.Parallel()

ts := time.Date(2018, 4, 9, 12, 43, 12, 678359, time.UTC)

enc := &sliceArrayEncoder{}
zapdriver.RFC3339NanoTimeEncoder(ts, enc)

require.Len(t, enc.elems, 1)
assert.Equal(t, ts.Format(time.RFC3339Nano), enc.elems[0].(string))
}

0 comments on commit 2d60034

Please sign in to comment.