github.com/boomhut/fiber/v2@v2.0.0-20230603160335-b65c856e57d3/middleware/logger/logger.go (about) 1 package logger 2 3 import ( 4 "fmt" 5 "os" 6 "strconv" 7 "strings" 8 "sync" 9 "sync/atomic" 10 "time" 11 12 "github.com/boomhut/fiber/v2" 13 "github.com/boomhut/fiber/v2/utils" 14 15 "github.com/mattn/go-colorable" 16 "github.com/mattn/go-isatty" 17 "github.com/valyala/bytebufferpool" 18 "github.com/valyala/fasthttp" 19 ) 20 21 // New creates a new middleware handler 22 func New(config ...Config) fiber.Handler { 23 // Set default config 24 cfg := configDefault(config...) 25 26 // Get timezone location 27 tz, err := time.LoadLocation(cfg.TimeZone) 28 if err != nil || tz == nil { 29 cfg.timeZoneLocation = time.Local 30 } else { 31 cfg.timeZoneLocation = tz 32 } 33 34 // Check if format contains latency 35 cfg.enableLatency = strings.Contains(cfg.Format, "${"+TagLatency+"}") 36 37 var timestamp atomic.Value 38 // Create correct timeformat 39 timestamp.Store(time.Now().In(cfg.timeZoneLocation).Format(cfg.TimeFormat)) 40 41 // Update date/time every 500 milliseconds in a separate go routine 42 if strings.Contains(cfg.Format, "${"+TagTime+"}") { 43 go func() { 44 for { 45 time.Sleep(cfg.TimeInterval) 46 timestamp.Store(time.Now().In(cfg.timeZoneLocation).Format(cfg.TimeFormat)) 47 } 48 }() 49 } 50 51 // Set PID once 52 pid := strconv.Itoa(os.Getpid()) 53 54 // Set variables 55 var ( 56 once sync.Once 57 mu sync.Mutex 58 errHandler fiber.ErrorHandler 59 60 dataPool = sync.Pool{New: func() interface{} { return new(Data) }} 61 ) 62 63 // If colors are enabled, check terminal compatibility 64 if cfg.enableColors { 65 cfg.Output = colorable.NewColorableStdout() 66 if os.Getenv("TERM") == "dumb" || os.Getenv("NO_COLOR") == "1" || (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) { 67 cfg.Output = colorable.NewNonColorable(os.Stdout) 68 } 69 } 70 errPadding := 15 71 errPaddingStr := strconv.Itoa(errPadding) 72 73 // instead of analyzing the template inside(handler) each time, this is done once before 74 // and we create several slices of the same length with the functions to be executed and fixed parts. 75 templateChain, logFunChain, err := buildLogFuncChain(&cfg, createTagMap(&cfg)) 76 if err != nil { 77 panic(err) 78 } 79 80 // Return new handler 81 return func(c *fiber.Ctx) error { 82 // Don't execute middleware if Next returns true 83 if cfg.Next != nil && cfg.Next(c) { 84 return c.Next() 85 } 86 87 // Alias colors 88 colors := c.App().Config().ColorScheme 89 90 // Set error handler once 91 once.Do(func() { 92 // get longested possible path 93 stack := c.App().Stack() 94 for m := range stack { 95 for r := range stack[m] { 96 if len(stack[m][r].Path) > errPadding { 97 errPadding = len(stack[m][r].Path) 98 errPaddingStr = strconv.Itoa(errPadding) 99 } 100 } 101 } 102 // override error handler 103 errHandler = c.App().ErrorHandler 104 }) 105 106 // Logger data 107 data := dataPool.Get().(*Data) //nolint:forcetypeassert,errcheck // We store nothing else in the pool 108 // no need for a reset, as long as we always override everything 109 data.Pid = pid 110 data.ErrPaddingStr = errPaddingStr 111 data.Timestamp = timestamp 112 // put data back in the pool 113 defer dataPool.Put(data) 114 115 // Set latency start time 116 if cfg.enableLatency { 117 data.Start = time.Now() 118 } 119 120 // Handle request, store err for logging 121 chainErr := c.Next() 122 123 data.ChainErr = chainErr 124 // Manually call error handler 125 if chainErr != nil { 126 if err := errHandler(c, chainErr); err != nil { 127 _ = c.SendStatus(fiber.StatusInternalServerError) //nolint:errcheck // TODO: Explain why we ignore the error here 128 } 129 } 130 131 // Set latency stop time 132 if cfg.enableLatency { 133 data.Stop = time.Now() 134 } 135 136 // Get new buffer 137 buf := bytebufferpool.Get() 138 139 // Default output when no custom Format or io.Writer is given 140 if cfg.enableColors && cfg.Format == ConfigDefault.Format { 141 // Format error if exist 142 formatErr := "" 143 if chainErr != nil { 144 formatErr = colors.Red + " | " + chainErr.Error() + colors.Reset 145 } 146 147 // Format log to buffer 148 _, _ = buf.WriteString( //nolint:errcheck // This will never fail 149 fmt.Sprintf("%s |%s %3d %s| %7v | %15s |%s %-7s %s| %-"+errPaddingStr+"s %s\n", 150 timestamp.Load().(string), 151 statusColor(c.Response().StatusCode(), colors), c.Response().StatusCode(), colors.Reset, 152 data.Stop.Sub(data.Start).Round(time.Millisecond), 153 c.IP(), 154 methodColor(c.Method(), colors), c.Method(), colors.Reset, 155 c.Path(), 156 formatErr, 157 ), 158 ) 159 160 // Write buffer to output 161 _, _ = cfg.Output.Write(buf.Bytes()) //nolint:errcheck // This will never fail 162 163 if cfg.Done != nil { 164 cfg.Done(c, buf.Bytes()) 165 } 166 167 // Put buffer back to pool 168 bytebufferpool.Put(buf) 169 170 // End chain 171 return nil 172 } 173 174 var err error 175 // Loop over template parts execute dynamic parts and add fixed parts to the buffer 176 for i, logFunc := range logFunChain { 177 if logFunc == nil { 178 _, _ = buf.Write(templateChain[i]) //nolint:errcheck // This will never fail 179 } else if templateChain[i] == nil { 180 _, err = logFunc(buf, c, data, "") 181 } else { 182 _, err = logFunc(buf, c, data, utils.UnsafeString(templateChain[i])) 183 } 184 if err != nil { 185 break 186 } 187 } 188 189 // Also write errors to the buffer 190 if err != nil { 191 _, _ = buf.WriteString(err.Error()) //nolint:errcheck // This will never fail 192 } 193 mu.Lock() 194 // Write buffer to output 195 if _, err := cfg.Output.Write(buf.Bytes()); err != nil { 196 // Write error to output 197 if _, err := cfg.Output.Write([]byte(err.Error())); err != nil { 198 // There is something wrong with the given io.Writer 199 _, _ = fmt.Fprintf(os.Stderr, "Failed to write to log, %v\n", err) 200 } 201 } 202 mu.Unlock() 203 204 if cfg.Done != nil { 205 cfg.Done(c, buf.Bytes()) 206 } 207 208 // Put buffer back to pool 209 bytebufferpool.Put(buf) 210 211 return nil 212 } 213 } 214 215 func appendInt(output Buffer, v int) (int, error) { 216 old := output.Len() 217 output.Set(fasthttp.AppendUint(output.Bytes(), v)) 218 return output.Len() - old, nil 219 }