Skip to content

Commit

Permalink
Merge pull request #201 from rebuy-de/get-subsystem
Browse files Browse the repository at this point in the history
add function to extract subsystem from context
  • Loading branch information
svenwltr committed May 3, 2024
2 parents 50d026a + 4af6ca9 commit 41a3a41
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions pkg/logutil/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ type meta struct {
log logrus.FieldLogger
}

func (m meta) subsystem() string {
subsystems := []string{"/"}

for _, t := range m.path {
subsystems = append(subsystems, t.subsystem)
}

return path.Join(subsystems...)
}

type trace struct {
id string
subsystem string
Expand All @@ -40,6 +50,15 @@ func Get(ctx context.Context) logrus.FieldLogger {
return m.log
}

// GetSubsystem extracts the name of the subsystem from the given context.
func GetSubsystem(ctx context.Context) string {
m, ok := ctx.Value(contextKeyMeta).(meta)
if !ok {
return ""
}
return m.subsystem()
}

// Start creates a new logger and stores it in the returned context.
// Additionally it creates a new trace ID and injects them into the new logger
// together with previous trace IDs from the given context.
Expand All @@ -55,18 +74,15 @@ func Start(ctx context.Context, subsystem string, opts ...ContextOption) context
subsystem: subsystem,
})

subsystems := []string{"/"}
ids := []string{}

for _, t := range m.path {
name := fmt.Sprintf("trace-id-%s", t.subsystem)
m.log = m.log.WithField(name, t.id)

subsystems = append(subsystems, t.subsystem)
ids = append(ids, t.id)
}

m.log = m.log.WithField("subsystem", path.Join(subsystems...))
m.log = m.log.WithField("subsystem", m.subsystem())
m.log = m.log.WithField("trace-id", strings.Join(ids, "-"))

for _, opt := range opts {
Expand Down Expand Up @@ -126,10 +142,10 @@ func WithFields(ctx context.Context, fields logrus.Fields) context.Context {

// FromStruct converts any struct into a valid logrus.Fields. It can be customized with the logfield annotation:
//
// type Instance struct {
// InstanceID string `logfield:"instance-id"`
// InstanceName string `logfield:"instance-name"`
// }
// type Instance struct {
// InstanceID string `logfield:"instance-id"`
// InstanceName string `logfield:"instance-name"`
// }
//
// See mapstructure docs for more information:
// https://pkg.go.dev/github.com/mitchellh/mapstructure?tab=doc
Expand Down

0 comments on commit 41a3a41

Please sign in to comment.