Skip to content
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
49 changes: 28 additions & 21 deletions textlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions thorlog/v3/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions thorlog/v3/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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() {}
Expand Down