Skip to content

Commit

Permalink
Add console as logging encoding type (#4549)
Browse files Browse the repository at this point in the history
  • Loading branch information
longquanzheng committed Oct 8, 2021
1 parent 9336ed9 commit 84b8d79
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
16 changes: 14 additions & 2 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,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"`
}

// ClusterMetadata contains the all cluster which participated in cross DC
Expand Down Expand Up @@ -303,6 +309,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
Expand Down
12 changes: 11 additions & 1 deletion common/config/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package config

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit 84b8d79

Please sign in to comment.