Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
add -log-format option. Closes #247 (#256)
Browse files Browse the repository at this point in the history
* add -log-format option. Closes #247

* fix info
  • Loading branch information
mthenw authored Aug 9, 2017
1 parent 7696e67 commit 0a9a039
Showing 1 changed file with 48 additions and 11 deletions.
59 changes: 48 additions & 11 deletions cmd/event-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ func init() {
func main() {
showVersion := flag.Bool("version", false, "Show version.")
logLevel := zap.LevelFlag("log-level", zap.InfoLevel, `The level of logging to show after the event gateway has started. The available log levels are "debug", "info", "warn", and "err".`)
logFormat := flag.String("log-format", "", `The format of logs. The available formats are "text", "json".)`)
dbHosts := flag.String("db-hosts", "127.0.0.1:2379", "Comma-separated list of database hosts to connect to.")
developmentMode := flag.Bool("dev", false, "Run embedded etcd for testing.")
developmentMode := flag.Bool("dev", false, `Run in development mode with embedded etcd and "text" log format.`)
embedPeerAddr := flag.String("embed-peer-addr", "http://127.0.0.1:2380", "Address for testing embedded etcd to receive peer connections.")
embedCliAddr := flag.String("embed-cli-addr", "http://127.0.0.1:2379", "Address for testing embedded etcd to receive client connections.")
embedDataDir := flag.String("embed-data-dir", "default.etcd", "Path for testing embedded etcd to store its state.")
Expand All @@ -50,16 +51,7 @@ func main() {
prometheus.MustRegister(metrics.RequestDuration)
prometheus.MustRegister(metrics.DroppedPubSubEvents)

logCfg := zap.NewProductionConfig()
logCfg.Level = zap.NewAtomicLevelAt(*logLevel)
if *developmentMode {
logCfg = zap.NewDevelopmentConfig()
logCfg.EncoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {}
logCfg.DisableCaller = true
logCfg.DisableStacktrace = true
logCfg.Level = zap.NewAtomicLevelAt(*logLevel)
}
log, err := logCfg.Build()
log, err := loggerConfiguration(*developmentMode, *logLevel, *logFormat).Build()
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -106,3 +98,48 @@ func main() {

shutdownGuard.Wait()
}

const (
consoleEncoding = "console"
jsonEncoding = "json"
)

func loggerConfiguration(dev bool, level zapcore.Level, format string) zap.Config {
cfg := zap.Config{
Level: zap.NewAtomicLevelAt(level),
Development: false,
Sampling: &zap.SamplingConfig{Initial: 100, Thereafter: 100},
Encoding: "json",
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}

if dev {
cfg.Sampling = nil
cfg.Encoding = consoleEncoding
}

if format != "" {
if format == "text" {
cfg.Encoding = consoleEncoding
} else if format == jsonEncoding {
cfg.Encoding = jsonEncoding
} else {
cfg.Encoding = ""
}
}

if cfg.Encoding == jsonEncoding {
cfg.EncoderConfig = zap.NewProductionEncoderConfig()
}

if cfg.Encoding == consoleEncoding {
cfg.EncoderConfig = zap.NewDevelopmentEncoderConfig()
cfg.EncoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {}
}

cfg.DisableCaller = true
cfg.DisableStacktrace = true

return cfg
}

0 comments on commit 0a9a039

Please sign in to comment.