Source file src/log/slog/example_discard_test.go

     1  package slog_test
     2  
     3  import (
     4  	"log/slog"
     5  	"log/slog/internal/slogtest"
     6  	"os"
     7  )
     8  
     9  func ExampleDiscardHandler() {
    10  	// A slog.TextHandler can output log messages.
    11  	logger1 := slog.New(slog.NewTextHandler(
    12  		os.Stdout,
    13  		&slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime},
    14  	))
    15  	logger1.Info("message 1")
    16  
    17  	// A slog.DiscardHandler will discard all messages.
    18  	logger2 := slog.New(slog.DiscardHandler)
    19  	logger2.Info("message 2")
    20  
    21  	// Output:
    22  	// level=INFO msg="message 1"
    23  }
    24  

View as plain text