forked from golang/go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a package-level variable, slog.Discard, which is a slog.Handler which performs no output. This serves a similar purpose to io.Discard. Fixes golang#62005
- Loading branch information
1 parent
c96939f
commit 8f714b1
Showing
5 changed files
with
76 additions
and
11 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 @@ | ||
pkg log/slog, var DiscardHandler Handler #62005 |
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,14 @@ | ||
package slog | ||
|
||
import "context" | ||
|
||
// DiscardHandler discards all log output. | ||
// DiscardHandler.Enabled returns false for all Levels. | ||
var DiscardHandler Handler = discardHandler{} | ||
|
||
type discardHandler struct{} | ||
|
||
func (dh discardHandler) Enabled(context.Context, Level) bool { return false } | ||
func (dh discardHandler) Handle(context.Context, Record) error { return nil } | ||
func (dh discardHandler) WithAttrs(attrs []Attr) Handler { return dh } | ||
func (dh discardHandler) WithGroup(name string) Handler { return dh } |
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,28 @@ | ||
package slog | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDiscardHandler(t *testing.T) { | ||
ctx := context.Background() | ||
stdout, stderr := os.Stdout, os.Stderr | ||
os.Stdout, os.Stderr = nil, nil // panic on write | ||
t.Cleanup(func() { | ||
os.Stdout, os.Stderr = stdout, stderr | ||
}) | ||
|
||
// Just ensure nothing panics during normal usage | ||
l := New(DiscardHandler) | ||
l.Info("msg", "a", 1, "b", 2) | ||
l.Debug("bg", Int("a", 1), "b", 2) | ||
l.Warn("w", Duration("dur", 3*time.Second)) | ||
l.Error("bad", "a", 1) | ||
l.Log(ctx, LevelWarn+1, "w", Int("a", 1), String("b", "two")) | ||
l.LogAttrs(ctx, LevelInfo+1, "a b c", Int("a", 1), String("b", "two")) | ||
l.Info("info", "a", []Attr{Int("i", 1)}) | ||
l.Info("info", "a", GroupValue(Int("i", 1))) | ||
} |
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,23 @@ | ||
package slog_test | ||
|
||
import ( | ||
"log/slog" | ||
"log/slog/internal/slogtest" | ||
"os" | ||
) | ||
|
||
func ExampleDiscardHandler() { | ||
// A slog.TextHandler will output logs | ||
logger1 := slog.New(slog.NewTextHandler( | ||
os.Stdout, | ||
&slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}, | ||
)) | ||
logger1.Info("message 1") | ||
|
||
// A slog.DiscardHandler will discard all messages | ||
logger2 := slog.New(slog.DiscardHandler) | ||
logger2.Info("message 2") | ||
|
||
// Output: | ||
// level=INFO msg="message 1" | ||
} |
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