Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the correct format for the message timestamp #5

Merged
merged 1 commit into from
Jun 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}