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

Removing "level" and "message" fields when logging #724

Closed
juicemia opened this issue Jun 24, 2019 · 2 comments
Closed

Removing "level" and "message" fields when logging #724

juicemia opened this issue Jun 24, 2019 · 2 comments

Comments

@juicemia
Copy link
Contributor

I was trying to figure out how to remove the "level" and "message" keys from log output and I couldn't find anything in the documentation.

After going through the source code, I found these lines in the JSON encoder's implementation of EncodeEntry:

	if final.LevelKey != "" {
		final.addKey(final.LevelKey)
		cur := final.buf.Len()
		final.EncodeLevel(ent.Level, final)
		if cur == final.buf.Len() {
			// User-supplied EncodeLevel was a no-op. Fall back to strings to keep
			// output JSON valid.
			final.AppendString(ent.Level.String())
		}
	}
	if final.MessageKey != "" {
		final.addKey(enc.MessageKey)
		final.AppendString(ent.Message)
	}

While it works for our use case, this behavior isn't documented anywhere and the Encoder interface doesn't specify that it should do that. I'm worried that because of this, at some point it'll change and we'll have to reevaluate how we do this.

I'd like to submit a PR that adds this documentation to the Encoder interface, as well as a constant (like const OmitKey = "") to make this behavior explicitly part of the interface.

I wanted to get your feedback on this before submitting the PR.

@jdheyburn
Copy link

Is this still an issue? Looks like merged PR closes it

@prashantv
Copy link
Collaborator

Yes this is done, here's an example on how to skip the message / level keys:

package main

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

func main() {
	cfg := zap.NewProductionConfig()
	cfg.EncoderConfig.MessageKey = zapcore.OmitKey
	cfg.EncoderConfig.LevelKey = zapcore.OmitKey

	logger, err := cfg.Build()
	if err != nil {
		panic(err)
	}

	logger.Info("this is a message", zap.String("field", "field-value"))
}

playground

Output:

{"ts":1257894000,"caller":"sandbox906972517/prog.go:18","field":"field-value"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants