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

     1  // Copyright 2024 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  	"os"
    11  	"time"
    12  
    13  	"github.com/searKing/golang/go/log/slog/internal/slogtest"
    14  )
    15  
    16  func ExampleNewRotateTextHandler() {
    17  	getPid = func() int { return 0 } // set pid to zero for test
    18  	defer func() { getPid = os.Getpid }()
    19  
    20  	path := "" // If path is empty, the default os.Stdout are used. path can be relative path or absolute path.
    21  	var slogOpt slog.HandlerOptions
    22  	slogOpt.ReplaceAttr = slogtest.RemoveTime
    23  
    24  	// if set: path = /tmp/logs/slog.log
    25  	// slog.log -> /tmp/logs/slog.20240202180000.log
    26  	// /tmp/logs/slog.20240202170000.log
    27  	// /tmp/logs/slog.20240202180000.log
    28  	// ...
    29  	rotateOpts := []RotateOption{
    30  		WithRotateRotateInterval(time.Hour),
    31  		WithRotateMaxCount(3),
    32  		WithRotateMaxAge(24 * time.Hour),
    33  		// Below is default options.
    34  		// WithRotateFilePathRotateStrftime(".%Y%m%d%H%M%S.log"), // time layout in strftime format to format rotate file.
    35  		// WithRotateFilePathRotateLayout(".20060102150405.log"), // time layout in golang format to format rotate file.
    36  		// WithRotateFileLinkPath(filepath.Base(path) + ".log"), // the symbolic link name that gets linked to the current file name being used.
    37  	}
    38  	// ...
    39  	{
    40  		fmt.Printf("----rotate_text----\n")
    41  		handler, err := NewRotateTextHandler(path,
    42  			&slogOpt, // If opts is nil, the default options are used.
    43  			rotateOpts...)
    44  		if err != nil {
    45  			panic("failed to create rotate text handler:" + err.Error())
    46  		}
    47  		logger := slog.New(handler)
    48  		logger.Info("rotate text message")
    49  	}
    50  
    51  	// Output:
    52  	// ----rotate_text----
    53  	// level=INFO msg="rotate text message"
    54  }
    55  
    56  func ExampleNewRotateJSONHandler() {
    57  	getPid = func() int { return 0 } // set pid to zero for test
    58  	defer func() { getPid = os.Getpid }()
    59  
    60  	path := "" // If path is empty, the default os.Stdout are used. path can be relative path or absolute path.
    61  	var slogOpt slog.HandlerOptions
    62  	slogOpt.ReplaceAttr = slogtest.RemoveTime
    63  
    64  	// if set: path = /tmp/logs/slog.log
    65  	// slog.log -> /tmp/logs/slog.20240202180000.log
    66  	// /tmp/logs/slog.20240202170000.log
    67  	// /tmp/logs/slog.20240202180000.log
    68  	// ...
    69  	rotateOpts := []RotateOption{
    70  		WithRotateRotateInterval(time.Hour),
    71  		WithRotateMaxCount(3),
    72  		WithRotateMaxAge(24 * time.Hour),
    73  		// Below is default options.
    74  		// WithRotateFilePathRotateStrftime(".%Y%m%d%H%M%S.log"), // time layout in strftime format to format rotate file.
    75  		// WithRotateFilePathRotateLayout(".20060102150405.log"), // time layout in golang format to format rotate file.
    76  		// WithRotateFileLinkPath(filepath.Base(path) + ".log"), // the symbolic link name that gets linked to the current file name being used.
    77  	}
    78  	// ...
    79  	{
    80  		fmt.Printf("----rotate_json----\n")
    81  		handler, err := NewRotateJSONHandler(path,
    82  			&slogOpt, // If opts is nil, the default options are used.
    83  			rotateOpts...)
    84  		if err != nil {
    85  			panic("failed to create rotate json handler:" + err.Error())
    86  		}
    87  		logger := slog.New(handler)
    88  		logger.Info("rotate json message")
    89  	}
    90  
    91  	// Output:
    92  	// ----rotate_json----
    93  	// {"level":"INFO","msg":"rotate json message"}
    94  }
    95  
    96  func ExampleNewRotateGlogHandler() {
    97  	getPid = func() int { return 0 } // set pid to zero for test
    98  	defer func() { getPid = os.Getpid }()
    99  
   100  	path := "" // If path is empty, the default os.Stdout are used. path can be relative path or absolute path.
   101  	var slogOpt slog.HandlerOptions
   102  	slogOpt.ReplaceAttr = slogtest.RemoveTime
   103  
   104  	// if set: path = /tmp/logs/slog.log
   105  	// slog.log -> /tmp/logs/slog.20240202180000.log
   106  	// /tmp/logs/slog.20240202170000.log
   107  	// /tmp/logs/slog.20240202180000.log
   108  	// ...
   109  	rotateOpts := []RotateOption{
   110  		WithRotateRotateInterval(time.Hour),
   111  		WithRotateMaxCount(3),
   112  		WithRotateMaxAge(24 * time.Hour),
   113  		// Below is default options.
   114  		// WithRotateFilePathRotateStrftime(".%Y%m%d%H%M%S.log"), // time layout in strftime format to format rotate file.
   115  		// WithRotateFilePathRotateLayout(".20060102150405.log"), // time layout in golang format to format rotate file.
   116  		// WithRotateFileLinkPath(filepath.Base(path) + ".log"), // the symbolic link name that gets linked to the current file name being used.
   117  	}
   118  	// ...
   119  	{
   120  		fmt.Printf("----rotate_glog----\n")
   121  		handler, err := NewRotateGlogHandler(path,
   122  			&slogOpt, // If opts is nil, the default options are used.
   123  			rotateOpts...)
   124  		if err != nil {
   125  			panic("failed to create rotate glog handler:" + err.Error())
   126  		}
   127  		logger := slog.New(handler)
   128  		logger.Info("rotate glog message")
   129  	}
   130  
   131  	// Output:
   132  	// ----rotate_glog----
   133  	// I 0] rotate glog message
   134  }
   135  
   136  func ExampleNewRotateGlogHumanHandler() {
   137  	getPid = func() int { return 0 } // set pid to zero for test
   138  	defer func() { getPid = os.Getpid }()
   139  
   140  	path := "" // If path is empty, the default os.Stdout are used. path can be relative path or absolute path.
   141  	var slogOpt slog.HandlerOptions
   142  	slogOpt.ReplaceAttr = slogtest.RemoveTime
   143  
   144  	// if set: path = /tmp/logs/slog.log
   145  	// slog.log -> /tmp/logs/slog.20240202180000.log
   146  	// /tmp/logs/slog.20240202170000.log
   147  	// /tmp/logs/slog.20240202180000.log
   148  	// ...
   149  	rotateOpts := []RotateOption{
   150  		WithRotateRotateInterval(time.Hour),
   151  		WithRotateMaxCount(3),
   152  		WithRotateMaxAge(24 * time.Hour),
   153  		// Below is default options.
   154  		// WithRotateFilePathRotateStrftime(".%Y%m%d%H%M%S.log"), // time layout in strftime format to format rotate file.
   155  		// WithRotateFilePathRotateLayout(".20060102150405.log"), // time layout in golang format to format rotate file.
   156  		// WithRotateFileLinkPath(filepath.Base(path) + ".log"), // the symbolic link name that gets linked to the current file name being used.
   157  	}
   158  	// ...
   159  	{
   160  		fmt.Printf("----rotate_glog_human----\n")
   161  		handler, err := NewRotateGlogHumanHandler(path,
   162  			&slogOpt, // If opts is nil, the default options are used.
   163  			rotateOpts...)
   164  		if err != nil {
   165  			panic("failed to create rotate human glog handler:" + err.Error())
   166  		}
   167  		logger := slog.New(handler)
   168  		logger.Info("rotate glog_human message")
   169  	}
   170  
   171  	// Output:
   172  	// ----rotate_glog_human----
   173  	// [INFO ] [0] rotate glog_human message
   174  }
   175  
   176  func ExampleNewRotateHandler() {
   177  	getPid = func() int { return 0 } // set pid to zero for test
   178  	defer func() { getPid = os.Getpid }()
   179  
   180  	path := "" // If path is empty, the default os.Stdout are used. path can be relative path or absolute path.
   181  	var slogOpt slog.HandlerOptions
   182  	slogOpt.ReplaceAttr = slogtest.RemoveTime
   183  
   184  	// if set: path = /tmp/logs/slog.log
   185  	// slog.log -> /tmp/logs/slog.20240202180000.log
   186  	// /tmp/logs/slog.20240202170000.log
   187  	// /tmp/logs/slog.20240202180000.log
   188  	// ...
   189  	rotateOpts := []RotateOption{
   190  		WithRotateRotateInterval(time.Hour),
   191  		WithRotateMaxCount(3),
   192  		WithRotateMaxAge(24 * time.Hour),
   193  		// Below is default options.
   194  		// WithRotateFilePathRotateStrftime(".%Y%m%d%H%M%S.log"), // time layout in strftime format to format rotate file.
   195  		// WithRotateFilePathRotateLayout(".20060102150405.log"), // time layout in golang format to format rotate file.
   196  		// WithRotateFileLinkPath(filepath.Base(path) + ".log"), // the symbolic link name that gets linked to the current file name being used.
   197  	}
   198  	// ...
   199  	{
   200  		fmt.Printf("----rotate_text----\n")
   201  		handler, err := NewRotateTextHandler(path,
   202  			&slogOpt, // If opts is nil, the default options are used.
   203  			rotateOpts...)
   204  		if err != nil {
   205  			panic("failed to create rotate text handler:" + err.Error())
   206  		}
   207  		logger := slog.New(handler)
   208  		logger.Info("rotate text message")
   209  	}
   210  	// ...
   211  	{
   212  		fmt.Printf("----rotate_json----\n")
   213  		handler, err := NewRotateJSONHandler(path,
   214  			&slogOpt, // If opts is nil, the default options are used.
   215  			rotateOpts...)
   216  		if err != nil {
   217  			panic("failed to create rotate json handler:" + err.Error())
   218  		}
   219  		logger := slog.New(handler)
   220  		logger.Info("rotate json message")
   221  	}
   222  	// ...
   223  	{
   224  		fmt.Printf("----rotate_glog----\n")
   225  		handler, err := NewRotateGlogHandler(path,
   226  			&slogOpt, // If opts is nil, the default options are used.
   227  			rotateOpts...)
   228  		if err != nil {
   229  			panic("failed to create rotate glog handler:" + err.Error())
   230  		}
   231  		logger := slog.New(handler)
   232  		logger.Info("rotate glog message")
   233  	}
   234  	// ...
   235  	{
   236  		fmt.Printf("----rotate_glog_human----\n")
   237  		handler, err := NewRotateGlogHumanHandler(path,
   238  			&slogOpt, // If opts is nil, the default options are used.
   239  			rotateOpts...)
   240  		if err != nil {
   241  			panic("failed to create rotate human glog handler:" + err.Error())
   242  		}
   243  		logger := slog.New(handler)
   244  		logger.Info("rotate glog_human message")
   245  	}
   246  	// ...
   247  	{
   248  		fmt.Printf("----multi_rotate[text-json-glog-glog_human]----\n")
   249  		logger := slog.New(MultiHandler(
   250  			func() slog.Handler {
   251  				handler, err := NewRotateTextHandler(path,
   252  					&slogOpt, // If opts is nil, the default options are used.
   253  					rotateOpts...)
   254  				if err != nil {
   255  					panic("failed to create rotate text handler:" + err.Error())
   256  				}
   257  				return handler
   258  			}(),
   259  			func() slog.Handler {
   260  				handler, err := NewRotateJSONHandler(path,
   261  					&slogOpt, // If opts is nil, the default options are used.
   262  					rotateOpts...)
   263  				if err != nil {
   264  					panic("failed to create rotate json handler:" + err.Error())
   265  				}
   266  				return handler
   267  			}(),
   268  			func() slog.Handler {
   269  				handler, err := NewRotateGlogHandler(path,
   270  					&slogOpt, // If opts is nil, the default options are used.
   271  					rotateOpts...)
   272  				if err != nil {
   273  					panic("failed to create rotate glog handler:" + err.Error())
   274  				}
   275  				return handler
   276  			}(),
   277  			func() slog.Handler {
   278  				handler, err := NewRotateGlogHumanHandler(path,
   279  					&slogOpt, // If opts is nil, the default options are used.
   280  					rotateOpts...)
   281  				if err != nil {
   282  					panic("failed to create rotate glog_human handler:" + err.Error())
   283  				}
   284  				return handler
   285  			}(),
   286  		))
   287  		logger.Info("rotate multi_rotate[text-json-glog-glog_human] message")
   288  	}
   289  
   290  	// Output:
   291  	// ----rotate_text----
   292  	// level=INFO msg="rotate text message"
   293  	// ----rotate_json----
   294  	// {"level":"INFO","msg":"rotate json message"}
   295  	// ----rotate_glog----
   296  	// I 0] rotate glog message
   297  	// ----rotate_glog_human----
   298  	// [INFO ] [0] rotate glog_human message
   299  	// ----multi_rotate[text-json-glog-glog_human]----
   300  	// level=INFO msg="rotate multi_rotate[text-json-glog-glog_human] message"
   301  	// {"level":"INFO","msg":"rotate multi_rotate[text-json-glog-glog_human] message"}
   302  	// I 0] rotate multi_rotate[text-json-glog-glog_human] message
   303  	// [INFO ] [0] rotate multi_rotate[text-json-glog-glog_human] message
   304  }