Skip to content

Commit f12b68c

Browse files
Allow configuration of different log formats: text, json
Signed-off-by: Vlad Ungureanu <vladu@palantir.com>
1 parent 59a0667 commit f12b68c

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

cmd/containerd/command/main.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@ high performance container runtime
5151
`
5252

5353
func init() {
54-
logrus.SetFormatter(&logrus.TextFormatter{
55-
TimestampFormat: log.RFC3339NanoFixed,
56-
FullTimestamp: true,
57-
})
58-
5954
// Discard grpc logs so that they don't mess with our stdio
6055
grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
6156

@@ -252,7 +247,10 @@ func serve(ctx gocontext.Context, l net.Listener, serveFunc func(net.Listener) e
252247
func applyFlags(context *cli.Context, config *srvconfig.Config) error {
253248
// the order for config vs flag values is that flags will always override
254249
// the config values if they are set
255-
if err := setLevel(context, config); err != nil {
250+
if err := setLogLevel(context, config); err != nil {
251+
return err
252+
}
253+
if err := setLogFormat(config); err != nil {
256254
return err
257255
}
258256
for _, v := range []struct {
@@ -282,7 +280,7 @@ func applyFlags(context *cli.Context, config *srvconfig.Config) error {
282280
return nil
283281
}
284282

285-
func setLevel(context *cli.Context, config *srvconfig.Config) error {
283+
func setLogLevel(context *cli.Context, config *srvconfig.Config) error {
286284
l := context.GlobalString("log-level")
287285
if l == "" {
288286
l = config.Debug.Level
@@ -297,6 +295,29 @@ func setLevel(context *cli.Context, config *srvconfig.Config) error {
297295
return nil
298296
}
299297

298+
func setLogFormat(config *srvconfig.Config) error {
299+
f := config.Debug.Format
300+
if f == "" {
301+
f = log.TextFormat
302+
}
303+
304+
switch f {
305+
case log.TextFormat:
306+
logrus.SetFormatter(&logrus.TextFormatter{
307+
TimestampFormat: log.RFC3339NanoFixed,
308+
FullTimestamp: true,
309+
})
310+
case log.JSONFormat:
311+
logrus.SetFormatter(&logrus.JSONFormatter{
312+
TimestampFormat: log.RFC3339NanoFixed,
313+
})
314+
default:
315+
return errors.Errorf("unknown log format: %s", f)
316+
}
317+
318+
return nil
319+
}
320+
300321
func dumpStacks(writeToFile bool) {
301322
var (
302323
buf []byte

log/context.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,17 @@ type (
3737
loggerKey struct{}
3838
)
3939

40-
// RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to
41-
// ensure the formatted time is always the same number of characters.
42-
const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
40+
const (
41+
// RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to
42+
// ensure the formatted time is always the same number of characters.
43+
RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
44+
45+
// TextFormat represents the text logging format
46+
TextFormat = "text"
47+
48+
// JSONFormat represents the JSON logging format
49+
JSONFormat = "json"
50+
)
4351

4452
// WithLogger returns a new context with the provided logger. Use in
4553
// combination with logger.WithField(s) for great effect.

services/server/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ type Debug struct {
138138
UID int `toml:"uid"`
139139
GID int `toml:"gid"`
140140
Level string `toml:"level"`
141+
// Format represents the logging format
142+
Format string `toml:"format"`
141143
}
142144

143145
// MetricsConfig provides metrics configuration

0 commit comments

Comments
 (0)