Do not log in log library#1364
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: bobcatfish Assign the PR to them by writing The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
squinty eyes |
| proxy.ServeHTTP(w, r) | ||
| } | ||
|
|
||
| func _initializeLogging() *zap.SugaredLogger { |
There was a problem hiding this comment.
I'd suggest dropping the underscore since it's not really a style I see in go
There was a problem hiding this comment.
face palm my bad!
| logger.Error("Failed to parse the logging config. Falling back to default logger.", zap.Error(err), zap.String(logkey.JSONConfig, configJSON)) | ||
| return logger.Sugar() | ||
| errStr := fmt.Sprintf("Failed to parse the logging config. Falling back to default logger. Error: %s. Config: %s", err, configJSON) | ||
| return logger.Sugar(), errors.New(errStr) |
There was a problem hiding this comment.
fmt.Errorf will combine your fmt.Sprintf and errors.New
|
|
||
| func _initializeLogging() *zap.SugaredLogger { | ||
| configJSON, levelOverride := logging.LoggerDefaultConfigMap("loglevel.activator") | ||
| genericLogger, err := logging.NewLogger(configJSON, levelOverride) |
There was a problem hiding this comment.
Maybe we could simplify the interface a bit more so we only make one call to the logging pkg to instantiate a configured logger.
ie.
logger, err := logging.NewLoggerFromConfig("loglevel.activator")
then maybe _initializeLogging is small enough to inline it into main() ?
There was a problem hiding this comment.
I'd like to change the assumption this package makes about the config being mounted at /etc/config-logging as well. @mdemirhan thoughts?
There was a problem hiding this comment.
I am all for removing the dependency on the etc/config-logging. It was meant as a helper to reduce the copy/paste between all main.go files.
@bobcatfish The changes look good to me. The duplication hurts the eyes, but if it is going to make things better for tests, I am all for it. Can the new "initializeLogging" not be generalized enough so that we avoid duplication? Alternatively, passing a "do not log" flag to the existing NewLoggerFromXXX method could achieve the same thing and tests can use that flag when initializing its loggers.
e7f10da to
4cd6bc4
Compare
| } | ||
| defer logger.Sync() | ||
| return logger | ||
| >>>>>>> Do not log in log library |
4cd6bc4 to
441ef04
Compare
| } | ||
| } | ||
|
|
||
| func writeConfig(dir, key, value string) { |
There was a problem hiding this comment.
copied @josephburnett 's unit tests for k8sflag here ;)
|
Okay so:
@mdemirhan @mattmoor @dprotaso PTAL |
|
|
||
| // DefaultLoggingConfigPath is the path where the zap logging | ||
| // config will be written in a default Knative deployment. | ||
| const DefaultLoggingConfigPath = "/etc/config-logging" |
There was a problem hiding this comment.
@mattmoor let me know if you wanted me to do something more specific re 'not relying on /etc/config-logging'
| // No good way to test if all the config is applied, | ||
| // but at the minimum, we can check and see if level is getting applied. | ||
| logger = NewLogger("{\"level\": \"error\", \"outputPaths\": [\"stdout\"],\"errorOutputPaths\": [\"stderr\"],\"encoding\": \"json\"}", "") | ||
| logger, err := NewLogger("{\"level\": \"error\", \"outputPaths\": [\"stdout\"],\"errorOutputPaths\": [\"stderr\"],\"encoding\": \"json\"}", "") |
There was a problem hiding this comment.
since you're changing a few NewLogger calls in these tests can you clean up all the escaping by using raw strings
|
|
||
| logger, err := NewDefaultConfigMapLogger(dir, "myfoo") | ||
| if err == nil { | ||
| t.Error("expecte error when creating logger from invalid config file") |
| func TestNewDefaultConfigMapLogger(t *testing.T) { | ||
| dir, err := ioutil.TempDir("", "") | ||
| if err != nil { | ||
| panic(err) |
There was a problem hiding this comment.
using t.Fatal in these tests instead of panic will cause this to log to the test output buffer
There was a problem hiding this comment.
oh yeah, way better thanks!
| }` | ||
| configJSON := fmt.Sprintf(configJSONTemplate, logLevel) | ||
| return logging.NewLogger(string(configJSON), logLevel) | ||
| logger, err := logging.NewLogger(string(configJSON), logLevel) |
There was a problem hiding this comment.
with the line change can you remove the unnecessary cast string(configJSON)
There was a problem hiding this comment.
nice catch, thanks!
| // NewDefaultConfigMapLogger creates a logging object for the | ||
| // for the logLevelKey within configPath corresponding | ||
| // to the provided componentKey. | ||
| func NewDefaultConfigMapLogger(configPath string, componentKey string) (*zap.SugaredLogger, error) { |
There was a problem hiding this comment.
Given there is a default config path we can simplify the usability of this function a bit more by making configPath optional. Maybe it makes sense to name this function NewComponentLogger now?
ie.
func NewComponentLogger(componentKey string, configPath ...string) (*zap.SugaredLogger, error) {
conf := DefaultLoggingConfigPath
if len(configPath) > 0 {
conf = configPath[0]
}
...
}
then the usage would simplified to
if logger, err := logging.NewComponentLogger("webhook"); err != nil {
logger.Errorf("Error initializing logger: %s", err)
}
There was a problem hiding this comment.
thanks for letting me know about this! i didn't know about variadic functions in Go!
however after looking into it a bit, I'm not sure I like the idea of using them for this optional params and here is an example of why. The TL;DidntRun is the fact that using a variadic args means callers can start passing you incorrect arguments have you have to either check and make sure they're not doing that explicitly or allow the weird extra arguments to silently not do anything, and neither option seems great
There was a problem hiding this comment.
Yup it's dirty - would you settle on having two New* functions
ie.
func NewComponentLogger(componentKey string) (*zap.SugaredLogger, error) {
return NewComponentLoggerFromConfig(componentKey, DefaultLoggingConfigPath)
}
func NewComponentLoggerFromConfig(componentKey, configPath string) (*zap.SugaredLogger, error) {
...
}441ef04 to
3dd0776
Compare
Okay this is slightly more repeated code, but hear me out! When you log in a library, it means the caller has no control over whether those logs are emitted or not - case in point, the e2e tests are using this library, and now they dump the json config and log level at the beginning of every test, which we don't want and can't control. This commit changes the `logging` lib such that it's the caller's responsibility to decide whether to log or not. This also changes the logic such that the logging configuration is no longer logged at debug.
3dd0776 to
274973a
Compare
|
Ready for another round! PTAL @dprotaso @mattmoor @mdemirhan |
|
The following is the coverage report on pkg/. Say
*TestCoverage feature is being tested, do not rely on any info here yet |
|
/test pull-knative-serving-integration-tests |
|
@bobcatfish: The following test failed, say
Full PR test history. Your PR dashboard. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
|
Oh neat I think I actually broke something :D AND THE TESTS CAUGHT IIIIIT |
|
@bobcatfish FYI, my change to refactor the way we load things (to not be in the library!) landed. LMK when this is RFAL. |
|
/hold Wait until after we cut first release. @bobcatfish Are you going to come back to this PR? |
|
I'm going to close this out. Feel free to reopen, if it's still relevant. |
|
Yeah looks like I'm not going to be able to get back to this, esp. since the logging libs have changed significantly since I've last looked, thanks for closing it @mattmoor I HAVE FAILED YOU @jonjohnsonjr :'( I've created #1630 so hopefully someone can look into this in the future. |

Okay this is a lot repeated code, but hear me out! When you log in
a library, it means the caller has no control over whether those
logs are emitted or not - case in point, the e2e tests are using this
library, and now they dump the json config and log level at the
beginning of every test, which we don't want and can't control.
This commit changes the
logginglib such that it's the caller'sresponsibility to decide whether to log or not.
zapdirectly instead of using the knativelogginglib.)This is what the test output looks like before this change, in verbose mode:

In the default mode:

Proposed Changes