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 <gyuhox@gmail.com>
This commit is contained in:
Gyuho Lee 2018-05-16 14:47:43 -07:00
parent 3a80499da4
commit 15fcd6d599
2 changed files with 20 additions and 24 deletions

View File

@ -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.

View File

@ -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)
}
}
}