-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): Add set logger to context function and refactor
- Loading branch information
1 parent
08f5395
commit 5db6244
Showing
2 changed files
with
14 additions
and
10 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 |
---|---|---|
@@ -1,8 +1,22 @@ | ||
package log | ||
|
||
import "context" | ||
|
||
// contextKey is a value for use with context.WithValue. It's used as | ||
// a pointer so it fits in an interface{} without allocation. This technique | ||
// for defining context keys was copied from Go 1.7's new use of context in net/http. | ||
type contextKey struct { | ||
name string | ||
} | ||
|
||
var loggerContextKey = &contextKey{name: "LOGGER_CONTEXT_KEY"} | ||
|
||
func GetLoggerFromContext(ctx context.Context) Logger { | ||
res, _ := ctx.Value(loggerContextKey).(Logger) | ||
|
||
return res | ||
} | ||
|
||
func SetLoggerInContext(ctx context.Context, logger Logger) context.Context { | ||
return context.WithValue(ctx, loggerContextKey, logger) | ||
} |
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