Browse Source

*: use default log configuration for server

Signed-off-by: Gyuho Lee <leegyuho@amazon.com>
Gyuho Lee 6 years ago
parent
commit
8d1a62e7ef
3 changed files with 20 additions and 65 deletions
  1. 17 38
      embed/config_logging.go
  2. 1 13
      etcdmain/grpc_proxy.go
  3. 2 14
      etcdserver/raft.go

+ 17 - 38
embed/config_logging.go

@@ -21,7 +21,6 @@ import (
 	"io/ioutil"
 	"os"
 	"reflect"
-	"sort"
 	"sync"
 
 	"go.etcd.io/etcd/pkg/logutil"
@@ -131,77 +130,55 @@ func (cfg *Config) setupLogging() error {
 			}
 		}
 
-		// TODO: use zapcore to support more features?
-		lcfg := zap.Config{
-			Level:       zap.NewAtomicLevelAt(zap.InfoLevel),
-			Development: false,
-			Sampling: &zap.SamplingConfig{
-				Initial:    100,
-				Thereafter: 100,
-			},
-			Encoding:      "json",
-			EncoderConfig: zap.NewProductionEncoderConfig(),
-
-			OutputPaths:      make([]string, 0),
-			ErrorOutputPaths: make([]string, 0),
-		}
-
-		outputPaths, errOutputPaths := make(map[string]struct{}), make(map[string]struct{})
+		outputPaths, errOutputPaths := make([]string, 0), make([]string, 0)
 		isJournal := false
 		for _, v := range cfg.LogOutputs {
 			switch v {
 			case DefaultLogOutput:
-				outputPaths[StdErrLogOutput] = struct{}{}
-				errOutputPaths[StdErrLogOutput] = struct{}{}
+				outputPaths = append(outputPaths, StdErrLogOutput)
+				errOutputPaths = append(errOutputPaths, StdErrLogOutput)
 
 			case JournalLogOutput:
 				isJournal = true
 
 			case StdErrLogOutput:
-				outputPaths[StdErrLogOutput] = struct{}{}
-				errOutputPaths[StdErrLogOutput] = struct{}{}
+				outputPaths = append(outputPaths, StdErrLogOutput)
+				errOutputPaths = append(errOutputPaths, StdErrLogOutput)
 
 			case StdOutLogOutput:
-				outputPaths[StdOutLogOutput] = struct{}{}
-				errOutputPaths[StdOutLogOutput] = struct{}{}
+				outputPaths = append(outputPaths, StdOutLogOutput)
+				errOutputPaths = append(errOutputPaths, StdOutLogOutput)
 
 			default:
-				outputPaths[v] = struct{}{}
-				errOutputPaths[v] = struct{}{}
+				outputPaths = append(outputPaths, v)
+				errOutputPaths = append(errOutputPaths, v)
 			}
 		}
 
 		if !isJournal {
-			for v := range outputPaths {
-				lcfg.OutputPaths = append(lcfg.OutputPaths, v)
-			}
-			for v := range errOutputPaths {
-				lcfg.ErrorOutputPaths = append(lcfg.ErrorOutputPaths, v)
-			}
-			sort.Strings(lcfg.OutputPaths)
-			sort.Strings(lcfg.ErrorOutputPaths)
+			copied := logutil.AddOutputPaths(logutil.DefaultZapLoggerConfig, outputPaths, errOutputPaths)
 
 			if cfg.Debug {
-				lcfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
+				copied.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
 				grpc.EnableTracing = true
 			}
 			if cfg.ZapLoggerBuilder == nil {
 				cfg.ZapLoggerBuilder = func(c *Config) error {
 					var err error
-					c.logger, err = lcfg.Build()
+					c.logger, err = copied.Build()
 					if err != nil {
 						return err
 					}
 					c.loggerMu.Lock()
 					defer c.loggerMu.Unlock()
-					c.loggerConfig = &lcfg
+					c.loggerConfig = &copied
 					c.loggerCore = nil
 					c.loggerWriteSyncer = nil
 					grpcLogOnce.Do(func() {
 						// debug true, enable info, warning, error
 						// debug false, only discard info
 						var gl grpclog.LoggerV2
-						gl, err = logutil.NewGRPCLoggerV2(lcfg)
+						gl, err = logutil.NewGRPCLoggerV2(copied)
 						if err == nil {
 							grpclog.SetLoggerV2(gl)
 						}
@@ -233,7 +210,7 @@ func (cfg *Config) setupLogging() error {
 			// WARN: do not change field names in encoder config
 			// journald logging writer assumes field names of "level" and "caller"
 			cr := zapcore.NewCore(
-				zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
+				zapcore.NewJSONEncoder(logutil.DefaultZapLoggerConfig.EncoderConfig),
 				syncer,
 				lvl,
 			)
@@ -253,10 +230,12 @@ func (cfg *Config) setupLogging() error {
 				}
 			}
 		}
+
 		err := cfg.ZapLoggerBuilder(cfg)
 		if err != nil {
 			return err
 		}
+
 		logTLSHandshakeFailure := func(conn *tls.Conn, err error) {
 			state := conn.ConnectionState()
 			remoteAddr := conn.RemoteAddr().String()

+ 1 - 13
etcdmain/grpc_proxy.go

@@ -151,19 +151,7 @@ func newGRPCProxyStartCommand() *cobra.Command {
 func startGRPCProxy(cmd *cobra.Command, args []string) {
 	checkArgs()
 
-	lcfg := zap.Config{
-		Level:       zap.NewAtomicLevelAt(zap.InfoLevel),
-		Development: false,
-		Sampling: &zap.SamplingConfig{
-			Initial:    100,
-			Thereafter: 100,
-		},
-		Encoding:      "json",
-		EncoderConfig: zap.NewProductionEncoderConfig(),
-
-		OutputPaths:      []string{"stderr"},
-		ErrorOutputPaths: []string{"stderr"},
-	}
+	lcfg := logutil.DefaultZapLoggerConfig
 	if grpcProxyDebug {
 		lcfg.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
 		grpc.EnableTracing = true

+ 2 - 14
etcdserver/raft.go

@@ -58,20 +58,8 @@ var (
 )
 
 func init() {
-	lcfg := &zap.Config{
-		Level:       zap.NewAtomicLevelAt(zap.InfoLevel),
-		Development: false,
-		Sampling: &zap.SamplingConfig{
-			Initial:    100,
-			Thereafter: 100,
-		},
-		Encoding:      "json",
-		EncoderConfig: zap.NewProductionEncoderConfig(),
-
-		OutputPaths:      []string{"stderr"},
-		ErrorOutputPaths: []string{"stderr"},
-	}
-	lg, err := logutil.NewRaftLogger(lcfg)
+	lcfg := logutil.DefaultZapLoggerConfig
+	lg, err := logutil.NewRaftLogger(&lcfg)
 	if err != nil {
 		log.Fatalf("cannot create raft logger %v", err)
 	}