Skip to content

Commit 8952b70

Browse files
authored
Merge pull request moby#4044 from chagui/master
2 parents ec2d958 + 5d15e36 commit 8952b70

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

cmd/buildctl/main.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
_ "github.com/moby/buildkit/util/tracing/detect/jaeger"
2020
_ "github.com/moby/buildkit/util/tracing/env"
2121
"github.com/moby/buildkit/version"
22+
"github.com/pkg/errors"
2223
"github.com/sirupsen/logrus"
2324
"github.com/urfave/cli"
2425
"go.opentelemetry.io/otel"
@@ -57,6 +58,12 @@ func main() {
5758
Usage: "buildkitd address",
5859
Value: defaultAddress,
5960
},
61+
// Add format flag to control log formatter
62+
cli.StringFlag{
63+
Name: "log-format",
64+
Usage: "log formatter: json or text",
65+
Value: "text",
66+
},
6067
cli.StringFlag{
6168
Name: "tlsservername",
6269
Usage: "buildkitd server name for certificate validation",
@@ -106,8 +113,16 @@ func main() {
106113

107114
app.Before = func(context *cli.Context) error {
108115
debugEnabled = context.GlobalBool("debug")
109-
110-
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
116+
// Use Format flag to control log formatter
117+
logFormat := context.GlobalString("log-format")
118+
switch logFormat {
119+
case "json":
120+
logrus.SetFormatter(&logrus.JSONFormatter{})
121+
case "text", "":
122+
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
123+
default:
124+
return errors.Errorf("unsupported log type %q", logFormat)
125+
}
111126
if debugEnabled {
112127
logrus.SetLevel(logrus.DebugLevel)
113128
}

cmd/buildkitd/config/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ type Config struct {
1414

1515
// Entitlements e.g. security.insecure, network.host
1616
Entitlements []string `toml:"insecure-entitlements"`
17+
18+
// LogFormat is the format of the logs. It can be "json" or "text".
19+
Log LogConfig `toml:"log"`
20+
1721
// GRPC configuration settings
1822
GRPC GRPCConfig `toml:"grpc"`
1923

@@ -31,6 +35,10 @@ type Config struct {
3135
History *HistoryConfig `toml:"history"`
3236
}
3337

38+
type LogConfig struct {
39+
Format string `toml:"format"`
40+
}
41+
3442
type GRPCConfig struct {
3543
Address []string `toml:"address"`
3644
DebugAddress string `toml:"debugAddress"`

cmd/buildkitd/main.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ func main() {
171171
Usage: "listening address (socket or tcp)",
172172
Value: &cli.StringSlice{defaultConf.GRPC.Address[0]},
173173
},
174+
// Add format flag to control log formatter
175+
cli.StringFlag{
176+
Name: "log-format",
177+
Usage: "log formatter: json or text",
178+
Value: "text",
179+
},
174180
cli.StringFlag{
175181
Name: "group",
176182
Usage: "group (name or gid) which will own all Unix socket listening addresses",
@@ -227,7 +233,16 @@ func main() {
227233
return err
228234
}
229235

230-
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
236+
logFormat := cfg.Log.Format
237+
switch logFormat {
238+
case "json":
239+
logrus.SetFormatter(&logrus.JSONFormatter{})
240+
case "text", "":
241+
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
242+
default:
243+
return errors.Errorf("unsupported log type %q", logFormat)
244+
}
245+
231246
if cfg.Debug {
232247
logrus.SetLevel(logrus.DebugLevel)
233248
}
@@ -478,7 +493,9 @@ func applyMainFlags(c *cli.Context, cfg *config.Config) error {
478493
if c.IsSet("root") {
479494
cfg.Root = c.String("root")
480495
}
481-
496+
if c.IsSet("log-format") {
497+
cfg.Log.Format = c.String("log-format")
498+
}
482499
if c.IsSet("addr") || len(cfg.GRPC.Address) == 0 {
483500
cfg.GRPC.Address = c.StringSlice("addr")
484501
}

docs/buildkitd.toml.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ root = "/var/lib/buildkit"
1818
# insecure-entitlements allows insecure entitlements, disabled by default.
1919
insecure-entitlements = [ "network.host", "security.insecure" ]
2020

21+
[log]
22+
# log formatter: json or text
23+
format = "text"
24+
2125
[grpc]
2226
address = [ "tcp://0.0.0.0:1234" ]
2327
# debugAddress is address for attaching go profiles and debuggers.

docs/reference/buildctl.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ COMMANDS:
2424
GLOBAL OPTIONS:
2525
--debug enable debug output in logs
2626
--addr value buildkitd address (default: "unix:///run/buildkit/buildkitd.sock")
27+
--log-format value log formatter: json or text (default: "text")
2728
--tlsservername value buildkitd server name for certificate validation
2829
--tlscacert value CA certificate for validation
2930
--tlscert value client certificate

0 commit comments

Comments
 (0)