From 278f94a8b62052ed790bb1c6832268eb79a6d03d Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 21 Jan 2022 00:36:06 +0100 Subject: [PATCH 1/2] root: filter out useless commandConn.CloseWrite warning message Signed-off-by: CrazyMax --- commands/root.go | 14 +++++++++++++- util/logutil/filter.go | 6 ++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/commands/root.go b/commands/root.go index 7df8c4b7..c5f03097 100644 --- a/commands/root.go +++ b/commands/root.go @@ -28,12 +28,24 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman } } - logrus.AddHook(logutil.NewFilter( + logrus.AddHook(logutil.NewFilter([]logrus.Level{ + logrus.DebugLevel, + }, "serving grpc connection", "stopping session", "using default config store", )) + // filter out useless commandConn.CloseWrite warning message that can occur + // when listing builder instances with "buildx ls" for those that are + // unreachable: "commandConn.CloseWrite: commandconn: failed to wait: signal: killed" + // https://github.com/docker/cli/blob/3fb4fb83dfb5db0c0753a8316f21aea54dab32c5/cli/connhelper/commandconn/commandconn.go#L203-L214 + logrus.AddHook(logutil.NewFilter([]logrus.Level{ + logrus.WarnLevel, + }, + "commandConn.CloseWrite:", + )) + addCommands(cmd, dockerCli) return cmd } diff --git a/util/logutil/filter.go b/util/logutil/filter.go index 2e8f91d9..2e054f6e 100644 --- a/util/logutil/filter.go +++ b/util/logutil/filter.go @@ -7,22 +7,24 @@ import ( "github.com/sirupsen/logrus" ) -func NewFilter(filters ...string) logrus.Hook { +func NewFilter(levels []logrus.Level, filters ...string) logrus.Hook { dl := logrus.New() dl.SetOutput(ioutil.Discard) return &logsFilter{ + levels: levels, filters: filters, discardLogger: dl, } } type logsFilter struct { + levels []logrus.Level filters []string discardLogger *logrus.Logger } func (d *logsFilter) Levels() []logrus.Level { - return []logrus.Level{logrus.DebugLevel} + return d.levels } func (d *logsFilter) Fire(entry *logrus.Entry) error { From d3e56ea9d917eeee457b9b71409264938ea12409 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 25 Jan 2022 08:54:05 +0100 Subject: [PATCH 2/2] root: simple output format on logrus for parity with cli Signed-off-by: CrazyMax --- commands/root.go | 2 ++ util/logutil/format.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 util/logutil/format.go diff --git a/commands/root.go b/commands/root.go index c5f03097..bed7dece 100644 --- a/commands/root.go +++ b/commands/root.go @@ -28,6 +28,8 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman } } + logrus.SetFormatter(&logutil.Formatter{}) + logrus.AddHook(logutil.NewFilter([]logrus.Level{ logrus.DebugLevel, }, diff --git a/util/logutil/format.go b/util/logutil/format.go new file mode 100644 index 00000000..fcd73208 --- /dev/null +++ b/util/logutil/format.go @@ -0,0 +1,16 @@ +package logutil + +import ( + "fmt" + "strings" + + "github.com/sirupsen/logrus" +) + +type Formatter struct { + logrus.TextFormatter +} + +func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) { + return []byte(fmt.Sprintf("%s: %s\n", strings.ToUpper(entry.Level.String()), entry.Message)), nil +}