github.com/searKing/golang/go@v1.2.117/log/slog/example_level_handler_test.go (about)

     1  // Copyright 2022 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
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"log/slog"
    11  	"os"
    12  
    13  	"github.com/searKing/golang/go/log/slog/internal/slogtest"
    14  )
    15  
    16  // A LevelHandler wraps a Handler with an Enabled method
    17  // that returns false for levels below a minimum.
    18  type LevelHandler struct {
    19  	level   slog.Leveler
    20  	handler slog.Handler
    21  }
    22  
    23  // NewLevelHandler returns a LevelHandler with the given level.
    24  // All methods except Enabled delegate to h.
    25  func NewLevelHandler(level slog.Leveler, h slog.Handler) *LevelHandler {
    26  	// Optimization: avoid chains of LevelHandlers.
    27  	if lh, ok := h.(*LevelHandler); ok {
    28  		h = lh.Handler()
    29  	}
    30  	return &LevelHandler{level, h}
    31  }
    32  
    33  // Enabled implements Handler.Enabled by reporting whether
    34  // level is at least as large as h's level.
    35  func (h *LevelHandler) Enabled(_ context.Context, level slog.Level) bool {
    36  	return level >= h.level.Level()
    37  }
    38  
    39  // Handle implements Handler.Handle.
    40  func (h *LevelHandler) Handle(ctx context.Context, r slog.Record) error {
    41  	return h.handler.Handle(ctx, r)
    42  }
    43  
    44  // WithAttrs implements Handler.WithAttrs.
    45  func (h *LevelHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
    46  	return NewLevelHandler(h.level, h.handler.WithAttrs(attrs))
    47  }
    48  
    49  // WithGroup implements Handler.WithGroup.
    50  func (h *LevelHandler) WithGroup(name string) slog.Handler {
    51  	return NewLevelHandler(h.level, h.handler.WithGroup(name))
    52  }
    53  
    54  // Handler returns the Handler wrapped by h.
    55  func (h *LevelHandler) Handler() slog.Handler {
    56  	return h.handler
    57  }
    58  
    59  // This example shows how to Use a LevelHandler to change the level of an
    60  // existing Handler while preserving its other behavior.
    61  //
    62  // This example demonstrates increasing the log level to reduce a logger's
    63  // output.
    64  //
    65  // Another typical use would be to decrease the log level (to LevelDebug, say)
    66  // during a part of the program that was suspected of containing a bug.
    67  func ExampleHandler_levelHandler() {
    68  	getPid = func() int { return 0 } // set pid to zero for test
    69  	defer func() { getPid = os.Getpid }()
    70  	{
    71  		fmt.Printf("----text----\n")
    72  		th := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime})
    73  		logger := slog.New(NewLevelHandler(slog.LevelWarn, th))
    74  		logger.Info("not printed")
    75  		logger.Warn("printed")
    76  	}
    77  	{
    78  		fmt.Printf("----glog----\n")
    79  		th := NewGlogHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime})
    80  		logger := slog.New(NewLevelHandler(slog.LevelWarn, th))
    81  		logger.Info("not printed")
    82  		logger.Warn("printed")
    83  	}
    84  	{
    85  		fmt.Printf("----glog_human----\n")
    86  		th := NewGlogHumanHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: slogtest.RemoveTime})
    87  		logger := slog.New(NewLevelHandler(slog.LevelWarn, th))
    88  		logger.Info("not printed")
    89  		logger.Warn("printed")
    90  	}
    91  
    92  	// Output:
    93  	// ----text----
    94  	// level=WARN msg=printed
    95  	// ----glog----
    96  	// W 0] printed
    97  	// ----glog_human----
    98  	// [WARN ] [0] printed
    99  }