From d6331d80c97d63e7f40ef933e69080dccffe0fd5 Mon Sep 17 00:00:00 2001 From: Edouard Schweisguth Date: Mon, 1 Jul 2024 12:21:02 +0200 Subject: [PATCH] Add custom filter for text_formatter (#212) * Add custom filter for text_formatter In order to make console output more user friendly, lets remove some fields that are unnecessary for human consumption (team, component, service) * remove run_id as well --- pkg/telemetry/log/logger.go | 6 +++-- pkg/telemetry/log/text_formatter.go | 36 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 pkg/telemetry/log/text_formatter.go diff --git a/pkg/telemetry/log/logger.go b/pkg/telemetry/log/logger.go index cc1b7bed3..1a360efa6 100644 --- a/pkg/telemetry/log/logger.go +++ b/pkg/telemetry/log/logger.go @@ -125,6 +125,8 @@ func Trace(ctx context.Context, opts ...LoggerOption) *KubehoundLogger { } func GetLogrusFormatter() logrus.Formatter { + customTextFormatter := NewFilteredTextFormatter(DefaultRemovedFields) + switch logFormat := os.Getenv("KH_LOG_FORMAT"); { // Datadog require the logged field to be "message" and not "msg" case logFormat == "dd": @@ -138,8 +140,8 @@ func GetLogrusFormatter() logrus.Formatter { case logFormat == "json": return &logrus.JSONFormatter{} case logFormat == "text": - return &logrus.TextFormatter{} + return customTextFormatter default: - return &logrus.TextFormatter{} + return customTextFormatter } } diff --git a/pkg/telemetry/log/text_formatter.go b/pkg/telemetry/log/text_formatter.go new file mode 100644 index 000000000..40e566024 --- /dev/null +++ b/pkg/telemetry/log/text_formatter.go @@ -0,0 +1,36 @@ +package log + +import ( + "time" + + logrus "github.com/sirupsen/logrus" +) + +var ( + DefaultRemovedFields = []string{"component", "team", "service", "run_id"} +) + +// FilteredTextFormatter is a logrus.TextFormatter that filters out some specific fields that are not needed for humans. +// These fields are usually more helpful for machines, and with json formatting for example. +type FilteredTextFormatter struct { + tf logrus.TextFormatter + removedFields []string +} + +func NewFilteredTextFormatter(removedFields []string) logrus.Formatter { + return &FilteredTextFormatter{ + tf: logrus.TextFormatter{ + FullTimestamp: true, + TimestampFormat: time.TimeOnly, + }, + removedFields: removedFields, + } +} + +func (f *FilteredTextFormatter) Format(entry *logrus.Entry) ([]byte, error) { + for _, field := range f.removedFields { + delete(entry.Data, field) + } + + return f.tf.Format(entry) +}