Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

root: filter out useless commandConn.CloseWrite warning message #924

Merged
merged 2 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,26 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
}
}

logrus.AddHook(logutil.NewFilter(
logrus.SetFormatter(&logutil.Formatter{})
crazy-max marked this conversation as resolved.
Show resolved Hide resolved

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
}
Expand Down
6 changes: 4 additions & 2 deletions util/logutil/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions util/logutil/format.go
Original file line number Diff line number Diff line change
@@ -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
}