Skip to content

Commit

Permalink
Allow configuration of different log formats: text, json
Browse files Browse the repository at this point in the history
Signed-off-by: Vlad Ungureanu <vladu@palantir.com>
  • Loading branch information
ungureanuvladvictor committed Dec 4, 2020
1 parent 59a0667 commit f12b68c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
35 changes: 28 additions & 7 deletions cmd/containerd/command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ high performance container runtime
`

func init() {
logrus.SetFormatter(&logrus.TextFormatter{
TimestampFormat: log.RFC3339NanoFixed,
FullTimestamp: true,
})

// Discard grpc logs so that they don't mess with our stdio
grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))

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

func setLevel(context *cli.Context, config *srvconfig.Config) error {
func setLogLevel(context *cli.Context, config *srvconfig.Config) error {
l := context.GlobalString("log-level")
if l == "" {
l = config.Debug.Level
Expand All @@ -297,6 +295,29 @@ func setLevel(context *cli.Context, config *srvconfig.Config) error {
return nil
}

func setLogFormat(config *srvconfig.Config) error {
f := config.Debug.Format
if f == "" {
f = log.TextFormat
}

switch f {
case log.TextFormat:
logrus.SetFormatter(&logrus.TextFormatter{
TimestampFormat: log.RFC3339NanoFixed,
FullTimestamp: true,
})
case log.JSONFormat:
logrus.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: log.RFC3339NanoFixed,
})
default:
return errors.Errorf("unknown log format: %s", f)
}

return nil
}

func dumpStacks(writeToFile bool) {
var (
buf []byte
Expand Down
14 changes: 11 additions & 3 deletions log/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ type (
loggerKey struct{}
)

// RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to
// ensure the formatted time is always the same number of characters.
const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
const (
// RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to
// ensure the formatted time is always the same number of characters.
RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"

// TextFormat represents the text logging format
TextFormat = "text"

// JSONFormat represents the JSON logging format
JSONFormat = "json"
)

// WithLogger returns a new context with the provided logger. Use in
// combination with logger.WithField(s) for great effect.
Expand Down
2 changes: 2 additions & 0 deletions services/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ type Debug struct {
UID int `toml:"uid"`
GID int `toml:"gid"`
Level string `toml:"level"`
// Format represents the logging format
Format string `toml:"format"`
}

// MetricsConfig provides metrics configuration
Expand Down

0 comments on commit f12b68c

Please sign in to comment.