diff --git a/cmd/internal/flags/flags.go b/cmd/internal/flags/flags.go index 272ad8dd4fe..91060af5d6f 100644 --- a/cmd/internal/flags/flags.go +++ b/cmd/internal/flags/flags.go @@ -29,6 +29,7 @@ import ( const ( spanStorageType = "span-storage.type" // deprecated logLevel = "log-level" + logEncoding = "log-encoding" // json or console configFile = "config-file" ) @@ -98,23 +99,26 @@ type SharedFlags struct { } type logging struct { - Level string + Level string + Encoding string } // AddFlags adds flags for SharedFlags func AddFlags(flagSet *flag.FlagSet) { flagSet.String(spanStorageType, "", "(deprecated) please use SPAN_STORAGE_TYPE environment variable. Run this binary with the 'env' command for help.") - AddLoggingFlag(flagSet) + AddLoggingFlags(flagSet) } // AddLoggingFlag adds logging flag for SharedFlags -func AddLoggingFlag(flagSet *flag.FlagSet) { +func AddLoggingFlags(flagSet *flag.FlagSet) { flagSet.String(logLevel, "info", "Minimal allowed log Level. For more levels see https://github.com/uber-go/zap") + flagSet.String(logEncoding, "json", "Log encoding. Supported values are 'json' and 'console'.") } // InitFromViper initializes SharedFlags with properties from viper func (flags *SharedFlags) InitFromViper(v *viper.Viper) *SharedFlags { flags.Logging.Level = v.GetString(logLevel) + flags.Logging.Encoding = v.GetString(logEncoding) return flags } @@ -126,5 +130,9 @@ func (flags *SharedFlags) NewLogger(conf zap.Config, options ...zap.Option) (*za return nil, err } conf.Level = zap.NewAtomicLevelAt(level) + conf.Encoding = flags.Logging.Encoding + if flags.Logging.Encoding == "console" { + conf.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + } return conf.Build(options...) } diff --git a/cmd/internal/flags/service.go b/cmd/internal/flags/service.go index a8be54d9d56..12293a7b3ef 100644 --- a/cmd/internal/flags/service.go +++ b/cmd/internal/flags/service.go @@ -68,7 +68,7 @@ func NewService(adminPort int) *Service { func (s *Service) AddFlags(flagSet *flag.FlagSet) { AddConfigFileFlag(flagSet) if s.NoStorage { - AddLoggingFlag(flagSet) + AddLoggingFlags(flagSet) } else { AddFlags(flagSet) } diff --git a/pkg/es/config/config.go b/pkg/es/config/config.go index 0d3dc07ee04..5ecdeb62e3f 100644 --- a/pkg/es/config/config.go +++ b/pkg/es/config/config.go @@ -378,7 +378,7 @@ func (c *Configuration) getConfigOptions(logger *zap.Logger) ([]elastic.ClientOp options = append(options, elastic.SetSendGetBodyAs(c.SendGetBodyAs)) } - options, err := addLoggerOptions(options, c.LogLevel) + options, err := addLoggerOptions(options, c.LogLevel, logger) if err != nil { return options, err } @@ -391,13 +391,11 @@ func (c *Configuration) getConfigOptions(logger *zap.Logger) ([]elastic.ClientOp return options, nil } -func addLoggerOptions(options []elastic.ClientOptionFunc, logLevel string) ([]elastic.ClientOptionFunc, error) { +func addLoggerOptions(options []elastic.ClientOptionFunc, logLevel string, logger *zap.Logger) ([]elastic.ClientOptionFunc, error) { // Decouple ES logger from the log-level assigned to the parent application's log-level; otherwise, the least // permissive log-level will dominate. // e.g. --log-level=info and --es.log-level=debug would mute ES's debug logging and would require --log-level=debug // to show ES debug logs. - prodConfig := zap.NewProductionConfig() - var lvl zapcore.Level var setLogger func(logger elastic.Logger) elastic.ClientOptionFunc @@ -415,11 +413,10 @@ func addLoggerOptions(options []elastic.ClientOptionFunc, logLevel string) ([]el return options, fmt.Errorf("unrecognized log-level: \"%s\"", logLevel) } - prodConfig.Level.SetLevel(lvl) - esLogger, err := prodConfig.Build() - if err != nil { - return options, err - } + esLogger := logger.WithOptions( + zap.IncreaseLevel(lvl), + zap.AddCallerSkip(2), // to ensure the right caller:lineno are logged + ) // Elastic client requires a "Printf"-able logger. l := zapgrpc.NewLogger(esLogger)