-
Notifications
You must be signed in to change notification settings - Fork 260
fix: panic caused by nil Writer provided to Logger #1228
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
Conversation
log/logger_test.go
Outdated
|
|
||
| // confirm the assumptions of this test before we run it: | ||
| if _, err := os.Stat(fullLogPath); err == nil { | ||
| t.Skipf("The log file at %q exists, so this test cannot sensibly run. Delete it first", fullLogPath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it be better to use a relative path and always delete it during test setup?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rbtr I like the general idea. I did a bit more research and found this SO post. The Go equivalent is os.TempDir which also does the right thing on Linux and Windows, so I'm inclined to use it. Patch forthcoming :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on os.TempDir
In the log.(*Logger).Init method, several different strategies are considered based on the configuration provided by the user. In one common case, a rotating log file is opened and the log output is tee-ed into it. If there is any issue with opening that log file, the `log.(*Log).SetOutput` method is never invoked on the underlying logger. Since this standard library logger was created with `nil` passed as the `io.Writer`, this results in a nil pointer deref panic. The error causing the issue is also not surfaced to the user. This initializes the standard library logger with the `io.Discard` writer. This adheres to the original intention here, while also being a safer alternative to nil (effectively writes are to /dev/null). Also, the error is written out to stderr, in the hopes that an operator will be able to diagnose and fix the issue (in the absense of a correct logger configuration). Finally, the logger initialization has been moved to a new function, `log.NewLoggerE` that properly exposes the underlying error. The original function, `NewLogger`, has been modified to use this function instead and has been marked Deprecated.
|
/azp run |
The linter correctly pointed out that the types are redundant here. While ordinarily I tend to leave them if the ellision appears in the middle of the signature, I respect the linter's decision :)
|
Azure Pipelines successfully started running 1 pipeline(s). |
Rather than trying to pull a non-existant path out of thin air and hoping that it doesn't exist, this instead uses os.TempDir to create a guaranteed-empty directory. From within the newly-created tempdir we try to use a non-existent path. This is better both on a single platform, but also has the benefit of working well on Windows too.
5bbf6ec to
6abe071
Compare
rbtr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deleting the wrong thing in the test, i think
rbtr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forgot to include the comment 🙃
The intention of this test was to create a new temporary directory rather than use the system's temporary directory. This mistake was pointed out by @rbtr in a code review (thanks!).
When an error is present, we want to return both the error and the logger to guarantee that a usable logger is always present. This returns the logger in all circumstances and documents that fact in the godoc for this function.
|
/azp run |
|
Commenter does not have sufficient privileges for PR 1228 in repo Azure/azure-container-networking |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
rbtr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Reason for Change:
In the log.(*Logger).Init method, several different strategies are
considered based on the configuration provided by the user. In one
common case, a rotating log file is opened and the log output is tee-ed
into it. If there is any issue with opening that log file, the
log.(*Log).SetOutputmethod is never invoked on the underlying logger.Since this standard library logger was created with
nilpassed as theio.Writer, this results in a nil pointer deref panic. The errorcausing the issue is also not surfaced to the user.
This initializes the standard library logger with the
io.Discardwriter. This adheres to the original intention here, while also being a
safer alternative to nil (effectively writes are to /dev/null). Also,
the error is written out to stderr, in the hopes that an operator will
be able to diagnose and fix the issue (in the absense of a correct
logger configuration).
Finally, the logger initialization has been moved to a new
function,
log.NewLoggerEthat properly exposes the underlying error.The original function,
NewLogger, has been modified to use thisfunction instead and has been marked Deprecated.
Issue Fixed:
Fixes #1227
Requirements: