embed: support "io-discard" for logging, fix racey logging setup

Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
This commit is contained in:
Gyuho Lee
2018-04-16 14:16:46 -07:00
parent 38e32a1b54
commit cc778746fc

View File

@@ -347,6 +347,9 @@ func (cfg Config) GetLogger() *zap.Logger {
return l
}
// for testing
var grpcLogOnce = new(sync.Once)
// setupLogging initializes etcd logging.
// Must be called after flag parsing or finishing configuring embed.Config.
func (cfg *Config) setupLogging() error {
@@ -402,6 +405,7 @@ func (cfg *Config) setupLogging() error {
Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(),
}
ignoreLog := false
switch cfg.LogOutput {
case DefaultLogOutput:
if syscall.Getppid() == 1 {
@@ -423,6 +427,8 @@ func (cfg *Config) setupLogging() error {
case "stdout":
lcfg.OutputPaths = []string{"stdout"}
lcfg.ErrorOutputPaths = []string{"stdout"}
case "io-discard": // only for testing
ignoreLog = true
default:
lcfg.OutputPaths = []string{cfg.LogOutput}
lcfg.ErrorOutputPaths = []string{cfg.LogOutput}
@@ -433,20 +439,28 @@ func (cfg *Config) setupLogging() error {
}
var err error
cfg.logger, err = lcfg.Build()
if !ignoreLog {
cfg.logger, err = lcfg.Build()
} else {
cfg.logger = zap.NewNop()
}
if err != nil {
return err
}
cfg.loggerConfig = lcfg
// debug true, enable info, warning, error
// debug false, only discard info
var gl grpclog.LoggerV2
gl, err = logutil.NewGRPCLoggerV2(lcfg)
grpcLogOnce.Do(func() {
// debug true, enable info, warning, error
// debug false, only discard info
var gl grpclog.LoggerV2
gl, err = logutil.NewGRPCLoggerV2(lcfg)
if err == nil {
grpclog.SetLoggerV2(gl)
}
})
if err != nil {
return err
}
grpclog.SetLoggerV2(gl)
logTLSHandshakeFailure := func(conn *tls.Conn, err error) {
state := conn.ConnectionState()