github.com/searKing/golang/go@v1.2.117/log/slog/rotate_handler.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  	"io"
     9  	"log/slog"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	os_ "github.com/searKing/golang/go/os"
    14  	time_ "github.com/searKing/golang/go/time"
    15  )
    16  
    17  // NewHandler creates a slog.Handler that writes to w,
    18  // using the given options.
    19  // If opts is nil, the default options are used.
    20  type NewHandler func(w io.Writer, opts *slog.HandlerOptions) slog.Handler
    21  
    22  // NewRotateHandler creates a slog.Handler that writes to rotate file,
    23  // using the given options.
    24  // If path is empty, the default os.Stdout are used.
    25  // If opts is nil, the default options are used.
    26  func NewRotateHandler(h NewHandler, path string, opts *slog.HandlerOptions, options ...RotateOption) (slog.Handler, error) {
    27  	if path == "" {
    28  		return h(os.Stdout, opts), nil
    29  	}
    30  
    31  	if err := os_.MakeAll(filepath.Dir(path)); err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	var opt rotate
    36  	opt.FilePathRotateLayout = time_.LayoutStrftimeToSimilarTime(".%Y%m%d%H%M%S.log")
    37  	opt.FileLinkPath = filepath.Base(path) + ".log"
    38  	opt.ApplyOptions(options...)
    39  
    40  	file := os_.NewRotateFile(opt.FilePathRotateLayout)
    41  	file.FilePathPrefix = path
    42  	file.FileLinkPath = opt.FileLinkPath
    43  	file.RotateInterval = opt.RotateInterval
    44  	file.RotateSize = opt.RotateSize
    45  	file.MaxAge = opt.MaxAge
    46  	file.MaxCount = opt.MaxCount
    47  	file.ForceNewFileOnStartup = opt.ForceNewFileOnStartup
    48  	file.PostRotateHandler = GlogRotateHeader
    49  	return h(file, opts), nil
    50  }
    51  
    52  // NewRotateJSONHandler creates a JSONHandler that writes to rotate file,
    53  // using the given options.
    54  // If opts is nil, the default options are used.
    55  func NewRotateJSONHandler(path string, opts *slog.HandlerOptions, options ...RotateOption) (slog.Handler, error) {
    56  	return NewRotateHandler(func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
    57  		return slog.NewJSONHandler(w, opts)
    58  	}, path, opts, options...)
    59  }
    60  
    61  // NewRotateTextHandler creates a TextHandler that writes to rotate file,
    62  // using the given options.
    63  // If opts is nil, the default options are used.
    64  func NewRotateTextHandler(path string, opts *slog.HandlerOptions, options ...RotateOption) (slog.Handler, error) {
    65  	return NewRotateHandler(func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
    66  		return slog.NewTextHandler(w, opts)
    67  	}, path, opts, options...)
    68  }
    69  
    70  // NewRotateGlogHandler creates a GlogHandler that writes to rotate file,
    71  // using the given options.
    72  // If opts is nil, the default options are used.
    73  // # LOG LINE PREFIX FORMAT
    74  //
    75  // Log lines have this form:
    76  //
    77  //	Lyyyymmdd hh:mm:ss.uuuuuu threadid file:line] msg...
    78  //
    79  // where the fields are defined as follows:
    80  //
    81  //	L                A single character, representing the log level
    82  //	                 (eg 'I' for INFO)
    83  //	yyyy             The year
    84  //	mm               The month (zero padded; ie May is '05')
    85  //	dd               The day (zero padded)
    86  //	hh:mm:ss.uuuuuu  Time in hours, minutes and fractional seconds
    87  //	threadid         The space-padded thread ID as returned by GetTID()
    88  //	                 (this matches the PID on Linux)
    89  //	file             The file name
    90  //	line             The line number
    91  //	msg              The user-supplied message
    92  //
    93  // Example:
    94  //
    95  //	I1103 11:57:31.739339 24395 google.cc:2341] Command line: ./some_prog
    96  //	I1103 11:57:31.739403 24395 google.cc:2342] Process id 24395
    97  func NewRotateGlogHandler(path string, opts *slog.HandlerOptions, options ...RotateOption) (slog.Handler, error) {
    98  	return NewRotateHandler(func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
    99  		return NewGlogHandler(w, opts)
   100  	}, path, opts, options...)
   101  }
   102  
   103  // NewRotateGlogHumanHandler creates a human-readable GlogHandler that writes to rotate file,
   104  // using the given options.
   105  // If opts is nil, the default options are used.
   106  // # LOG LINE PREFIX FORMAT
   107  //
   108  // Log lines have this form:
   109  //
   110  //	[LLLLL] [yyyymmdd hh:mm:ss.uuuuuu] [threadid] [file:line(func)] msg...
   111  //
   112  // where the fields are defined as follows:
   113  //
   114  //	LLLLL            Five characters, representing the log level
   115  //	                 (eg 'INFO ' for INFO)
   116  //	yyyy             The year
   117  //	mm               The month (zero padded; ie May is '05')
   118  //	dd               The day (zero padded)
   119  //	hh:mm:ss.uuuuuu  Time in hours, minutes and fractional seconds
   120  //	threadid         The space-padded thread ID as returned by GetTID()
   121  //	                 (this matches the PID on Linux)
   122  //	file             The file name
   123  //	line             The line number
   124  //	func             The func name
   125  //	msg              The user-supplied message
   126  //
   127  // Example:
   128  //
   129  //	[INFO] [1103 11:57:31.739339] [24395] [google.cc:2341] Command line: ./some_prog
   130  //	[INFO] [1103 11:57:31.739403 24395] [google.cc:2342] Process id 24395
   131  func NewRotateGlogHumanHandler(path string, opts *slog.HandlerOptions, options ...RotateOption) (slog.Handler, error) {
   132  	return NewRotateHandler(func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
   133  		return NewGlogHumanHandler(w, opts)
   134  	}, path, opts, options...)
   135  }