From 10222f98da0ff56ed247566b85509f72af5fd1a2 Mon Sep 17 00:00:00 2001 From: Max Altgelt Date: Tue, 19 Aug 2025 16:16:20 +0200 Subject: [PATCH] feat: skip specific values in context --- textlog.go | 49 ++++++++++++++++++++++++------------------ thorlog/v3/event.go | 13 +++++++++++ thorlog/v3/registry.go | 4 ++-- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/textlog.go b/textlog.go index 3024c65..cc22125 100644 --- a/textlog.go +++ b/textlog.go @@ -30,6 +30,9 @@ type TextlogValuePair struct { type TextlogFormatter struct { // FormatValue is a function that formats a single value into a string. If it is nil, fmt.Sprint is used. FormatValue func(data any, modifiers []string) string + // Omit is a function that determines whether a field should be omitted from the log entry. + // If it is nil, no fields are omitted. + Omit func(modifiers []string, value any) bool } func (t TextlogFormatter) format(data any, modifiers []string) string { @@ -114,30 +117,34 @@ func (t TextlogFormatter) toEntry(object reflect.Value) TextlogEntry { if logfield == "-" { continue } - if typeField.Anonymous || textlogTag != "" || slices.Contains(tagModifiers, modifierExplicit) { - if slices.Contains(tagModifiers, modifierOmitempty) && isZero(field) { - continue - } - if typeField.Anonymous || slices.Contains(tagModifiers, modifierExpand) { - // Use the tag as a prefix for the subfields - subentry := t.toEntry(field) - for _, subentryValue := range subentry { - details = append(details, TextlogValuePair{ - Key: ConcatTextLabels(logfield, subentryValue.Key), - Value: subentryValue.Value, - }) - } - } else { - // Add the field as a single value - key := logfield - if field.Kind() == reflect.Ptr { - field = field.Elem() - } + if !typeField.Anonymous && textlogTag == "" && !slices.Contains(tagModifiers, modifierExplicit) { + continue + } + if slices.Contains(tagModifiers, modifierOmitempty) && isZero(field) { + continue + } + if t.Omit != nil && t.Omit(tagModifiers, field.Interface()) { + continue + } + if typeField.Anonymous || slices.Contains(tagModifiers, modifierExpand) { + // Use the tag as a prefix for the subfields + subentry := t.toEntry(field) + for _, subentryValue := range subentry { details = append(details, TextlogValuePair{ - Key: key, - Value: t.format(field.Interface(), tagModifiers), + Key: ConcatTextLabels(logfield, subentryValue.Key), + Value: subentryValue.Value, }) } + } else { + // Add the field as a single value + key := logfield + if field.Kind() == reflect.Ptr { + field = field.Elem() + } + details = append(details, TextlogValuePair{ + Key: key, + Value: t.format(field.Interface(), tagModifiers), + }) } } return details diff --git a/thorlog/v3/event.go b/thorlog/v3/event.go index 314457c..e0eaa7d 100644 --- a/thorlog/v3/event.go +++ b/thorlog/v3/event.go @@ -11,6 +11,7 @@ import ( "github.com/NextronSystems/jsonlog" "github.com/NextronSystems/jsonlog/jsonpointer" "github.com/NextronSystems/jsonlog/thorlog/common" + "golang.org/x/exp/slices" ) type Finding struct { @@ -116,6 +117,8 @@ func (c *ContextObject) UnmarshalJSON(data []byte) error { return nil } +const omitInContext = "omitincontext" + func (c Context) MarshalTextLog(t jsonlog.TextlogFormatter) jsonlog.TextlogEntry { var elementsByRelation [][]ContextObject for _, element := range c { @@ -131,6 +134,16 @@ func (c Context) MarshalTextLog(t jsonlog.TextlogFormatter) jsonlog.TextlogEntry elementsByRelation = append(elementsByRelation, []ContextObject{element}) } } + oldOmit := t.Omit + t.Omit = func(modifiers []string, value any) bool { + if slices.Contains(modifiers, omitInContext) { + return true // Omit fields that are marked with "omitincontext" + } + if oldOmit != nil { + return oldOmit(modifiers, value) // Call the original omit function if it exists + } + return false // Default behavior is to not omit any fields + } var result jsonlog.TextlogEntry for _, group := range elementsByRelation { diff --git a/thorlog/v3/registry.go b/thorlog/v3/registry.go index 8eefc1f..55297a5 100644 --- a/thorlog/v3/registry.go +++ b/thorlog/v3/registry.go @@ -11,7 +11,7 @@ type RegistryValue struct { Key string `json:"key" textlog:"key"` Modified time.Time `json:"modified" textlog:"modified"` - ParsedValue string `json:"value" textlog:"value"` + ParsedValue string `json:"value" textlog:"value,omitincontext"` Size uint64 `json:"size" textlog:"size"` } @@ -33,7 +33,7 @@ type RegistryKey struct { jsonlog.ObjectHeader Key string `json:"key" textlog:"key"` Modified time.Time `json:"modified" textlog:"modified"` - FormattedValues string `json:"values" textlog:"values"` + FormattedValues string `json:"values" textlog:"values,omitincontext"` } func (RegistryKey) reportable() {}