-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(log): store log in context so it"s automatically propagated
- Loading branch information
1 parent
d83b743
commit b335887
Showing
17 changed files
with
177 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package log | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type ctxKey struct{} | ||
|
||
func NewCtx(ctx context.Context, logger *logrus.Entry) (context.Context, *logrus.Entry) { | ||
ctx = context.WithValue(ctx, ctxKey{}, logger) | ||
|
||
return ctx, entryWithCtx(ctx, logger) | ||
} | ||
|
||
func FromCtx(ctx context.Context) *logrus.Entry { | ||
logger, ok := ctx.Value(ctxKey{}).(*logrus.Entry) | ||
if !ok { | ||
// Fallback to the global logger | ||
return logrus.NewEntry(Log()) | ||
} | ||
|
||
// Ensure `logger.Context == ctx`, not always the case since `ctx` could be a child of `logger.Context` | ||
return entryWithCtx(ctx, logger) | ||
} | ||
|
||
func entryWithCtx(ctx context.Context, logger *logrus.Entry) *logrus.Entry { | ||
loggerCopy := *logger | ||
loggerCopy.Context = ctx | ||
|
||
return &loggerCopy | ||
} | ||
|
||
func WrapCtx(ctx context.Context, wrap func(*logrus.Entry) *logrus.Entry) (context.Context, *logrus.Entry) { | ||
logger := FromCtx(ctx) | ||
logger = wrap(logger) | ||
|
||
return NewCtx(ctx, logger) | ||
} | ||
|
||
func CtxWithFields(ctx context.Context, fields logrus.Fields) (context.Context, *logrus.Entry) { | ||
return WrapCtx(ctx, func(e *logrus.Entry) *logrus.Entry { | ||
return e.WithFields(fields) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.