diff --git a/docs/md/melange_build.md b/docs/md/melange_build.md index f62061ff5..67e3df275 100644 --- a/docs/md/melange_build.md +++ b/docs/md/melange_build.md @@ -44,6 +44,7 @@ melange build [flags] --guest-dir string directory used for the build environment guest -h, --help help for build -k, --keyring-append strings path to extra keys to include in the build environment keyring + --log-policy strings logging policy to use (default [builtin:stderr]) --namespace string namespace to use in package URLs in SBOM (eg wolfi, alpine) (default "unknown") --out-dir string directory where packages will be output (default "./packages/") --overlay-binsh string use specified file as /bin/sh overlay in build environment diff --git a/pkg/build/build.go b/pkg/build/build.go index 78a3f4d7d..252965c98 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -31,6 +31,7 @@ import ( apko_build "chainguard.dev/apko/pkg/build" apko_oci "chainguard.dev/apko/pkg/build/oci" apko_types "chainguard.dev/apko/pkg/build/types" + apko_iocomb "chainguard.dev/apko/pkg/iocomb" apko_log "chainguard.dev/apko/pkg/log" "k8s.io/kube-openapi/pkg/util/sets" @@ -327,6 +328,7 @@ type Context struct { imgDigest name.Digest containerConfig *container.Config Debug bool + LogPolicy []string EnabledBuildOptions []string } @@ -342,17 +344,13 @@ type Dependencies struct { var ErrSkipThisArch = errors.New("error: skip this arch") func New(opts ...Option) (*Context, error) { - logger := &apko_log.Adapter{ - Out: os.Stderr, - Level: apko_log.InfoLevel, - } - ctx := Context{ WorkspaceIgnore: ".melangeignore", SourceDir: ".", OutDir: ".", CacheDir: "./melange-cache/", Arch: apko_types.ParseArchitecture(runtime.GOARCH), + LogPolicy: []string{"builtin:stderr"}, } for _, opt := range opts { @@ -361,6 +359,16 @@ func New(opts ...Option) (*Context, error) { } } + writer, err := apko_iocomb.Combine(ctx.LogPolicy) + if err != nil { + return nil, err + } + + logger := &apko_log.Adapter{ + Out: writer, + Level: apko_log.InfoLevel, + } + fields := apko_log.Fields{ "arch": ctx.Arch.ToAPK(), } @@ -725,6 +733,14 @@ func WithDebug(debug bool) Option { } } +// WithLogPolicy sets the logging policy to use during builds. +func WithLogPolicy(policy []string) Option { + return func(ctx *Context) error { + ctx.LogPolicy = policy + return nil + } +} + type ConfigurationParsingOption func(*configOptions) type configOptions struct { diff --git a/pkg/cli/build.go b/pkg/cli/build.go index ff2c4f429..887508a3d 100644 --- a/pkg/cli/build.go +++ b/pkg/cli/build.go @@ -53,6 +53,7 @@ func Build() *cobra.Command { var varsFile string var purlNamespace string var buildOption []string + var logPolicy []string var createBuildLog bool var debug bool @@ -89,6 +90,7 @@ func Build() *cobra.Command { build.WithEnabledBuildOptions(buildOption), build.WithCreateBuildLog(createBuildLog), build.WithDebug(debug), + build.WithLogPolicy(logPolicy), } if len(args) > 0 { @@ -128,6 +130,7 @@ func Build() *cobra.Command { cmd.Flags().StringVar(&purlNamespace, "namespace", "unknown", "namespace to use in package URLs in SBOM (eg wolfi, alpine)") cmd.Flags().StringSliceVar(&archstrs, "arch", nil, "architectures to build for (e.g., x86_64,ppc64le,arm64) -- default is all, unless specified in config") cmd.Flags().StringSliceVar(&buildOption, "build-option", []string{}, "build options to enable") + cmd.Flags().StringSliceVar(&logPolicy, "log-policy", []string{"builtin:stderr"}, "logging policy to use") cmd.Flags().StringSliceVarP(&extraKeys, "keyring-append", "k", []string{}, "path to extra keys to include in the build environment keyring") cmd.Flags().StringSliceVarP(&extraRepos, "repository-append", "r", []string{}, "path to extra repositories to include in the build environment") cmd.Flags().BoolVar(&createBuildLog, "create-build-log", false, "creates a package.log file containing a list of packages that were built by the command")