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

Function Name #716

Open
sourabhxyz opened this issue May 17, 2019 · 4 comments
Open

Function Name #716

sourabhxyz opened this issue May 17, 2019 · 4 comments

Comments

@sourabhxyz
Copy link

sourabhxyz commented May 17, 2019

Is there a way to modify configuration or some tweak in caller configuration so that I can as well have a function name along with filename and line. Currently I had written my own code to do it using runtime.

Thanks

@prashantv
Copy link
Collaborator

This doesn't exist today, zap can only add file:line. I think adding the function could be a useful option though.

@prashantv
Copy link
Collaborator

We'll be happy to accept a PR implementing this, thanks!

@wijayaerick
Copy link
Contributor

wijayaerick commented Jun 19, 2020

I'd like to implement this. This is my first time contributing to open source repo, so please bear with me.

Several API changes I think of:

  1. Add Function string field on zapcore.Entry
  2. Add FunctionKey string field on zapcore.EncoderConfig. With both changes in zapcore.Entry and zapcore.EncoderConfig, we can modify encoders EncodeEntry in console/json encoder to include the Function.
type Entry struct {
	...
	Caller     EntryCaller
	Function   string
	Stack      string
}

type EncoderConfig struct {
	...
	CallerKey     string `json:"callerKey" yaml:"callerKey"`
	FunctionKey   string `json:"functionKey" yaml:"functionKey"`
	StacktraceKey string `json:"stacktraceKey" yaml:"stacktraceKey"`
	...
}
  1. Changes in zap.Logger

We can introduce WithFunction(bool) Option and private field Logger.addFunction. This way, we can just add new condition on Logger.check(..). We have AddCallerSkip(int) Option to skip callers; I'm thinking that we want to reuse this to skip functions (instead of introducing AddFunctionSkip(int) Option, because we'll never want to skip them differently (if we skip caller by 2, we want to also skip function by 2).

  1. FunctionEncoder

I can't think about how we want to encode function names other than the name given by runtime. So, I think we won't need FunctionEncoder.

Would love to hear your opinion on this, thanks!
@prashantv

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

4 participants
@prashantv @wijayaerick @sourabhxyz and others