Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better logging options #5675

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions cmd/internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
const (
spanStorageType = "span-storage.type" // deprecated
logLevel = "log-level"
logEncoding = "log-encoding" // json or console
configFile = "config-file"
)

Expand Down Expand Up @@ -98,23 +99,26 @@
}

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
}

Expand All @@ -126,5 +130,9 @@
return nil, err
}
conf.Level = zap.NewAtomicLevelAt(level)
conf.Encoding = flags.Logging.Encoding
if flags.Logging.Encoding == "console" {
conf.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder

Check warning on line 135 in cmd/internal/flags/flags.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/flags/flags.go#L135

Added line #L135 was not covered by tests
}
return conf.Build(options...)
}
2 changes: 1 addition & 1 deletion cmd/internal/flags/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
15 changes: 6 additions & 9 deletions pkg/es/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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

Expand All @@ -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)
Expand Down
Loading