diff --git a/cmd/buildctl/main.go b/cmd/buildctl/main.go index 08a2b8114576e..e7f158dc9a4b8 100644 --- a/cmd/buildctl/main.go +++ b/cmd/buildctl/main.go @@ -19,6 +19,7 @@ import ( _ "github.com/moby/buildkit/util/tracing/detect/jaeger" _ "github.com/moby/buildkit/util/tracing/env" "github.com/moby/buildkit/version" + "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/urfave/cli" "go.opentelemetry.io/otel" @@ -57,6 +58,12 @@ func main() { Usage: "buildkitd address", Value: defaultAddress, }, + // Add format flag to control log formatter + cli.StringFlag{ + Name: "log-format", + Usage: "log formatter: json or text", + Value: "text", + }, cli.StringFlag{ Name: "tlsservername", Usage: "buildkitd server name for certificate validation", @@ -106,8 +113,16 @@ func main() { app.Before = func(context *cli.Context) error { debugEnabled = context.GlobalBool("debug") - - logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true}) + // Use Format flag to control log formatter + logFormat := context.GlobalString("log-format") + switch logFormat { + case "json": + logrus.SetFormatter(&logrus.JSONFormatter{}) + case "text", "": + logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true}) + default: + return errors.Errorf("unsupported log type %q", logFormat) + } if debugEnabled { logrus.SetLevel(logrus.DebugLevel) } diff --git a/cmd/buildkitd/config/config.go b/cmd/buildkitd/config/config.go index a92588e53f979..7749b64478ff1 100644 --- a/cmd/buildkitd/config/config.go +++ b/cmd/buildkitd/config/config.go @@ -27,6 +27,9 @@ type Config struct { DNS *DNSConfig `toml:"dns"` History *HistoryConfig `toml:"history"` + + // LogFormat is the format of the logs. It can be "json" or "text". + LogFormat string `toml:"log-format"` } type GRPCConfig struct { diff --git a/cmd/buildkitd/main.go b/cmd/buildkitd/main.go index 75c4c5628f7d6..90bb2aa744176 100644 --- a/cmd/buildkitd/main.go +++ b/cmd/buildkitd/main.go @@ -171,6 +171,12 @@ func main() { Usage: "listening address (socket or tcp)", Value: &cli.StringSlice{defaultConf.GRPC.Address[0]}, }, + // Add format flag to control log formatter + cli.StringFlag{ + Name: "log-format", + Usage: "log formatter: json or text", + Value: "text", + }, cli.StringFlag{ Name: "group", Usage: "group (name or gid) which will own all Unix socket listening addresses", @@ -223,7 +229,15 @@ func main() { return err } - logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true}) + switch cfg.LogFormat { + case "json": + logrus.SetFormatter(&logrus.JSONFormatter{}) + case "text", "": + logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true}) + default: + return errors.Errorf("unsupported log type %q", cfg.LogFormat) + } + if cfg.Debug { logrus.SetLevel(logrus.DebugLevel) } @@ -470,7 +484,9 @@ func applyMainFlags(c *cli.Context, cfg *config.Config) error { if c.IsSet("root") { cfg.Root = c.String("root") } - + if c.IsSet("log-format") { + cfg.LogFormat = c.String("log-format") + } if c.IsSet("addr") || len(cfg.GRPC.Address) == 0 { cfg.GRPC.Address = c.StringSlice("addr") } diff --git a/docs/reference/buildctl.md b/docs/reference/buildctl.md index e24766a0937c6..61e8b9b5b504f 100644 --- a/docs/reference/buildctl.md +++ b/docs/reference/buildctl.md @@ -24,6 +24,7 @@ COMMANDS: GLOBAL OPTIONS: --debug enable debug output in logs --addr value buildkitd address (default: "unix:///run/buildkit/buildkitd.sock") + --log-format value log formatter: json or text (default: "text") --tlsservername value buildkitd server name for certificate validation --tlscacert value CA certificate for validation --tlscert value client certificate