-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As described in #13, third-party implementations of `slog.Handler` can choose to take values out of a context to supplement the attributes already used. An example of this in my day job is adding trace IDs to enhance serviceability. This change adds the ability to optionally identify `slog` methods that do not take a context and so prevent the ability to take values from that context. Any matches can be resolved by appending `Context` to the method. For example, `slog.Info` becomes `slog.Context`.
- Loading branch information
Matthew Dowdell
committed
Oct 22, 2023
1 parent
bc45156
commit b563390
Showing
4 changed files
with
67 additions
and
0 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
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,35 @@ | ||
package context_only | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"log/slog" | ||
) | ||
|
||
func tests() { | ||
ctx := context.Background() | ||
|
||
slog.Debug("msg") // want `methods that do not take a context should not be used` | ||
slog.Info("msg") // want `methods that do not take a context should not be used` | ||
slog.Warn("msg") // want `methods that do not take a context should not be used` | ||
slog.Error("msg") // want `methods that do not take a context should not be used` | ||
|
||
slog.Log(context.Background(), slog.LevelInfo, "msg") | ||
slog.DebugContext(context.TODO(), "msg") | ||
slog.InfoContext(context.WithoutCancel(ctx), "msg") | ||
slog.WarnContext(ctx, "msg") | ||
slog.ErrorContext(ctx, "msg") | ||
|
||
logger := slog.New(slog.NewJSONHandler(io.Discard, &slog.HandlerOptions{})) | ||
|
||
logger.Debug("msg") // want `methods that do not take a context should not be used` | ||
logger.Info("msg") // want `methods that do not take a context should not be used` | ||
logger.Warn("msg") // want `methods that do not take a context should not be used` | ||
logger.Error("msg") // want `methods that do not take a context should not be used` | ||
|
||
logger.Log(ctx, slog.LevelInfo, "msg") | ||
logger.DebugContext(ctx, "msg") | ||
logger.InfoContext(ctx, "msg") | ||
logger.WarnContext(ctx, "msg") | ||
logger.ErrorContext(ctx, "msg") | ||
} |