Source file src/log/slog/example_discard_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package slog_test
     6  
     7  import (
     8  	"log/slog"
     9  	"log/slog/internal/slogtest"
    10  	"os"
    11  )
    12  
    13  func ExampleDiscardHandler() {
    14  	// A slog.TextHandler can output log messages.
    15  	logger1 := slog.New(slog.NewTextHandler(
    16  		os.Stdout,
    17  		&slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime},
    18  	))
    19  	logger1.Info("message 1")
    20  
    21  	// A slog.DiscardHandler will discard all messages.
    22  	logger2 := slog.New(slog.DiscardHandler)
    23  	logger2.Info("message 2")
    24  
    25  	// Output:
    26  	// level=INFO msg="message 1"
    27  }
    28  

View as plain text