Repository navigation
Let consumers capture or redirect loading warnings with a Logger - #954
Merged
Merged
Conversation
Add a logging.Logger contract and a template.WithLogger option. Without the option, the warning is still written through the standard logrus logger, unchanged. Default values and error messages carried by a substitution, as well as the text following a braced expression, report to the same logger as the outer substitution. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
Options.Logger, when set, receives the warnings raised by the loader: obsolete version attribute, YAML 1.1 booleans, unset variables during interpolation and misconfigured resource loaders. It is inherited by the files loaded through include and extends. When nil, warnings keep going through the standard logrus logger, with the same messages. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
WithLogger sends to a logging.Logger the warnings raised by the file discovery options (several default config or override files, an inaccessible default .env file, a project name taken from a symlink), and hands the logger to the loader. Without it, those warnings keep going through the standard logrus logger, unchanged. Signed-off-by: Nicolas De Loof <nicolas.deloof@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.
What this PR does, in one sentence
Lets a consumer of compose-go capture or redirect the warnings emitted while loading a project, through a single optional
Loggerthat defaults to today's behavior.Context
compose-go reports obsolete
versionattributes, YAML 1.1 booleans, unset variables, ambiguous default file discovery and similar issues through the global logrus logger. A library embedding compose-go (Docker Compose is one example) can neither collect these warnings to surface them its own way, nor keep them out of its output, nor route them to the logger it already uses, except by mutating global logrus state.What the PR brings
A one-method contract,
logging.Logger:and one way to provide it at each level:
loader.Options.Logger,cli.WithLogger(l)(which also hands it to the loader), andtemplate.WithLogger(l)for callers using the template package directly. The warnings of the main loading path reach it: obsoleteversion, YAML 1.1 booleans (during interpolation and when decoding the model), unset variables (including inside default values and error messages, and in files loaded throughincludeandextends), a misplaced local resource loader, multiple default config or override files, an inaccessible default.env, and a project name taken from a symlinked directory.Guardrails:
goreleasereports only additions:loggingpackage,Options.Logger,cli.WithLogger,template.WithLogger). No new exported mutable variable.Warnfinterface is satisfied as-is by*logrus.Logger(so Docker Compose can pass its logger unchanged) and by zap's sugared logger, and alog/slogadapter is a three-line method.*slog.Loggerwas not retained as the contract because it would force logrus-based consumers to write a handler bridge to keep their current output, and the messages are plain sentences with no useful structure to carry. A bare callback was not retained because an interface is the idiomatic, self-documenting shape for a logger and lets consumers pass their logger directly.loader.Options,cli.ProjectOptions,template.Config); the interpolation and type-cast functions built byloader.ToOptionsare bound to the options, sointerpolation.Optionsis untouched. Options set throughcli.WithLoggerapply in order, so it must precede options that may warn, such asWithDefaultConfigPath(documented).Known limits, deliberately not forced into this PR because routing them needs a wider refactoring or new exported API:
external.name is deprecatedwarning (transform, whose canonicalizers are a registry of fixed-signature functions);paths.ExpandUser("cannot expand '~'");.env/env_filefiles (dotenvhas no options object, its parsing functions are exported with fixed signatures);x-*extension values andincludeentries decoded through the exportedloader.Transform.These keep going through logrus. Also, the obsolete
versionwarning remains emitted once per file for the whole process, as before, whichever logger the first load used.loader/loader.gois touched in small, localized spots (theOptionsstruct,ToOptions,clone, two warning calls), to limit conflicts with the pending refactoring of the same file in #948.🤖 Generated with Claude Code