github.com/searKing/golang/go@v1.2.117/log/slog/go1.21_example_test.go (about)

     1  // Copyright 2023 The searKing Author. 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  //go:build go1.21 && !go1.22
     6  
     7  package slog
     8  
     9  import (
    10  	"log/slog"
    11  	"os"
    12  
    13  	"github.com/searKing/golang/go/log/slog/internal/slogtest"
    14  )
    15  
    16  func ExampleTypedNil() {
    17  	getPid = func() int { return 0 } // set pid to zero for test
    18  
    19  	defer func() { getPid = os.Getpid }()
    20  	slogNil("text", slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
    21  	slogNil("json", slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
    22  	slogNil("glog", NewGlogHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
    23  	slogNil("glog_human", NewGlogHumanHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
    24  
    25  	// Output:
    26  	// level=INFO msg=[slog/text] attr_typed_nil=<nil> args_typed_nil=<nil>
    27  	// [slog/text] unexpected panic: value method github.com/searKing/golang/go/log/slog.text.MarshalText called using nil *text pointer
    28  	// {"level":"INFO","msg":"[slog/json]","attr_typed_nil":null,"args_typed_nil":null}
    29  	// [slog/json] unexpected panic: value method github.com/searKing/golang/go/log/slog.text.Error called using nil *text pointer
    30  	// I 0] [slog/glog], attr_typed_nil=<nil>, args_typed_nil=<nil>
    31  	// I 0] [slog/glog], attr_typed_nil=<nil>, args_typed_nil=<nil>
    32  	// [INFO ] [0] [slog/glog_human], attr_typed_nil=<nil>, args_typed_nil=<nil>
    33  	// [INFO ] [0] [slog/glog_human], attr_typed_nil=<nil>, args_typed_nil=<nil>
    34  }