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

Change LogLevel without creating a new core #774

Closed
weeco opened this issue Dec 22, 2019 · 1 comment · Fixed by #775
Closed

Change LogLevel without creating a new core #774

weeco opened this issue Dec 22, 2019 · 1 comment · Fixed by #775

Comments

@weeco
Copy link

weeco commented Dec 22, 2019

I'd like to create a new subLogger which only logs messages at the WarnLevel instead of the InfoLevel (which is desired for the rest of the application). I read the other issues about doing so, but they implied creating a new zapcore.

I do not want to create a new zapcore for my sublogger, because my logger's core has hooks which should still be used for the sublogger. So is there any way to create a sublogger which reuses my existent zapcore but only emits log messages at the WarnLevel+?

stripeLogger := logger.
	With(zap.String("source", "stripe")).
	WithOptions(zap.WrapCore(/** Help **/))
stripe.Logger = zap.NewStdLog(stripeLogger)
@prashantv
Copy link
Collaborator

prashantv commented Dec 23, 2019

This is possible to do using WrapCore and a custom core wrapper, but we don't have an easy way to do it as part of zap today.

I've provided an example here:
https://play.golang.org/p/0uA7ck8mhKx

Given similar requests (#763), I think maybe we should include something as part of zap to simplify this, but would have to make it very clear that it can only ever be used to increase the log level (e.g., decrease the number of logs that would be logged).

I've created a WIP PR: #775

prashantv added a commit that referenced this issue Dec 23, 2019
We've had a few requests on how to change the level of a logger. With
the current APIs, it's not possible to reduce the log level (e.g., go
from Info to Debug), but it is possible to increase the level using
`WrapCore` with a custom filtering core.

Since it's a common request, I think we should add an option to make
this easy to use.

I want to make sure "increase" is part of the API to avoid confusion on
whether it can reduce the log level.

Fixes #774.
prashantv added a commit that referenced this issue Jan 24, 2020
We've had a few requests on how to change the level of a logger. With
the current APIs, it's not possible to reduce the log level (e.g., go
from Info to Debug), but it is possible to increase the level using
`WrapCore` with a custom filtering core.

Since it's a common request, I think we should add an option to make
this easy to use.

I want to make sure "increase" is part of the API to avoid confusion on
whether it can reduce the log level.

Fixes #774.
prashantv added a commit that referenced this issue Jan 30, 2020
We've had a few requests on how to change the level of a logger. With
the current APIs, it's not possible to reduce the log level (e.g., go
from Info to Debug), but it is possible to increase the level using
`WrapCore` with a custom filtering core.

Since it's a common request, I think we should add an option to make
this easy to use.

I want to make sure "increase" is part of the API to avoid confusion on
whether it can reduce the log level.

Fixes #774.
cgxxv pushed a commit to cgxxv/zap that referenced this issue Mar 25, 2022
We've had a few requests on how to change the level of a logger. With
the current APIs, it's not possible to reduce the log level (e.g., go
from Info to Debug), but it is possible to increase the level using
`WrapCore` with a custom filtering core.

Since it's a common request, I think we should add an option to make
this easy to use.

I want to make sure "increase" is part of the API to avoid confusion on
whether it can reduce the log level.

Fixes uber-go#774.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants