-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from cdr/ctx-log
Refactor for Context only logging
- Loading branch information
Showing
21 changed files
with
230 additions
and
150 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
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,27 @@ | ||
package slog | ||
|
||
import "context" | ||
|
||
type loggerCtxKey = struct{} | ||
|
||
// SinkContext is used by slog.Make to compose many loggers together. | ||
type SinkContext struct { | ||
Sink | ||
context.Context | ||
} | ||
|
||
func contextWithLogger(ctx context.Context, l logger) SinkContext { | ||
ctx = context.WithValue(ctx, loggerCtxKey{}, l) | ||
return SinkContext{ | ||
Context: ctx, | ||
Sink: l, | ||
} | ||
} | ||
|
||
func loggerFromContext(ctx context.Context) (logger, bool) { | ||
v := ctx.Value(loggerCtxKey{}) | ||
if v == nil { | ||
return logger{}, false | ||
} | ||
return v.(logger), true | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
package slog | ||
|
||
func (l *Logger) SetExit(fn func(int)) { | ||
import "context" | ||
|
||
func SetExit(ctx context.Context, fn func(int)) context.Context { | ||
l, ok := loggerFromContext(ctx) | ||
if !ok { | ||
return ctx | ||
} | ||
l.exit = fn | ||
return contextWithLogger(ctx, l) | ||
} |
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.