From 6251354ec8d4c2fb166703d5695455f14225ed74 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Fri, 25 Feb 2022 15:42:51 -0500 Subject: [PATCH] Implement log output redirection by setting a new logrus logger that writes to the requested output , instead of changing the behavior of the standard logrus logger. Rename WithLogOutput to WithLogger to make this new behavior clearer. Use non-global logger to avoid unexpected global log discarding Reported in https://github.com/GoogleContainerTools/kaniko/issues/1955 Changes in https://github.com/awslabs/amazon-ecr-credential-helper/pull/309 made it possible for callers depending on this code as a Go library to redirect log output to ioutil.Discard to avoid spurious log statements. Unfortunately, because in that change the logger being used was the shared global logger.StandardLogger(), redirecting its output to ioutil.Discard to quiet ecr-login also disabled all logging using the shared global logger (like Kaniko does). Signed-off-by: Jason Hall --- ecr-login/ecr.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ecr-login/ecr.go b/ecr-login/ecr.go index 2dad267d..461e3090 100644 --- a/ecr-login/ecr.go +++ b/ecr-login/ecr.go @@ -33,18 +33,28 @@ type ECRHelper struct { type Option func(*ECRHelper) +// WithClientFactory sets the ClientFactory used to make API requests. func WithClientFactory(clientFactory api.ClientFactory) Option { return func(e *ECRHelper) { e.clientFactory = clientFactory } } -func WithLogOutput(w io.Writer) Option { +// WithLogger sets a new logger instance that writes to the given writer, +// instead of the default writer which writes to stderr. +// +// This can be useful if callers want to redirect logging emitted by this tool +// to another location. +func WithLogger(w io.Writer) Option { return func(e *ECRHelper) { - e.logger.Out = w + logger := logrus.New() + logger.Out = w + e.logger = logger } } +// NewECRHelper returns a new ECRHelper with the given options to override +// default behavior. func NewECRHelper(opts ...Option) *ECRHelper { e := &ECRHelper{ clientFactory: api.DefaultClientFactory{},