github.com/searKing/golang/go@v1.2.117/log/slog/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  package slog
     6  
     7  import (
     8  	"fmt"
     9  	"log/slog"
    10  	"net/http"
    11  	"os"
    12  	"time"
    13  
    14  	"github.com/searKing/golang/go/exp/types"
    15  	"github.com/searKing/golang/go/log/slog/internal/slogtest"
    16  )
    17  
    18  func slogNil(name string, h slog.Handler) {
    19  	defer func() {
    20  		if err := recover(); err != nil {
    21  			// in go1.21, [slog.TextHandler] and [slog.JSONHandler] will panic
    22  			// https://github.com/golang/go/commit/73667209c1c83bd48fe7338c3b4caaa05c073202
    23  			fmt.Printf("[slog/%s] unexpected panic: %v\n", name, err)
    24  		}
    25  	}()
    26  	logger := slog.New(h)
    27  	{
    28  		var typedNil *text
    29  		logger.With("attr_typed_nil", types.Any(typedNil)).Info(fmt.Sprintf("[slog/%s]", name), "args_typed_nil", types.Any(typedNil))
    30  		logger.With("attr_typed_nil", typedNil).Info(fmt.Sprintf("[slog/%s]", name), "args_typed_nil", typedNil)
    31  	}
    32  }
    33  
    34  func ExampleGroup() {
    35  	getPid = func() int { return 0 } // set pid to zero for test
    36  	defer func() { getPid = os.Getpid }()
    37  
    38  	r, _ := http.NewRequest("GET", "localhost", nil)
    39  	err := &os.PathError{
    40  		Op:   "test",
    41  		Path: "ExampleGroup",
    42  		Err:  os.ErrInvalid,
    43  	}
    44  	// ...
    45  	{
    46  		fmt.Printf("----text----\n")
    47  		logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
    48  		logger.Info("finished",
    49  			slog.Group("req",
    50  				slog.String("method", r.Method),
    51  				slog.String("url", r.URL.String())),
    52  			slog.Int("status", http.StatusOK),
    53  			slog.Duration("duration", time.Second),
    54  			Error(err))
    55  	}
    56  	// ...
    57  	{
    58  		fmt.Printf("----json----\n")
    59  		logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
    60  		logger.Info("finished",
    61  			slog.Group("req",
    62  				slog.String("method", r.Method),
    63  				slog.String("url", r.URL.String())),
    64  			slog.Int("status", http.StatusOK),
    65  			slog.Duration("duration", time.Second),
    66  			Error(err))
    67  	}
    68  	// ...
    69  	{
    70  		fmt.Printf("----glog----\n")
    71  		logger := slog.New(NewGlogHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
    72  		logger.Info("finished",
    73  			slog.Group("req",
    74  				slog.String("method", r.Method),
    75  				slog.String("url", r.URL.String())),
    76  			slog.Int("status", http.StatusOK),
    77  			slog.Duration("duration", time.Second),
    78  			Error(err))
    79  	}
    80  	// ...
    81  	{
    82  		fmt.Printf("----glog_human----\n")
    83  		logger := slog.New(NewGlogHumanHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
    84  		logger.Info("finished",
    85  			slog.Group("req",
    86  				slog.String("method", r.Method),
    87  				slog.String("url", r.URL.String())),
    88  			slog.Int("status", http.StatusOK),
    89  			slog.Duration("duration", time.Second),
    90  			Error(err))
    91  	}
    92  	// ...
    93  	{
    94  		fmt.Printf("----multi[text-json-glog-glog_human]----\n")
    95  		logger := slog.New(MultiHandler(
    96  			slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}),
    97  			slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}),
    98  			NewGlogHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}),
    99  			NewGlogHumanHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}),
   100  		))
   101  		logger.Info("finished",
   102  			slog.Group("req",
   103  				slog.String("method", r.Method),
   104  				slog.String("url", r.URL.String())),
   105  			slog.Int("status", http.StatusOK),
   106  			slog.Duration("duration", time.Second),
   107  			Error(err))
   108  	}
   109  
   110  	// Output:
   111  	// ----text----
   112  	// level=INFO msg=finished req.method=GET req.url=localhost status=200 duration=1s error="test ExampleGroup: invalid argument"
   113  	// ----json----
   114  	// {"level":"INFO","msg":"finished","req":{"method":"GET","url":"localhost"},"status":200,"duration":1000000000,"error":"test ExampleGroup: invalid argument"}
   115  	// ----glog----
   116  	// I 0] finished, req.method=GET, req.url=localhost, status=200, duration=1s, error=test ExampleGroup: invalid argument
   117  	// ----glog_human----
   118  	// [INFO ] [0] finished, req.method=GET, req.url=localhost, status=200, duration=1s, error=test ExampleGroup: invalid argument
   119  	// ----multi[text-json-glog-glog_human]----
   120  	// level=INFO msg=finished req.method=GET req.url=localhost status=200 duration=1s error="test ExampleGroup: invalid argument"
   121  	// {"level":"INFO","msg":"finished","req":{"method":"GET","url":"localhost"},"status":200,"duration":1000000000,"error":"test ExampleGroup: invalid argument"}
   122  	// I 0] finished, req.method=GET, req.url=localhost, status=200, duration=1s, error=test ExampleGroup: invalid argument
   123  	// [INFO ] [0] finished, req.method=GET, req.url=localhost, status=200, duration=1s, error=test ExampleGroup: invalid argument
   124  }
   125  
   126  func ExampleMultiHandler() {
   127  	getPid = func() int { return 0 } // set pid to zero for test
   128  	defer func() { getPid = os.Getpid }()
   129  
   130  	// ...
   131  	{
   132  		fmt.Printf("----text----\n")
   133  		logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
   134  		logger.Info("text message")
   135  	}
   136  	// ...
   137  	{
   138  		fmt.Printf("----json----\n")
   139  		logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
   140  		logger.Info("json message")
   141  	}
   142  	// ...
   143  	{
   144  		fmt.Printf("----glog----\n")
   145  		logger := slog.New(NewGlogHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
   146  		logger.Info("glog message")
   147  	}
   148  	// ...
   149  	{
   150  		fmt.Printf("----glog_human----\n")
   151  		logger := slog.New(NewGlogHumanHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}))
   152  		logger.Info("glog_human message")
   153  	}
   154  	// ...
   155  	{
   156  		fmt.Printf("----multi[text-json-glog-glog_human]----\n")
   157  		logger := slog.New(MultiHandler(
   158  			slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}),
   159  			slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}),
   160  			NewGlogHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}),
   161  			NewGlogHumanHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime}),
   162  		))
   163  		logger.Info("multi[text-json-glog-glog_human] message")
   164  	}
   165  
   166  	// Output:
   167  	// ----text----
   168  	// level=INFO msg="text message"
   169  	// ----json----
   170  	// {"level":"INFO","msg":"json message"}
   171  	// ----glog----
   172  	// I 0] glog message
   173  	// ----glog_human----
   174  	// [INFO ] [0] glog_human message
   175  	// ----multi[text-json-glog-glog_human]----
   176  	// level=INFO msg="multi[text-json-glog-glog_human] message"
   177  	// {"level":"INFO","msg":"multi[text-json-glog-glog_human] message"}
   178  	// I 0] multi[text-json-glog-glog_human] message
   179  	// [INFO ] [0] multi[text-json-glog-glog_human] message
   180  }