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: Moe Ghanem <[email protected]>

added toml and flag functionality to buildkitd, fixed buildctl

Signed-off-by: Moe Ghanem <[email protected]>

removed toml from ctl

Signed-off-by: Moe Ghanem <[email protected]>

buildctl –opt flags documentation

Signed-off-by: Moe Ghanem <[email protected]>

updated with the properly toml and flag usage

Signed-off-by: Moe Ghanem <[email protected]>

changed "format" to "log-format", config struct

Signed-off-by: Moe Ghanem <[email protected]>
  • Loading branch information
benjamin010095 authored and chagui committed Jul 20, 2023
1 parent f53c175 commit 460b516
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
15 changes: 13 additions & 2 deletions cmd/buildctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 +112,13 @@ 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")
if logFormat == "json" {
logrus.SetFormatter(&logrus.JSONFormatter{})
} else {
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
}
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,12 @@ func main() {
return err
}

logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
if cfg.LogFormat == "json" {
logrus.SetFormatter(&logrus.JSONFormatter{})
} else {
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
}

if cfg.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
Expand Down Expand Up @@ -470,7 +481,12 @@ func applyMainFlags(c *cli.Context, cfg *config.Config) error {
if c.IsSet("root") {
cfg.Root = c.String("root")
}

if c.IsSet("log-format") {
// set format if not set by config toml
if cfg.LogFormat == "" {
cfg.LogFormat = c.String("log-format")
}
}
if c.IsSet("addr") || len(cfg.GRPC.Address) == 0 {
cfg.GRPC.Address = c.StringSlice("addr")
}
Expand Down

0 comments on commit 460b516

Please sign in to comment.