Skip to content

Commit

Permalink
Merge pull request #1 from quiknode-labs/add-context-support
Browse files Browse the repository at this point in the history
Add ctx support
  • Loading branch information
emanuelpinho authored Jan 17, 2025
2 parents ec3ca05 + b110a38 commit dec1fd7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,24 @@ func healthCheck(url string) error {
}
return nil
}

// Define a private type to prevent context key collisions
type logContextKey struct{}

// logKey is the key for storing the logger in the context
var logKey = logContextKey{}

// WithLog embeds the logg into the context
func WithLog(ctx context.Context, logger *logrus.Entry) context.Context {
return context.WithValue(ctx, logKey, logger)
}

// LogFromContext retrieves the log from the context
func LogFromContext(ctx context.Context) *logrus.Entry {
logger, ok := ctx.Value(logKey).(*logrus.Entry)
if !ok {
// Return a default logger if none is found in the context
return logrus.NewEntry(logrus.StandardLogger())
}
return logger
}

0 comments on commit dec1fd7

Please sign in to comment.