Skip to content

Commit

Permalink
Add support for JSON formatted logs (moby#3133)
Browse files Browse the repository at this point in the history
Signed-off-by: Guilhem Charles <[email protected]>
  • Loading branch information
chagui committed Jul 21, 2023
1 parent f53c175 commit 40486e9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
19 changes: 17 additions & 2 deletions cmd/buildctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/buildkitd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 18 additions & 2 deletions cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions docs/reference/buildctl.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 40486e9

Please sign in to comment.