From 37e9845bd1f650fee7ee9b1df45bd67760c44a1c Mon Sep 17 00:00:00 2001 From: Quanzheng Long Date: Thu, 7 Oct 2021 15:25:52 -0700 Subject: [PATCH] Add console as logging encoding type (#4549) --- common/config/config.go | 16 ++++++++++++++-- common/config/log.go | 12 +++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/common/config/config.go b/common/config/config.go index b6797afad67..a9852279022 100644 --- a/common/config/config.go +++ b/common/config/config.go @@ -317,14 +317,20 @@ type ( // Logger contains the config items for logger Logger struct { - // Stdout is true if the output needs to goto standard out + // Stdout is true then the output needs to goto standard out + // By default this is false and output will go to standard error Stdout bool `yaml:"stdout"` // Level is the desired log level Level string `yaml:"level"` // OutputFile is the path to the log output file + // Stdout must be false, otherwise Stdout will take precedence OutputFile string `yaml:"outputFile"` - // levelKey is the desired log level, defaults to "level" + // LevelKey is the desired log level, defaults to "level" LevelKey string `yaml:"levelKey"` + // Encoding decides the format, supports "console" and "json". + // "json" will print the log in JSON format(better for machine), while "console" will print in plain-text format(more human friendly) + // Default is "json" + Encoding string `yaml:"encoding"` } // DCRedirectionPolicy contains the frontend datacenter redirection policy @@ -340,6 +346,12 @@ type ( // Statsd is the configuration for statsd reporter Statsd *Statsd `yaml:"statsd"` // Prometheus is the configuration for prometheus reporter + // Some documentation below because the tally library is missing it: + // In this configuration, default timerType is "histogram", alternatively "summary" is also supported. + // In some cases, summary is better. Choose it wisely. + // For histogram, default buckets are defined in https://github.com/uber/cadence/blob/master/common/metrics/tally/prometheus/buckets.go#L34 + // For summary, default objectives are defined in https://github.com/uber-go/tally/blob/137973e539cd3589f904c23d0b3a28c579fd0ae4/prometheus/reporter.go#L70 + // You can customize the buckets/objectives if the default is not good enough. Prometheus *prometheus.Configuration `yaml:"prometheus"` // Tags is the set of key-value pairs to be reported // as part of every metric diff --git a/common/config/log.go b/common/config/log.go index e5ac50111a2..c30b3744d2f 100644 --- a/common/config/log.go +++ b/common/config/log.go @@ -21,6 +21,7 @@ package config import ( + "fmt" "os" "strings" @@ -60,11 +61,20 @@ func (cfg *Logger) NewZapLogger() (*zap.Logger, error) { } } + encoding := "json" + if cfg.Encoding != "" { + if cfg.Encoding == "json" || cfg.Encoding == "console" { + encoding = cfg.Encoding + } else { + return nil, fmt.Errorf("invalid encoding for log, only supporting json or console") + } + } + config := zap.Config{ Level: zap.NewAtomicLevelAt(parseZapLevel(cfg.Level)), Development: false, Sampling: nil, // consider exposing this to config for our external customer - Encoding: "json", + Encoding: encoding, EncoderConfig: encodeConfig, OutputPaths: []string{outputPath}, ErrorOutputPaths: []string{outputPath},