embed: change "Config.LogOutput" to []string

Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
This commit is contained in:
Gyuho Lee 2018-04-17 11:50:48 -07:00
parent 954afc9156
commit bf937535f5
2 changed files with 25 additions and 18 deletions

View File

@ -242,11 +242,12 @@ type Config struct {
Logger string `json:"logger"` Logger string `json:"logger"`
// LogOutput is either: // LogOutput is either:
// - "default" as os.Stderr // - "default" as os.Stderr,
// - "stderr" as os.Stderr // - "stderr" as os.Stderr,
// - "stdout" as os.Stdout // - "stdout" as os.Stdout,
// - file path to append server logs to // - file path to append server logs to.
LogOutput string `json:"log-output"` // It can be multiple when "Logger" is zap.
LogOutput []string `json:"log-output"`
// Debug is true, to enable debug level logging. // Debug is true, to enable debug level logging.
Debug bool `json:"debug"` Debug bool `json:"debug"`
@ -319,7 +320,7 @@ func NewConfig() *Config {
loggerMu: new(sync.RWMutex), loggerMu: new(sync.RWMutex),
logger: nil, logger: nil,
Logger: "capnslog", Logger: "capnslog",
LogOutput: DefaultLogOutput, LogOutput: []string{DefaultLogOutput},
Debug: false, Debug: false,
LogPkgLevels: "", LogPkgLevels: "",
} }
@ -381,27 +382,33 @@ func (cfg *Config) setupLogging() error {
repoLog.SetLogLevel(settings) repoLog.SetLogLevel(settings)
} }
if len(cfg.LogOutput) != 1 {
fmt.Printf("expected only 1 value in 'log-output', got %v\n", cfg.LogOutput)
os.Exit(1)
}
// capnslog initially SetFormatter(NewDefaultFormatter(os.Stderr)) // capnslog initially SetFormatter(NewDefaultFormatter(os.Stderr))
// where NewDefaultFormatter returns NewJournaldFormatter when syscall.Getppid() == 1 // where NewDefaultFormatter returns NewJournaldFormatter when syscall.Getppid() == 1
// specify 'stdout' or 'stderr' to skip journald logging even when running under systemd // specify 'stdout' or 'stderr' to skip journald logging even when running under systemd
switch cfg.LogOutput { output := cfg.LogOutput[0]
switch output {
case "stdout": case "stdout":
capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stdout, cfg.Debug)) capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stdout, cfg.Debug))
case "stderr": case "stderr":
capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stderr, cfg.Debug)) capnslog.SetFormatter(capnslog.NewPrettyFormatter(os.Stderr, cfg.Debug))
case DefaultLogOutput: case DefaultLogOutput:
default: default:
plog.Panicf(`unknown log-output %q (only supports %q, "stdout", "stderr")`, cfg.LogOutput, DefaultLogOutput) plog.Panicf(`unknown log-output %q (only supports %q, "stdout", "stderr")`, output, DefaultLogOutput)
} }
case "zap": case "zap":
if cfg.LogOutput == "" { if len(cfg.LogOutput) == 0 {
cfg.LogOutput = DefaultLogOutput cfg.LogOutput = []string{DefaultLogOutput}
} }
outputs := strings.Split(cfg.LogOutput, ",") if len(cfg.LogOutput) > 1 {
for _, v := range outputs { for _, v := range cfg.LogOutput {
if v == DefaultLogOutput { if v == DefaultLogOutput {
panic(fmt.Errorf("multi logoutput for %q is not supported yet", DefaultLogOutput)) panic(fmt.Errorf("multi logoutput for %q is not supported yet", DefaultLogOutput))
}
} }
} }
@ -420,7 +427,7 @@ func (cfg *Config) setupLogging() error {
ErrorOutputPaths: make([]string, 0), ErrorOutputPaths: make([]string, 0),
} }
outputPaths, errOutputPaths := make(map[string]struct{}), make(map[string]struct{}) outputPaths, errOutputPaths := make(map[string]struct{}), make(map[string]struct{})
for _, v := range outputs { for _, v := range cfg.LogOutput {
switch v { switch v {
case DefaultLogOutput: case DefaultLogOutput:
if syscall.Getppid() == 1 { if syscall.Getppid() == 1 {

View File

@ -34,14 +34,14 @@ func TestConfigFileOtherFields(t *testing.T) {
PeerSecurityCfgFile securityConfig `json:"peer-transport-security"` PeerSecurityCfgFile securityConfig `json:"peer-transport-security"`
ForceNewCluster bool `json:"force-new-cluster"` ForceNewCluster bool `json:"force-new-cluster"`
Logger string `json:"logger"` Logger string `json:"logger"`
LogOutput string `json:"log-output"` LogOutputs []string `json:"log-output"`
Debug bool `json:"debug"` Debug bool `json:"debug"`
}{ }{
ctls, ctls,
ptls, ptls,
true, true,
"zap", "zap",
"/dev/null", []string{"/dev/null"},
false, false,
} }
@ -157,7 +157,7 @@ func mustCreateCfgFile(t *testing.T, b []byte) *os.File {
func TestAutoCompactionModeInvalid(t *testing.T) { func TestAutoCompactionModeInvalid(t *testing.T) {
cfg := NewConfig() cfg := NewConfig()
cfg.Logger = "zap" cfg.Logger = "zap"
cfg.LogOutput = "/dev/null" cfg.LogOutput = []string{"/dev/null"}
cfg.Debug = false cfg.Debug = false
cfg.AutoCompactionMode = "period" cfg.AutoCompactionMode = "period"
err := cfg.Validate() err := cfg.Validate()