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

Support for convenience functions #110

Open
stephenmw opened this issue Nov 1, 2016 · 1 comment
Open

Support for convenience functions #110

stephenmw opened this issue Nov 1, 2016 · 1 comment

Comments

@stephenmw
Copy link

I am new to log15 and currently evaluating it for a new project. One of the first things I did was make a convenience functions that would allow logging from contexts (in an http server). Example:

func Error(ctx context.Context, msg string, logCtx ...interface{}) {
	FromContext(ctx).Error(msg, logCtx...)
}

The problem is that the convenience function is the caller of logger.Error and therefore anything relating to the file and line numbers is less than useful. Now, if I wanted to, I could create a handler that fixes this, but the problem is I don't know that logging will always be done from one of my convenience functions. In fact, I expect it to often not be. I propose the following method be added to log15.Logger interface:

Write(msg string, lvl Lvl, ctx []interface{}, call stack.Call)

This would allow my convenience functions to work while not breaking anything. I would be happy to submit a PR if you agree to the change. Also, wording is open to change.

@stumo
Copy link

stumo commented Nov 9, 2018

I've got similar issues - I want to use log15 for a library that accepts a Func(string) as its logging function, so at the moment all my logs are coming out with the wrapper function's location.

I think that if the existing stack depth can be exposed, then most of what you want can be dealt with by creating a new logger. E.g.

loggerWithAdjustedStackDepth := log.New()
loggerWithAdjustedStackDepth.SetStackDepth(log.GetStackDepth() + 1)

loggerToPassToLibrary := func(s string){

loggerWithAdjustedStackDepth.Debug(s)
}

Is there any reason not to allow getting the current stack depth?

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