From 15fcd6d599cd20d0b68eb8b75c984c3b4ce5836c Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Wed, 16 May 2018 14:47:43 -0700 Subject: [PATCH] embed: do not support "--log-outputs=default" for zap logger systemd/journal should be configured manually. Interpreting PPID 1 as systemd unit is wrong, especially when embedded etcd runs under Docker container. For such case, every single log write will error and fall back to stderr, which is inefficient. Instead, add "systemd/journal" log output option and returns error if systemd/journal socket cannot be found. "--logger=zap --log-outputs=default" will only error for v3.4, since zap logger is still experimental. v3.5 deprecates capnslog and changes "--log-outputs" default value to "--log-outputs=stderr". Signed-off-by: Gyuho Lee --- embed/config.go | 3 +++ embed/config_logging.go | 41 +++++++++++++++++------------------------ 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/embed/config.go b/embed/config.go index 64eb02995..4289b615b 100644 --- a/embed/config.go +++ b/embed/config.go @@ -59,6 +59,9 @@ const ( DefaultListenClientURLs = "http://localhost:2379" DefaultLogOutput = "default" + JournalLogOutput = "systemd/journal" + StdErrLogOutput = "stderr" + StdOutLogOutput = "stdout" // DefaultStrictReconfigCheck is the default value for "--strict-reconfig-check" flag. // It's enabled by default. diff --git a/embed/config_logging.go b/embed/config_logging.go index 4b5919c90..3cd92ce6d 100644 --- a/embed/config_logging.go +++ b/embed/config_logging.go @@ -23,7 +23,6 @@ import ( "reflect" "sort" "sync" - "syscall" "github.com/coreos/etcd/pkg/logutil" @@ -112,13 +111,13 @@ func (cfg *Config) setupLogging() error { // specify 'stdout' or 'stderr' to skip journald logging even when running under systemd output := cfg.LogOutputs[0] switch output { - case "stdout": - capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stdout, cfg.Debug)) - case "stderr": + case StdErrLogOutput: capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stderr, cfg.Debug)) + case StdOutLogOutput: + capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stdout, cfg.Debug)) case DefaultLogOutput: default: - plog.Panicf(`unknown log-output %q (only supports %q, "stdout", "stderr")`, output, DefaultLogOutput) + plog.Panicf(`unknown log-output %q (only supports %q, %q, %q)`, output, DefaultLogOutput, StdErrLogOutput, StdOutLogOutput) } case "zap": @@ -147,30 +146,24 @@ func (cfg *Config) setupLogging() error { OutputPaths: make([]string, 0), ErrorOutputPaths: make([]string, 0), } + outputPaths, errOutputPaths := make(map[string]struct{}), make(map[string]struct{}) - isJournald := false + isJournal := false for _, v := range cfg.LogOutputs { switch v { case DefaultLogOutput: - if syscall.Getppid() == 1 { - // capnslog initially SetFormatter(NewDefaultFormatter(os.Stderr)) - // where "NewDefaultFormatter" returns "NewJournaldFormatter" - // specify 'stdout' or 'stderr' to override this redirects - // when syscall.Getppid() == 1 - isJournald = true - break - } + return errors.New("'--log-outputs=default' is not supported for v3.4 during zap logger migraion (use 'journal', 'stderr', 'stdout', etc.)") - outputPaths["stderr"] = struct{}{} - errOutputPaths["stderr"] = struct{}{} + case JournalLogOutput: + isJournal = true - case "stderr": - outputPaths["stderr"] = struct{}{} - errOutputPaths["stderr"] = struct{}{} + case StdErrLogOutput: + outputPaths[StdErrLogOutput] = struct{}{} + errOutputPaths[StdErrLogOutput] = struct{}{} - case "stdout": - outputPaths["stdout"] = struct{}{} - errOutputPaths["stdout"] = struct{}{} + case StdOutLogOutput: + outputPaths[StdOutLogOutput] = struct{}{} + errOutputPaths[StdOutLogOutput] = struct{}{} default: outputPaths[v] = struct{}{} @@ -178,7 +171,7 @@ func (cfg *Config) setupLogging() error { } } - if !isJournald { + if !isJournal { for v := range outputPaths { lcfg.OutputPaths = append(lcfg.OutputPaths, v) } @@ -219,7 +212,7 @@ func (cfg *Config) setupLogging() error { if len(cfg.LogOutputs) > 1 { for _, v := range cfg.LogOutputs { if v != DefaultLogOutput { - return fmt.Errorf("running as a systemd unit but other '--log-output' values (%q) are configured with 'default'; override 'default' value with something else", cfg.LogOutputs) + return fmt.Errorf("running with systemd/journal but other '--log-outputs' values (%q) are configured with 'default'; override 'default' value with something else", cfg.LogOutputs) } } }