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

Inconsistent namespace handling for reserved logger keys #12

Open
els0r opened this issue Nov 25, 2022 · 2 comments
Open

Inconsistent namespace handling for reserved logger keys #12

els0r opened this issue Nov 25, 2022 · 2 comments

Comments

@els0r
Copy link

els0r commented Nov 25, 2022

The encoder somewhat breaks namespacing that zap provides, since it appends the namespace foo to the msg key (and others) as well. This is not the case in the zap package.

Compare the output of logfmt (from this plugin):

database.ts=2022-11-25T13:50:59.759+0100 database.level=info database.caller=cmd/root.go:150 database.msg="initializing DB connection" 

to standard json encoding via zap package:

{"level":"info","ts":"2022-11-25T13:52:12.539+0100","caller":"cmd/root.go:150","msg":"initializing DB connection","app_name":"main","app_version":"snapshot","database":{"host": ... }}

The solution would be something along the lines of

func (enc *logfmtEncoder) addKey(key string) {
	if enc.buf.Len() > 0 {
		enc.buf.AppendByte(' ')
	}
	switch key {
	case enc.EncoderConfig.TimeKey, enc.EncoderConfig.LevelKey, enc.EncoderConfig.NameKey, enc.EncoderConfig.CallerKey, enc.EncoderConfig.MessageKey, enc.EncoderConfig.StacktraceKey:
	default:
		for _, ns := range enc.namespaces {
			enc.safeAddString(ns)
			enc.buf.AppendByte('.')
		}
	}
	enc.safeAddString(key)
	enc.buf.AppendByte('=')
}

Let me know if I should open a PR.

@jsternberg
Copy link
Owner

Yea this just seems like wrong behavior since we never really used namespaces. I'd be willing to merge a PR that fixes this.

@els0r
Copy link
Author

els0r commented Mar 6, 2023

Thanks for the feedback. I'll put something together soon.

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

No branches or pull requests

2 participants