From 4af6ca9e3b83ef0058921a7431cebc2a55bcab9f Mon Sep 17 00:00:00 2001 From: Sven Walter Date: Fri, 3 May 2024 17:58:50 +0200 Subject: [PATCH] add function to extract subsystem from context --- pkg/logutil/context.go | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/pkg/logutil/context.go b/pkg/logutil/context.go index 3d0c74f..dbd1524 100644 --- a/pkg/logutil/context.go +++ b/pkg/logutil/context.go @@ -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 @@ -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. @@ -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 { @@ -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