github.com/boomhut/fiber/v2@v2.0.0-20230603160335-b65c856e57d3/middleware/logger/config.go (about)

     1  package logger
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/boomhut/fiber/v2"
    10  )
    11  
    12  // Config defines the config for middleware.
    13  type Config struct {
    14  	// Next defines a function to skip this middleware when returned true.
    15  	//
    16  	// Optional. Default: nil
    17  	Next func(c *fiber.Ctx) bool
    18  
    19  	// Done is a function that is called after the log string for a request is written to Output,
    20  	// and pass the log string as parameter.
    21  	//
    22  	// Optional. Default: nil
    23  	Done func(c *fiber.Ctx, logString []byte)
    24  
    25  	// tagFunctions defines the custom tag action
    26  	//
    27  	// Optional. Default: map[string]LogFunc
    28  	CustomTags map[string]LogFunc
    29  
    30  	// Format defines the logging tags
    31  	//
    32  	// Optional. Default: [${time}] ${status} - ${latency} ${method} ${path}\n
    33  	Format string
    34  
    35  	// TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html
    36  	//
    37  	// Optional. Default: 15:04:05
    38  	TimeFormat string
    39  
    40  	// TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc
    41  	//
    42  	// Optional. Default: "Local"
    43  	TimeZone string
    44  
    45  	// TimeInterval is the delay before the timestamp is updated
    46  	//
    47  	// Optional. Default: 500 * time.Millisecond
    48  	TimeInterval time.Duration
    49  
    50  	// Output is a writer where logs are written
    51  	//
    52  	// Default: os.Stdout
    53  	Output io.Writer
    54  
    55  	enableColors     bool
    56  	enableLatency    bool
    57  	timeZoneLocation *time.Location
    58  }
    59  
    60  const (
    61  	startTag       = "${"
    62  	endTag         = "}"
    63  	paramSeparator = ":"
    64  )
    65  
    66  type Buffer interface {
    67  	Len() int
    68  	ReadFrom(r io.Reader) (int64, error)
    69  	WriteTo(w io.Writer) (int64, error)
    70  	Bytes() []byte
    71  	Write(p []byte) (int, error)
    72  	WriteByte(c byte) error
    73  	WriteString(s string) (int, error)
    74  	Set(p []byte)
    75  	SetString(s string)
    76  	String() string
    77  }
    78  
    79  type LogFunc func(output Buffer, c *fiber.Ctx, data *Data, extraParam string) (int, error)
    80  
    81  // ConfigDefault is the default config
    82  var ConfigDefault = Config{
    83  	Next:         nil,
    84  	Done:         nil,
    85  	Format:       "[${time}] ${status} - ${latency} ${method} ${path}\n",
    86  	TimeFormat:   "15:04:05",
    87  	TimeZone:     "Local",
    88  	TimeInterval: 500 * time.Millisecond,
    89  	Output:       os.Stdout,
    90  	enableColors: true,
    91  }
    92  
    93  // Function to check if the logger format is compatible for coloring
    94  func checkColorEnable(format string) bool {
    95  	validTemplates := []string{"${status}", "${method}"}
    96  	for _, template := range validTemplates {
    97  		if strings.Contains(format, template) {
    98  			return true
    99  		}
   100  	}
   101  	return false
   102  }
   103  
   104  // Helper function to set default values
   105  func configDefault(config ...Config) Config {
   106  	// Return default config if nothing provided
   107  	if len(config) < 1 {
   108  		return ConfigDefault
   109  	}
   110  
   111  	// Override default config
   112  	cfg := config[0]
   113  
   114  	// Set default values
   115  	if cfg.Next == nil {
   116  		cfg.Next = ConfigDefault.Next
   117  	}
   118  	if cfg.Done == nil {
   119  		cfg.Done = ConfigDefault.Done
   120  	}
   121  	if cfg.Format == "" {
   122  		cfg.Format = ConfigDefault.Format
   123  	}
   124  
   125  	if cfg.TimeZone == "" {
   126  		cfg.TimeZone = ConfigDefault.TimeZone
   127  	}
   128  	if cfg.TimeFormat == "" {
   129  		cfg.TimeFormat = ConfigDefault.TimeFormat
   130  	}
   131  	if int(cfg.TimeInterval) <= 0 {
   132  		cfg.TimeInterval = ConfigDefault.TimeInterval
   133  	}
   134  	if cfg.Output == nil {
   135  		cfg.Output = ConfigDefault.Output
   136  	}
   137  
   138  	// Enable colors if no custom format or output is given
   139  	if cfg.Output == ConfigDefault.Output && checkColorEnable(cfg.Format) {
   140  		cfg.enableColors = true
   141  	}
   142  
   143  	return cfg
   144  }