feat(logging): add --log-file to persist nerdctl's own log - #5147
Open
ekalinin wants to merge 1 commit into
Open
feat(logging): add --log-file to persist nerdctl's own log#5147ekalinin wants to merge 1 commit into
ekalinin wants to merge 1 commit into
Conversation
nerdctl only reports its diagnostics on the standard error, so nothing survives the process. When a container fails to be created there is no record left to look at, and containerd does not log the client side of the failure either. Add a global --log-file (also log_file in nerdctl.toml and $NERDCTL_LOG_FILE) that appends nerdctl's own log to a file, in addition to the standard error. Every terminal error funnels through log.L.Fatal in main(), so the failure that ends the command is recorded together with everything logged on the way there. Combine with --debug for a full trace. The output is attached as a logrus hook rather than by replacing Logger.Out with an io.MultiWriter: the formatter picks its output style by type-asserting Logger.Out to *os.File, so a MultiWriter would silently change the console format whenever the flag is used. The file is opened in append mode so concurrent invocations can share it, and it is left open on purpose since log.L.Fatal exits the process. Fixes containerd#4872 Signed-off-by: Eugene Kalinin <e.v.kalinin@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4872
What
Adds a global
--log-file(alsolog_fileinnerdctl.toml, and$NERDCTL_LOG_FILE) that appends nerdctl's own log to a file, in addition to the standard error. Empty by default, so nothing changes unless it is set.$ nerdctl --debug --log-file /var/log/nerdctl.log run --rm alpine trueWhy
#4872 asks for a way to keep a record of a failing nerdctl invocation: today the diagnostics only go to the standard error and are gone once the process exits, and containerd does not record the client side of a failed container creation either.
Everything nerdctl logs funnels through the standard logger, including the error that terminates the command:
SilenceErrors: trueon the root command means cobra prints nothing itself, the error returns tomain(), andmain()callslog.L.Fatal(err). Capturing that stream is therefore enough to capture the failure the issue is about, together with everything logged on the way there.Implementation notes
Logger.Outwith anio.MultiWriter. The text formatter picks its output style by type-assertingLogger.Outto*os.File, so a MultiWriter would silently downgrade the console output fromINFO[0000] msgtotime="..." level=info msg="..."for everyone enabling the flag. With a hook the console is left untouched and the file gets a stable, TTY-independent format.containerd/log, so nerdctl still does not import logrus directly.<RFC3339NanoFixed> <LEVEL> <message> [key="value" ...], fields sorted for stable output.O_CREATE|O_WRONLY|O_APPEND, mode0600, so concurrent invocations can share one file and a previous invocation is never truncated away. Rotation is deliberately left tologrotateor an equivalent - pulling in a rotation dependency for a CLI seemed disproportionate.log.L.Fatalcallsos.Exit, so a deferredClosewould not run, and the hook writes are unbuffered.--debug/--debug-full, so no new--log-levelflag is introduced.Not covered
buildctl, CNI plugins), which never comes back through the logger.binary://) is dispatched inxmainbefore cobra runs, so the flag does not apply there.Testing
pkg/logging/file_hook_test.go: record format and field ordering, append semantics, file mode, and thatLogger.Outis left alone.cmd/nerdctl/main_test.goTestLogFile: a failing invocation writes the error to the file, and two invocations both end up in it.Example output: