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

cni: Allow text-ts log format value #31686

Merged
merged 1 commit into from
Apr 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const (
// we want to use (possible values: text or json)
DefaultLogFormat LogFormat = LogFormatText

// DefaultLogFormat is the string representation of the default logrus.Formatter
// DefaultLogFormatTimestamp is the string representation of the default logrus.Formatter
// including timestamps.
// We don't use this for general runtime logs since kubernetes log capture handles those.
// This is only used for applications such as CNI which is written to disk so we have no
Expand Down Expand Up @@ -85,7 +85,7 @@ type LogOptions map[string]string
// settings.
func initializeDefaultLogger() (logger *logrus.Logger) {
logger = logrus.New()
logger.SetFormatter(GetFormatter(DefaultLogFormat))
logger.SetFormatter(GetFormatter(DefaultLogFormatTimestamp))
logger.SetLevel(DefaultLogLevel)
return
}
Expand All @@ -112,16 +112,16 @@ func (o LogOptions) GetLogLevel() (level logrus.Level) {
func (o LogOptions) GetLogFormat() LogFormat {
formatOpt, ok := o[FormatOpt]
if !ok {
return DefaultLogFormat
return DefaultLogFormatTimestamp
}

formatOpt = strings.ToLower(formatOpt)
re := regexp.MustCompile(`^(text|json|json-ts)$`)
re := regexp.MustCompile(`^(text|text-ts|json|json-ts)$`)
if !re.MatchString(formatOpt) {
logrus.WithError(
fmt.Errorf("incorrect log format configured '%s', expected 'text', 'json' or 'json-ts'", formatOpt),
fmt.Errorf("incorrect log format configured '%s', expected 'text', 'text-ts', 'json' or 'json-ts'", formatOpt),
).Warning("Ignoring user-configured log format")
return DefaultLogFormat
return DefaultLogFormatTimestamp
}

return LogFormat(formatOpt)
Expand Down Expand Up @@ -149,7 +149,7 @@ func SetLogFormat(logFormat LogFormat) {

// SetDefaultLogFormat updates the DefaultLogger with the DefaultLogFormat
func SetDefaultLogFormat() {
DefaultLogger.SetFormatter(GetFormatter(DefaultLogFormat))
DefaultLogger.SetFormatter(GetFormatter(DefaultLogFormatTimestamp))
}

// AddHooks adds additional logrus hook to default logger
Expand Down
2 changes: 1 addition & 1 deletion pkg/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *LoggingSuite) TestGetLogFormat(c *C) {
c.Assert(opts.GetLogFormat(), Equals, LogFormatJSON)

opts[FormatOpt] = "Invalid"
c.Assert(opts.GetLogFormat(), Equals, DefaultLogFormat)
c.Assert(opts.GetLogFormat(), Equals, DefaultLogFormatTimestamp)

opts[FormatOpt] = "JSON-TS"
c.Assert(opts.GetLogFormat(), Equals, LogFormatJSONTimestamp)
Expand Down