diff --git a/cli-plugins/manager/hooks.go b/cli-plugins/manager/hooks.go index bee212702da4..e6496f062401 100644 --- a/cli-plugins/manager/hooks.go +++ b/cli-plugins/manager/hooks.go @@ -5,10 +5,10 @@ import ( "encoding/json" "strings" + "github.com/containerd/log" "github.com/docker/cli/cli-plugins/hooks" "github.com/docker/cli/cli/config" "github.com/docker/cli/cli/config/configfile" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -106,7 +106,7 @@ func invokeAndCollectHooks(ctx context.Context, cfg *configfile.ConfigFile, root var appended bool nextSteps, appended = appendNextSteps(nextSteps, processedHook) if !appended { - logrus.Debugf("Plugin %s responded with an empty hook message %q. Ignoring.", pluginName, string(hookReturn)) + log.G(ctx).Debugf("Plugin %s responded with an empty hook message %q. Ignoring.", pluginName, string(hookReturn)) } } return nextSteps diff --git a/cli-plugins/socket/socket.go b/cli-plugins/socket/socket.go index 9a76f7a7805c..413a58c5fd43 100644 --- a/cli-plugins/socket/socket.go +++ b/cli-plugins/socket/socket.go @@ -10,7 +10,7 @@ import ( "runtime" "sync" - "github.com/sirupsen/logrus" + "github.com/containerd/log" ) // EnvKey represents the well-known environment variable used to pass the @@ -32,7 +32,7 @@ func NewPluginServer(h func(net.Conn)) (*PluginServer, error) { if err != nil { return nil, err } - logrus.Trace("Plugin server listening on ", l.Addr()) + log.L.Logger.Trace("Plugin server listening on ", l.Addr()) if h == nil { h = func(net.Conn) {} @@ -98,7 +98,7 @@ func (pl *PluginServer) Close() error { if pl == nil { return nil } - logrus.Trace("Closing plugin server") + log.L.Logger.Trace("Closing plugin server") // Close connections first to ensure the connections get io.EOF instead // of a connection reset. pl.closeAllConns() diff --git a/cli/command/container/attach.go b/cli/command/container/attach.go index f31a13f27b86..05fd08d41d65 100644 --- a/cli/command/container/attach.go +++ b/cli/command/container/attach.go @@ -5,13 +5,13 @@ import ( "errors" "io" + "github.com/containerd/log" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" "github.com/moby/moby/api/types/container" "github.com/moby/moby/client" "github.com/moby/sys/signal" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -185,6 +185,6 @@ func resizeTTY(ctx context.Context, dockerCli command.Cli, containerID string) { // After the above resizing occurs, the call to MonitorTtySize below will handle resetting back // to the actual size. if err := MonitorTtySize(ctx, dockerCli, containerID, false); err != nil { - logrus.Debugf("Error monitoring TTY size: %s", err) + log.G(ctx).Debugf("Error monitoring TTY size: %s", err) } } diff --git a/cli/command/container/exec.go b/cli/command/container/exec.go index bd4f9bc6242c..99149ec2dc56 100644 --- a/cli/command/container/exec.go +++ b/cli/command/container/exec.go @@ -6,6 +6,7 @@ import ( "fmt" "io" + "github.com/containerd/log" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" @@ -13,7 +14,6 @@ import ( "github.com/docker/cli/opts" "github.com/moby/moby/api/types/container" "github.com/moby/moby/client" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -194,7 +194,7 @@ func interactiveExec(ctx context.Context, dockerCli command.Cli, execOptions *cl } if err := <-errCh; err != nil { - logrus.Debugf("Error hijack: %s", err) + log.G(ctx).Debugf("Error hijack: %s", err) return err } diff --git a/cli/command/container/hijack.go b/cli/command/container/hijack.go index 1ce40962c927..98e3a73ba2f5 100644 --- a/cli/command/container/hijack.go +++ b/cli/command/container/hijack.go @@ -7,11 +7,11 @@ import ( "runtime" "sync" + "github.com/containerd/log" "github.com/docker/cli/cli/command" "github.com/moby/moby/api/pkg/stdcopy" "github.com/moby/moby/client" "github.com/moby/term" - "github.com/sirupsen/logrus" ) // The default escape key sequence: ctrl-p, ctrl-q @@ -152,10 +152,10 @@ func (h *hijackedIOStreamer) beginOutputStream(restoreInput func()) <-chan error _, err = stdcopy.StdCopy(h.outputStream, h.errorStream, h.resp.Reader) } - logrus.Debug("[hijack] End of stdout") + log.G(context.TODO()).Debug("[hijack] End of stdout") if err != nil { - logrus.Debugf("Error receiveStdout: %s", err) + log.G(context.TODO()).Debugf("Error receiveStdout: %s", err) } outputDone <- err @@ -176,7 +176,7 @@ func (h *hijackedIOStreamer) beginInputStream(restoreInput func()) (doneC <-chan // messages will be in normal type. restoreInput() - logrus.Debug("[hijack] End of stdin") + log.G(context.TODO()).Debug("[hijack] End of stdin") if _, ok := err.(term.EscapeError); ok { detached <- err @@ -187,12 +187,12 @@ func (h *hijackedIOStreamer) beginInputStream(restoreInput func()) (doneC <-chan // This error will also occur on the receive // side (from stdout) where it will be // propagated back to the caller. - logrus.Debugf("Error sendStdin: %s", err) + log.G(context.TODO()).Debugf("Error sendStdin: %s", err) } } if err := h.resp.CloseWrite(); err != nil { - logrus.Debugf("Couldn't send EOF: %s", err) + log.G(context.TODO()).Debugf("Couldn't send EOF: %s", err) } close(inputDone) diff --git a/cli/command/container/run.go b/cli/command/container/run.go index 3c25630d6b78..eb4a9ddb2c21 100644 --- a/cli/command/container/run.go +++ b/cli/command/container/run.go @@ -9,6 +9,7 @@ import ( "strings" "syscall" + "github.com/containerd/log" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/completion" @@ -17,7 +18,6 @@ import ( "github.com/moby/moby/client" "github.com/moby/sys/signal" "github.com/moby/term" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -240,7 +240,7 @@ func runContainer(ctx context.Context, dockerCli command.Cli, runOpts *runOption return nil } - logrus.Debugf("Error hijack: %s", err) + log.G(ctx).Debugf("Error hijack: %s", err) return err } status := <-statusChan diff --git a/cli/command/container/signals.go b/cli/command/container/signals.go index 3a98c1962364..603d26d8e0df 100644 --- a/cli/command/container/signals.go +++ b/cli/command/container/signals.go @@ -5,9 +5,9 @@ import ( "os" gosignal "os/signal" + "github.com/containerd/log" "github.com/moby/moby/client" "github.com/moby/sys/signal" - "github.com/sirupsen/logrus" ) // ForwardAllSignals forwards signals to the container @@ -52,7 +52,7 @@ func ForwardAllSignals(ctx context.Context, apiClient client.ContainerAPIClient, Signal: sig, }) if err != nil { - logrus.Debugf("Error sending signal: %s", err) + log.G(ctx).Debugf("Error sending signal: %s", err) } } } diff --git a/cli/command/container/tty.go b/cli/command/container/tty.go index 6d57bf0e3460..7e7542b84945 100644 --- a/cli/command/container/tty.go +++ b/cli/command/container/tty.go @@ -8,10 +8,10 @@ import ( "runtime" "time" + "github.com/containerd/log" "github.com/docker/cli/cli/command" "github.com/moby/moby/client" "github.com/moby/sys/signal" - "github.com/sirupsen/logrus" ) // TODO(thaJeztah): split resizeTTYTo @@ -40,7 +40,7 @@ func resizeTTYTo(ctx context.Context, apiClient resizeClient, id string, height, } if err != nil { - logrus.Debugf("Error resize: %s\r", err) + log.G(ctx).Debugf("Error resize: %s\r", err) } return err } diff --git a/cli/command/container/utils.go b/cli/command/container/utils.go index 7943500e5c83..96264f6aa0f4 100644 --- a/cli/command/container/utils.go +++ b/cli/command/container/utils.go @@ -4,9 +4,9 @@ import ( "context" "errors" + "github.com/containerd/log" "github.com/moby/moby/api/types/container" "github.com/moby/moby/client" - "github.com/sirupsen/logrus" ) func waitExitOrRemoved(ctx context.Context, apiClient client.APIClient, containerID string, waitRemove bool) <-chan int { @@ -32,7 +32,7 @@ func waitExitOrRemoved(ctx context.Context, apiClient client.APIClient, containe return case result := <-waitRes.Result: if result.Error != nil { - logrus.Errorf("Error waiting for container: %v", result.Error.Message) + log.G(ctx).Errorf("Error waiting for container: %v", result.Error.Message) statusC <- 125 } else { statusC <- int(result.StatusCode) @@ -41,7 +41,7 @@ func waitExitOrRemoved(ctx context.Context, apiClient client.APIClient, containe if errors.Is(err, context.Canceled) { return } - logrus.Errorf("error waiting for container: %v", err) + log.G(ctx).Errorf("error waiting for container: %v", err) statusC <- 125 } }() diff --git a/cli/command/inspect/inspector.go b/cli/command/inspect/inspector.go index 526cfda9f8c0..b204167946a3 100644 --- a/cli/command/inspect/inspector.go +++ b/cli/command/inspect/inspector.go @@ -5,15 +5,16 @@ package inspect import ( "bytes" + "context" "encoding/json" "errors" "fmt" "io" "text/template" + "github.com/containerd/log" "github.com/docker/cli/cli" "github.com/docker/cli/templates" - "github.com/sirupsen/logrus" ) // Inspector defines an interface to implement to process elements @@ -93,7 +94,7 @@ func Inspect(out io.Writer, references []string, tmplStr string, getRef GetRefFu } if err := inspector.Flush(); err != nil { - logrus.Error(err) + log.G(context.TODO()).Error(err) } if err := errors.Join(errs...); err != nil { diff --git a/cli/command/plugin/create.go b/cli/command/plugin/create.go index 6d087612fb55..30e7067fba43 100644 --- a/cli/command/plugin/create.go +++ b/cli/command/plugin/create.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" + "github.com/containerd/log" "github.com/distribution/reference" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" @@ -15,7 +16,6 @@ import ( "github.com/moby/go-archive/compression" "github.com/moby/moby/api/types/plugin" "github.com/moby/moby/client" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -103,7 +103,7 @@ func runCreate(ctx context.Context, dockerCli command.Cli, options pluginCreateO comp := compression.None if options.compress { - logrus.Debugf("compression enabled") + log.G(ctx).Debugf("compression enabled") comp = compression.Gzip } diff --git a/cli/command/system/dial_stdio.go b/cli/command/system/dial_stdio.go index 62206af0edd8..eb6b417e4720 100644 --- a/cli/command/system/dial_stdio.go +++ b/cli/command/system/dial_stdio.go @@ -7,9 +7,9 @@ import ( "io" "os" + "github.com/containerd/log" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -75,10 +75,10 @@ func runDialStdio(ctx context.Context, dockerCli command.Cli) error { func copier(to halfWriteCloser, from halfReadCloser, debugDescription string) error { defer func() { if err := from.CloseRead(); err != nil { - logrus.Errorf("error while CloseRead (%s): %v", debugDescription, err) + log.G(context.TODO()).Errorf("error while CloseRead (%s): %v", debugDescription, err) } if err := to.CloseWrite(); err != nil { - logrus.Errorf("error while CloseWrite (%s): %v", debugDescription, err) + log.G(context.TODO()).Errorf("error while CloseWrite (%s): %v", debugDescription, err) } }() if _, err := io.Copy(to, from); err != nil { diff --git a/cli/compose/loader/loader.go b/cli/compose/loader/loader.go index b8f725cb1a9e..076c15155990 100644 --- a/cli/compose/loader/loader.go +++ b/cli/compose/loader/loader.go @@ -4,6 +4,7 @@ package loader import ( + "context" "errors" "fmt" "path" @@ -14,6 +15,7 @@ import ( "strings" "time" + "github.com/containerd/log" interp "github.com/docker/cli/cli/compose/interpolation" "github.com/docker/cli/cli/compose/schema" "github.com/docker/cli/cli/compose/template" @@ -27,7 +29,6 @@ import ( "github.com/google/shlex" "github.com/moby/moby/api/types/network" "github.com/moby/moby/client/pkg/versions" - "github.com/sirupsen/logrus" "go.yaml.in/yaml/v3" ) @@ -515,7 +516,7 @@ func expandUser(srcPath string, lookupEnv template.Mapping) string { if strings.HasPrefix(srcPath, "~") { home, ok := lookupEnv("HOME") if !ok { - logrus.Warn("cannot expand '~', because the environment lacks HOME") + log.G(context.TODO()).Warn("cannot expand '~', because the environment lacks HOME") return srcPath } return strings.Replace(srcPath, "~", home, 1) @@ -555,7 +556,7 @@ func LoadNetworks(source map[string]any, version string) (map[string]types.Netwo return nil, fmt.Errorf("network %s: network.external.name and network.name conflict; only use network.name", name) } if versions.GreaterThanOrEqualTo(version, "3.5") { - logrus.Warnf("network %s: network.external.name is deprecated in favor of network.name", name) + log.G(context.TODO()).Warnf("network %s: network.external.name is deprecated in favor of network.name", name) } nw.Name = nw.External.Name nw.External.Name = "" @@ -596,7 +597,7 @@ func LoadVolumes(source map[string]any, version string) (map[string]types.Volume return nil, fmt.Errorf("volume %s: volume.external.name and volume.name conflict; only use volume.name", name) } if versions.GreaterThanOrEqualTo(version, "3.4") { - logrus.Warnf("volume %s: volume.external.name is deprecated in favor of volume.name", name) + log.G(context.TODO()).Warnf("volume %s: volume.external.name is deprecated in favor of volume.name", name) } volume.Name = volume.External.Name volume.External.Name = "" @@ -657,7 +658,7 @@ func loadFileObjectConfig(name string, objType string, obj types.FileObjectConfi return obj, fmt.Errorf("%[1]s %[2]s: %[1]s.external.name and %[1]s.name conflict; only use %[1]s.name", objType, name) } if versions.GreaterThanOrEqualTo(details.Version, "3.5") { - logrus.Warnf("%[1]s %[2]s: %[1]s.external.name is deprecated in favor of %[1]s.name", objType, name) + log.G(context.TODO()).Warnf("%[1]s %[2]s: %[1]s.external.name is deprecated in favor of %[1]s.name", objType, name) } obj.Name = obj.External.Name obj.External.Name = "" diff --git a/cli/compose/loader/loader_test.go b/cli/compose/loader/loader_test.go index cb2c9f20a191..52974b090f89 100644 --- a/cli/compose/loader/loader_test.go +++ b/cli/compose/loader/loader_test.go @@ -5,15 +5,16 @@ package loader import ( "bytes" + "context" "os" "runtime" "sort" "testing" "time" + "github.com/containerd/log" "github.com/docker/cli/cli/compose/types" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/sirupsen/logrus" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" "gotest.tools/v3/skip" @@ -1379,9 +1380,9 @@ func TestLoadVolumesWarnOnDeprecatedExternalNameVersion34(t *testing.T) { func patchLogrus() (*bytes.Buffer, func()) { buf := new(bytes.Buffer) - out := logrus.StandardLogger().Out - logrus.SetOutput(buf) - return buf, func() { logrus.SetOutput(out) } + out := log.G(context.TODO()).Logger.Out + log.G(context.TODO()).Logger.SetOutput(buf) + return buf, func() { log.G(context.TODO()).Logger.SetOutput(out) } } func TestLoadVolumesWarnOnDeprecatedExternalNameVersion33(t *testing.T) { diff --git a/cli/config/configfile/file.go b/cli/config/configfile/file.go index fab3ed4cba13..03a72d9b4cb2 100644 --- a/cli/config/configfile/file.go +++ b/cli/config/configfile/file.go @@ -1,6 +1,7 @@ package configfile import ( + "context" "encoding/base64" "encoding/json" "errors" @@ -10,10 +11,10 @@ import ( "path/filepath" "strings" + "github.com/containerd/log" "github.com/docker/cli/cli/config/credentials" "github.com/docker/cli/cli/config/memorystore" "github.com/docker/cli/cli/config/types" - "github.com/sirupsen/logrus" ) // ConfigFile ~/.docker/config.json file info @@ -180,7 +181,7 @@ func (configFile *ConfigFile) Save() (retErr error) { _ = temp.Close() if retErr != nil { if err := os.Remove(temp.Name()); err != nil { - logrus.WithError(err).WithField("file", temp.Name()).Debug("Error cleaning up temp file") + log.G(context.TODO()).WithError(err).WithField("file", temp.Name()).Debug("Error cleaning up temp file") } } }() @@ -390,8 +391,7 @@ func (configFile *ConfigFile) GetAllCredentials() (map[string]types.AuthConfig, for registryHostname := range configFile.CredentialHelpers { newAuth, err := configFile.GetAuthConfig(registryHostname) if err != nil { - // TODO(thaJeztah): use context-logger, so that this output can be suppressed (in tests). - logrus.WithError(err).Warnf("Failed to get credentials for registry: %s", registryHostname) + log.G(context.TODO()).WithError(err).Warnf("Failed to get credentials for registry: %s", registryHostname) continue } auths[registryHostname] = newAuth diff --git a/cli/connhelper/commandconn/commandconn.go b/cli/connhelper/commandconn/commandconn.go index 8084a65328b1..c2da4a82821f 100644 --- a/cli/connhelper/commandconn/commandconn.go +++ b/cli/connhelper/commandconn/commandconn.go @@ -28,7 +28,7 @@ import ( "syscall" "time" - "github.com/sirupsen/logrus" + "github.com/containerd/log" ) // New returns net.Conn @@ -48,7 +48,7 @@ func New(ctx context.Context, cmd string, args ...string) (net.Conn, error) { ctx = context.WithoutCancel(ctx) c := commandConn{cmd: exec.CommandContext(ctx, cmd, args...)} // we assume that args never contains sensitive information - logrus.Debugf("commandconn: starting %s with %v", cmd, args) + log.G(ctx).Debugf("commandconn: starting %s with %v", cmd, args) c.cmd.Env = os.Environ() c.cmd.SysProcAttr = &syscall.SysProcAttr{} setPdeathsig(c.cmd) @@ -251,17 +251,17 @@ func (c *commandConn) RemoteAddr() net.Addr { } func (*commandConn) SetDeadline(t time.Time) error { - logrus.Debugf("unimplemented call: SetDeadline(%v)", t) + log.G(context.TODO()).Debugf("unimplemented call: SetDeadline(%v)", t) return nil } func (*commandConn) SetReadDeadline(t time.Time) error { - logrus.Debugf("unimplemented call: SetReadDeadline(%v)", t) + log.G(context.TODO()).Debugf("unimplemented call: SetReadDeadline(%v)", t) return nil } func (*commandConn) SetWriteDeadline(t time.Time) error { - logrus.Debugf("unimplemented call: SetWriteDeadline(%v)", t) + log.G(context.TODO()).Debugf("unimplemented call: SetWriteDeadline(%v)", t) return nil } @@ -285,7 +285,7 @@ type stderrWriter struct { } func (w *stderrWriter) Write(p []byte) (int, error) { - logrus.Debugf("%s%s", w.debugPrefix, string(p)) + log.G(context.TODO()).Debug(w.debugPrefix + string(p)) w.stderrMu.Lock() if w.stderr.Len() > 4096 { w.stderr.Reset() diff --git a/cli/context/tlsdata.go b/cli/context/tlsdata.go index 9a53d2fd0108..638012e8a189 100644 --- a/cli/context/tlsdata.go +++ b/cli/context/tlsdata.go @@ -1,11 +1,12 @@ package context import ( + "context" "fmt" "os" + "github.com/containerd/log" "github.com/docker/cli/cli/context/store" - "github.com/sirupsen/logrus" ) const ( @@ -62,7 +63,7 @@ func LoadTLSData(s store.Reader, contextName, endpointName string) (*TLSData, er case keyKey: tlsData.Key = data default: - logrus.Warnf("unknown file in context %s TLS bundle: %s", contextName, f) + log.G(context.TODO()).Warnf("unknown file in context %s TLS bundle: %s", contextName, f) } } return &tlsData, nil diff --git a/cli/debug/debug.go b/cli/debug/debug.go index 5ad1b0329190..4c64215afcb8 100644 --- a/cli/debug/debug.go +++ b/cli/debug/debug.go @@ -3,7 +3,7 @@ package debug import ( "os" - "github.com/sirupsen/logrus" + "github.com/containerd/log" "go.opentelemetry.io/otel" ) @@ -11,14 +11,14 @@ import ( // and makes the logger to log at debug level. func Enable() { _ = os.Setenv("DEBUG", "1") - logrus.SetLevel(logrus.DebugLevel) + _ = log.SetLevel("debug") } // Disable sets the DEBUG env var to false // and makes the logger to log at info level. func Disable() { _ = os.Setenv("DEBUG", "") - logrus.SetLevel(logrus.InfoLevel) + _ = log.SetLevel("info") } // IsEnabled checks whether the debug flag is set or not. @@ -36,5 +36,5 @@ var OTELErrorHandler otel.ErrorHandler = otel.ErrorHandlerFunc(func(err error) { if err == nil { return } - logrus.WithError(err).Debug("otel error") + log.L.WithError(err).Debug("otel error") }) diff --git a/cli/debug/debug_test.go b/cli/debug/debug_test.go index e8f156401f8a..1a00309b663d 100644 --- a/cli/debug/debug_test.go +++ b/cli/debug/debug_test.go @@ -4,20 +4,20 @@ import ( "os" "testing" - "github.com/sirupsen/logrus" + "github.com/containerd/log" ) func TestEnable(t *testing.T) { defer func() { - logrus.SetLevel(logrus.InfoLevel) + _ = log.SetLevel("info") }() t.Setenv("DEBUG", "") Enable() if os.Getenv("DEBUG") != "1" { t.Fatalf("expected DEBUG=1, got %s\n", os.Getenv("DEBUG")) } - if logrus.GetLevel() != logrus.DebugLevel { - t.Fatalf("expected log level %v, got %v\n", logrus.DebugLevel, logrus.GetLevel()) + if log.GetLevel() != log.DebugLevel { + t.Fatalf("expected log level %v, got %v\n", log.DebugLevel, log.GetLevel()) } } @@ -27,8 +27,8 @@ func TestDisable(t *testing.T) { if os.Getenv("DEBUG") != "" { t.Fatalf("expected DEBUG=\"\", got %s\n", os.Getenv("DEBUG")) } - if logrus.GetLevel() != logrus.InfoLevel { - t.Fatalf("expected log level %v, got %v\n", logrus.InfoLevel, logrus.GetLevel()) + if log.GetLevel() != log.InfoLevel { + t.Fatalf("expected log level %v, got %v\n", log.InfoLevel, log.GetLevel()) } } diff --git a/cli/flags/options.go b/cli/flags/options.go index 8c31a0f17216..080b039f9387 100644 --- a/cli/flags/options.go +++ b/cli/flags/options.go @@ -6,10 +6,10 @@ import ( "os" "path/filepath" + "github.com/containerd/log" "github.com/docker/cli/cli/config" "github.com/docker/go-connections/tlsconfig" "github.com/moby/moby/client" - "github.com/sirupsen/logrus" "github.com/spf13/pflag" ) @@ -166,14 +166,11 @@ func (o *ClientOptions) SetDefaultOptions(flags *pflag.FlagSet) { // SetLogLevel sets the logrus logging level func SetLogLevel(logLevel string) { - if logLevel != "" { - lvl, err := logrus.ParseLevel(logLevel) - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, "Unable to parse logging level:", logLevel) - os.Exit(1) - } - logrus.SetLevel(lvl) - } else { - logrus.SetLevel(logrus.InfoLevel) + if logLevel == "" { + logLevel = "info" + } + if err := log.SetLevel(logLevel); err != nil { + _, _ = fmt.Fprintln(os.Stderr, "Unable to parse logging level:", logLevel) + os.Exit(1) } } diff --git a/cli/streams/out.go b/cli/streams/out.go index 2383b08576d1..8cbe37801caf 100644 --- a/cli/streams/out.go +++ b/cli/streams/out.go @@ -1,11 +1,12 @@ package streams import ( + "context" "io" "os" + "github.com/containerd/log" "github.com/moby/term" - "github.com/sirupsen/logrus" ) // Out is an output stream to write normal program output. It implements @@ -42,7 +43,7 @@ func (o *Out) GetTtySize() (height uint, width uint) { } ws, err := term.GetWinsize(o.fd) if err != nil { - logrus.WithError(err).Debug("Error getting TTY size") + log.G(context.TODO()).WithError(err).Debug("Error getting TTY size") if ws == nil { return 0, 0 } diff --git a/cmd/docker/docker.go b/cmd/docker/docker.go index 5450ea05ec84..d6d485e8284e 100644 --- a/cmd/docker/docker.go +++ b/cmd/docker/docker.go @@ -11,6 +11,7 @@ import ( "syscall" "github.com/containerd/errdefs" + "github.com/containerd/log" "github.com/docker/cli/cli" pluginmanager "github.com/docker/cli/cli-plugins/manager" "github.com/docker/cli/cli-plugins/socket" @@ -21,7 +22,6 @@ import ( "github.com/docker/cli/cli/version" platformsignals "github.com/docker/cli/cmd/docker/internal/signals" "github.com/moby/moby/client/pkg/versions" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" "go.opentelemetry.io/otel" @@ -83,7 +83,7 @@ func dockerMain(ctx context.Context) error { if err != nil { return err } - logrus.SetOutput(dockerCli.Err()) + log.G(ctx).Logger.SetOutput(dockerCli.Err()) otel.SetErrorHandler(debug.OTELErrorHandler) return runDocker(ctx, dockerCli) diff --git a/cmd/docker/docker_test.go b/cmd/docker/docker_test.go index a4529cbb9db3..b787efd029bd 100644 --- a/cmd/docker/docker_test.go +++ b/cmd/docker/docker_test.go @@ -10,11 +10,11 @@ import ( "testing" "time" + "github.com/containerd/log" "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/commands" "github.com/docker/cli/cli/debug" platformsignals "github.com/docker/cli/cmd/docker/internal/signals" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" @@ -51,7 +51,7 @@ func TestClientDebugEnabled(t *testing.T) { err = cmd.PersistentPreRunE(cmd, []string{}) assert.NilError(t, err) assert.Check(t, is.Equal("1", os.Getenv("DEBUG"))) - assert.Check(t, is.Equal(logrus.DebugLevel, logrus.GetLevel())) + assert.Check(t, is.Equal(log.DebugLevel, log.GetLevel())) } var discard = io.NopCloser(bytes.NewBuffer(nil)) diff --git a/internal/oauth/manager/manager.go b/internal/oauth/manager/manager.go index 96deb61b1325..7ae1f1341805 100644 --- a/internal/oauth/manager/manager.go +++ b/internal/oauth/manager/manager.go @@ -9,6 +9,7 @@ import ( "os" "strings" + "github.com/containerd/log" "github.com/docker/cli/cli/config/credentials" "github.com/docker/cli/cli/config/types" "github.com/docker/cli/cli/streams" @@ -17,8 +18,6 @@ import ( "github.com/docker/cli/internal/registry" "github.com/docker/cli/internal/tui" "github.com/morikuni/aec" - "github.com/sirupsen/logrus" - "github.com/pkg/browser" ) @@ -85,12 +84,12 @@ var ErrDeviceLoginStartFail = errors.New("failed to start device code flow login func (m *OAuthManager) LoginDevice(ctx context.Context, w io.Writer) (*types.AuthConfig, error) { state, err := m.api.GetDeviceCode(ctx, m.audience) if err != nil { - logrus.Debugf("failed to start device code login: %v", err) + log.G(ctx).WithError(err).Debug("failed to start device code login") return nil, ErrDeviceLoginStartFail } if state.UserCode == "" { - logrus.Debugf("failed to start device code login: missing user code") + log.G(ctx).Debug("failed to start device code login: missing user code") return nil, ErrDeviceLoginStartFail } diff --git a/internal/registryclient/client.go b/internal/registryclient/client.go index 20f0eda11d7f..19e695c8edb2 100644 --- a/internal/registryclient/client.go +++ b/internal/registryclient/client.go @@ -6,13 +6,13 @@ import ( "net/http" "strings" + "github.com/containerd/log" "github.com/distribution/reference" manifesttypes "github.com/docker/cli/cli/manifest/types" "github.com/docker/distribution" distributionclient "github.com/docker/distribution/registry/client" registrytypes "github.com/moby/moby/api/types/registry" "github.com/opencontainers/go-digest" - "github.com/sirupsen/logrus" ) // RegistryClient is a client used to communicate with a Docker distribution @@ -78,14 +78,14 @@ func (c *client) MountBlob(ctx context.Context, sourceRef reference.Canonical, t lu, err := repo.Blobs(ctx).Create(ctx, distributionclient.WithMountFrom(sourceRef)) switch err.(type) { case distribution.ErrBlobMounted: - logrus.Debugf("mount of blob %s succeeded", sourceRef) + log.G(ctx).Debugf("mount of blob %s succeeded", sourceRef) return nil case nil: default: return fmt.Errorf("failed to mount blob %s to %s: %w", sourceRef, targetRef, err) } _ = lu.Cancel(ctx) - logrus.Debugf("mount of blob %s created", sourceRef) + log.G(ctx).Debugf("mount of blob %s created", sourceRef) return ErrBlobCreated{From: sourceRef, Target: targetRef} } diff --git a/internal/registryclient/fetcher.go b/internal/registryclient/fetcher.go index 42ef0dec5423..523ade33c4b3 100644 --- a/internal/registryclient/fetcher.go +++ b/internal/registryclient/fetcher.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" + "github.com/containerd/log" "github.com/distribution/reference" "github.com/docker/cli/cli/manifest/types" "github.com/docker/cli/internal/registry" @@ -18,7 +19,6 @@ import ( distclient "github.com/docker/distribution/registry/client" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" - "github.com/sirupsen/logrus" ) // fetchManifest pulls a manifest from a registry and returns it. An error @@ -216,7 +216,7 @@ func continueOnError(err error) bool { } func (c *client) iterateEndpoints(ctx context.Context, namedRef reference.Named, each func(context.Context, distribution.Repository, reference.Named) (bool, error)) error { - endpoints, err := allEndpoints(namedRef, c.insecureRegistry) + endpoints, err := allEndpoints(ctx, namedRef, c.insecureRegistry) if err != nil { return err } @@ -228,7 +228,7 @@ func (c *client) iterateEndpoints(ctx context.Context, namedRef reference.Named, for _, endpoint := range endpoints { if endpoint.URL.Scheme != "https" { if _, confirmedTLS := confirmedTLSRegistries[endpoint.URL.Host]; confirmedTLS { - logrus.Debugf("skipping non-TLS endpoint %s for host/port that appears to use TLS", endpoint.URL) + log.G(ctx).Debugf("skipping non-TLS endpoint %s for host/port that appears to use TLS", endpoint.URL) continue } } @@ -243,7 +243,7 @@ func (c *client) iterateEndpoints(ctx context.Context, namedRef reference.Named, } repo, err := c.getRepositoryForReference(ctx, namedRef, repoEndpoint) if err != nil { - logrus.Debugf("error %s with repo endpoint %+v", err, repoEndpoint) + log.G(ctx).Debugf("error %s with repo endpoint %+v", err, repoEndpoint) var protoErr httpProtoError if errors.As(err, &protoErr) { continue @@ -252,7 +252,7 @@ func (c *client) iterateEndpoints(ctx context.Context, namedRef reference.Named, } if endpoint.URL.Scheme == "http" && !c.insecureRegistry { - logrus.Debugf("skipping non-tls registry endpoint: %s", endpoint.URL) + log.G(ctx).Debugf("skipping non-tls registry endpoint: %s", endpoint.URL) continue } done, err := each(ctx, repo, namedRef) @@ -261,46 +261,35 @@ func (c *client) iterateEndpoints(ctx context.Context, namedRef reference.Named, if endpoint.URL.Scheme == "https" { confirmedTLSRegistries[endpoint.URL.Host] = true } - logrus.Debugf("continuing on error (%T) %s", err, err) + log.G(ctx).Debugf("continuing on error (%T) %s", err, err) continue } - logrus.Debugf("not continuing on error (%T) %s", err, err) + log.G(ctx).Debugf("not continuing on error (%T) %s", err, err) return err } if done { return nil } } - return newNotFoundError(namedRef.String()) + return notFoundError{errors.New("no such manifest: " + namedRef.String())} } // allEndpoints returns a list of endpoints ordered by priority (v2, http). -func allEndpoints(namedRef reference.Named, insecure bool) ([]registry.APIEndpoint, error) { +func allEndpoints(ctx context.Context, namedRef reference.Named, insecure bool) ([]registry.APIEndpoint, error) { var serviceOpts registry.ServiceOptions if insecure { - logrus.Debugf("allowing insecure registry for: %s", reference.Domain(namedRef)) + log.G(ctx).Debugf("allowing insecure registry for: %s", reference.Domain(namedRef)) serviceOpts.InsecureRegistries = []string{reference.Domain(namedRef)} } registryService, err := registry.NewService(serviceOpts) if err != nil { return nil, err } - endpoints, err := registryService.Endpoints(context.TODO(), reference.Domain(namedRef)) - logrus.Debugf("endpoints for %s: %v", namedRef, endpoints) + endpoints, err := registryService.Endpoints(ctx, reference.Domain(namedRef)) + log.G(ctx).Debugf("endpoints for %s: %v", namedRef, endpoints) return endpoints, err } -func newNotFoundError(ref string) *notFoundError { - return ¬FoundError{err: errors.New("no such manifest: " + ref)} -} - -type notFoundError struct { - err error -} - -func (n *notFoundError) Error() string { - return n.err.Error() -} +type notFoundError struct{ error } -// NotFound satisfies interface github.com/docker/docker/errdefs.ErrNotFound func (notFoundError) NotFound() {} diff --git a/opts/swarmopts/port.go b/opts/swarmopts/port.go index 839d058f335e..cb20e7bbc04f 100644 --- a/opts/swarmopts/port.go +++ b/opts/swarmopts/port.go @@ -4,6 +4,7 @@ package swarmopts import ( + "context" "encoding/csv" "errors" "fmt" @@ -12,10 +13,10 @@ import ( "strconv" "strings" + "github.com/containerd/log" "github.com/docker/go-connections/nat" "github.com/moby/moby/api/types/network" "github.com/moby/moby/api/types/swarm" - "github.com/sirupsen/logrus" ) const ( @@ -163,8 +164,7 @@ func ConvertPortToPortConfig( ports := make([]swarm.PortConfig, 0, len(portBindings)) for _, binding := range portBindings[nat.Port(portProto.String())] { if p := net.ParseIP(binding.HostIP); p != nil && !p.IsUnspecified() { - // TODO(thaJeztah): use context-logger, so that this output can be suppressed (in tests). - logrus.Warnf("ignoring IP-address (%s:%s) service will listen on '0.0.0.0'", net.JoinHostPort(binding.HostIP, binding.HostPort), portProto.String()) + log.G(context.TODO()).Warnf("ignoring IP-address (%s:%s) service will listen on '0.0.0.0'", net.JoinHostPort(binding.HostIP, binding.HostPort), portProto.String()) } pr, err := network.ParsePortRange(binding.HostPort) diff --git a/opts/swarmopts/port_test.go b/opts/swarmopts/port_test.go index 5c283f2b7a33..1c58611089b0 100644 --- a/opts/swarmopts/port_test.go +++ b/opts/swarmopts/port_test.go @@ -2,13 +2,14 @@ package swarmopts import ( "bytes" + "context" "os" "testing" + "github.com/containerd/log" "github.com/docker/go-connections/nat" "github.com/moby/moby/api/types/network" "github.com/moby/moby/api/types/swarm" - "github.com/sirupsen/logrus" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" ) @@ -352,7 +353,8 @@ func TestConvertPortToPortConfigWithIP(t *testing.T) { } var b bytes.Buffer - logrus.SetOutput(&b) + log.G(context.TODO()).Logger.SetOutput(&b) + defer log.G(context.TODO()).Logger.SetOutput(os.Stderr) for _, tc := range testCases { t.Run(tc.value, func(t *testing.T) { _, err := ConvertPortToPortConfig(network.MustParsePort("80/tcp"), map[nat.Port][]nat.PortBinding{ @@ -366,7 +368,6 @@ func TestConvertPortToPortConfigWithIP(t *testing.T) { } }) } - logrus.SetOutput(os.Stderr) } func assertContains(t *testing.T, portConfigs []swarm.PortConfig, expected swarm.PortConfig) { diff --git a/vendor.mod b/vendor.mod index d22e3ddd8a1d..5f0db6c7a622 100644 --- a/vendor.mod +++ b/vendor.mod @@ -42,7 +42,6 @@ require ( github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/image-spec v1.1.1 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c - github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.10.2 github.com/spf13/pflag v1.0.10 github.com/tonistiigi/go-rosetta v0.0.0-20220804170347-3f4430f2d346 @@ -92,6 +91,7 @@ require ( github.com/prometheus/common v0.62.0 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect go.etcd.io/etcd/raft/v3 v3.5.16 // indirect