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

Manipulate the function name printed in the output at runtime #1467

Open
gandarez opened this issue Nov 8, 2024 · 2 comments
Open

Manipulate the function name printed in the output at runtime #1467

gandarez opened this issue Nov 8, 2024 · 2 comments

Comments

@gandarez
Copy link

gandarez commented Nov 8, 2024

Is your feature request related to a problem? Please describe.

Currently there's no way to change the function name printed in the output when FunctionKey is set. Instead of printing the full path like github.com/wakatime/wakatime-cli/cmd.RunE, I'd like to manipulate this output and replace by cmd.RunE.

{"level":"debug","now":"2024-11-08T11:58:54-03:00","caller":"cmd/run.go:141","func":"github.com/wakatime/wakatime-cli/cmd.RunE","message":"command: heartbeat"}

Describe the solution you'd like

I thought I could add a new option like func WithFunctionNameReplacer(fn func(function string) string) Option that would set this cutomized function in logger struct and may be called at check() where the function name is captured.

if log.addCaller {
	funcName := frame.Function

	if log.nameReplacerFunc != nil {
		funcName = log.nameReplacerFunc(funcName)
	}

	ce.Caller = zapcore.EntryCaller{
		Defined:  frame.PC != 0,
		PC:       frame.PC,
		File:     frame.File,
		Line:     frame.Line,
		Function: funcName,
	}
}

Then the result will be

{"level":"debug","now":"2024-11-08T12:42:07-03:00","caller":"cmd/run.go:141","func":"cmd.RunE","message":"command: heartbeat"}

Describe alternatives you've considered

Could not find any alternative.

Is this a breaking change?

No

Additional context

Does that make sense? If so, I'd be happy to make a PR.

@sywhang
Copy link
Contributor

sywhang commented Nov 12, 2024

Hello,

Thank you for submitting an issue on this. Before we discuss further, may I ask why this is useful? Seems like replacing the function name here is a strict degradation on finding the logging site, so I'm wondering in what use case doing this would be necessary.

@gandarez
Copy link
Author

@sywhang my idea here is to find a way to simplify the function name printed. Clearly it's not the best to have the full name as Go's runtime return because it considers the long full path.

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

2 participants