Skip to content

Commit

Permalink
cni: Allow text-ts log format value
Browse files Browse the repository at this point in the history
The new log format (e.g. text-ts) is added recently in the below commit,
so we need to allow it in regex. Additionally, text-ts is used as the
default value if not specified or invalid.

Fixes: a099bf1
Signed-off-by: Tam Mach <tam.mach@cilium.io>
  • Loading branch information
sayboras committed Mar 30, 2024
1 parent 76a659c commit 70035c7
Showing 1 changed file with 7 additions and 7 deletions.
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

0 comments on commit 70035c7

Please sign in to comment.