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