Commit 297a157
authored
fix: panic caused by nil Writer provided to Logger (#1228)
* fix: panic caused by nil Writer provided to Logger
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.
* Remove redundant types in function signatures
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 :)
* Change NewLoggerE test to use TempDir
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.
* Fix test to use os.MkdirTemp instead of os.TempDir
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!).
* Fix nil logger under error conditions
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.1 parent 1ea2f5a commit 297a157
2 files changed
+64
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
58 | 58 | | |
59 | 59 | | |
60 | 60 | | |
61 | | - | |
62 | | - | |
63 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
64 | 76 | | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
73 | 97 | | |
74 | | - | |
| 98 | + | |
75 | 99 | | |
76 | 100 | | |
77 | 101 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
17 | 45 | | |
18 | 46 | | |
19 | 47 | | |
| |||
0 commit comments