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

Add options for JSON encoders #105

Merged
merged 3 commits into from
Jul 27, 2016
Merged

Add options for JSON encoders #105

merged 3 commits into from
Jul 27, 2016

Conversation

akshayjshah
Copy link
Contributor

@akshayjshah akshayjshah commented Jul 26, 2016

Let users specify how they'd like the message, level, and timestamp formatted for each log entry.

In order to keep the proposed change small, this PR introduces a small performance regression in jsonEncoder.WriteEntry - it copies more bytes than necessary. In the next PR, I'm going to flatten the message structure, which will remove this extra copy and require many changes to the test code.

(I'm holding off on marking the related issues resolved until we export the newJSONEncoder function.)

(edit by Prashant: this brings internal support for #33)

Akshay Shah added 2 commits July 26, 2016 16:24
Add options for the JSON encoder, and supply options to control the
message, timestamp, and level formatting. Users can also supply their
own formatters if they have unusual requirements.
Allow users to pass options in the JSON encoder constructor, and
validate that those options are honored.

To minimize the test changes required, introduce a performance
regression in `WriteEntry`. When we flatten the serialized message (in
the next PR), we'll remove the extra truncate-and-copy step.
@@ -30,5 +33,5 @@ type encoder interface {
AddFields([]Field)
Clone() encoder
Free()
WriteEntry(io.Writer, *Entry) error
WriteEntry(io.Writer, string, Level, time.Time) error
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This undoes a change made in the previous PR - after some more thought, I think it's better to keep encoders and entries separate. This removes the concern about type mismatches between the receiver of WriteEntry and Entry.encoder.

I do still think that the WriteEntry name is appropriate, though, since we're writing out a single log line.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

import "time"

// JSONOption is used to set options for a JSON encoder.
type JSONOption interface {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be exported?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also unexport the Apply method so that the custom formatter types won't display Apply in the documentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexporting Apply definitely makes sense...not sure why I exported it to begin with. We should keep JSONOption exported, though, since it'll be part of the public NewJSONEncoder signature.

@prashantv
Copy link
Collaborator

Completed the first pass, the formatters look good. I'd like to reduce the exported API a fair bit more though (unexporting JSONOption and related functions, and removing some of the skip formatters).

Unexport some options-related functionality, remove some stubs, and
clean up multiple frees.
@prashantv
Copy link
Collaborator

lgtm

@akshayjshah
Copy link
Contributor Author

akshayjshah commented Jul 27, 2016

Benchmarks

go test -bench ZapWithAccumulatedContext -benchmem -run XXXX -cpu 1 ./benchmarks

Pre:

BenchmarkZapWithAccumulatedContext       2000000               667 ns/op               0 B/op          0 allocs/op

Post:

BenchmarkZapWithAccumulatedContext       2000000               652 ns/op               0 B/op          0 allocs/op

@akshayjshah akshayjshah merged commit ed0f791 into master Jul 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants