Skip to content

Commit 5d15e36

Browse files
committed
Add support for JSON formatted logs (moby#3133)
Signed-off-by: Guilhem Charles <[email protected]>
1 parent f53c175 commit 5d15e36

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

@@ -29,6 +33,10 @@ type Config struct {
2933
History *HistoryConfig `toml:"history"`
3034
}
3135

36+
type LogConfig struct {
37+
Format string `toml:"format"`
38+
}
39+
3240
type GRPCConfig struct {
3341
Address []string `toml:"address"`
3442
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",
@@ -223,7 +229,16 @@ func main() {
223229
return err
224230
}
225231

226-
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
232+
logFormat := cfg.Log.Format
233+
switch logFormat {
234+
case "json":
235+
logrus.SetFormatter(&logrus.JSONFormatter{})
236+
case "text", "":
237+
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
238+
default:
239+
return errors.Errorf("unsupported log type %q", logFormat)
240+
}
241+
227242
if cfg.Debug {
228243
logrus.SetLevel(logrus.DebugLevel)
229244
}
@@ -470,7 +485,9 @@ func applyMainFlags(c *cli.Context, cfg *config.Config) error {
470485
if c.IsSet("root") {
471486
cfg.Root = c.String("root")
472487
}
473-
488+
if c.IsSet("log-format") {
489+
cfg.Log.Format = c.String("log-format")
490+
}
474491
if c.IsSet("addr") || len(cfg.GRPC.Address) == 0 {
475492
cfg.GRPC.Address = c.StringSlice("addr")
476493
}

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)