default logger for fiber
(c fiber.Ctx, data *Data, cfg *Config)
| 15 | |
| 16 | // default logger for fiber |
| 17 | func defaultLoggerInstance(c fiber.Ctx, data *Data, cfg *Config) error { |
| 18 | if cfg == nil { |
| 19 | cfg = &Config{ |
| 20 | Stream: os.Stdout, |
| 21 | Format: DefaultFormat, |
| 22 | areColorsEnabled: true, |
| 23 | } |
| 24 | } |
| 25 | // Check if Skip is defined and call it. |
| 26 | // Now, if Skip(c) == true, we SKIP logging: |
| 27 | if cfg.Skip != nil && cfg.Skip(c) { |
| 28 | return nil // Skip logging if Skip returns true |
| 29 | } |
| 30 | |
| 31 | // Alias colors |
| 32 | colors := c.App().Config().ColorScheme |
| 33 | |
| 34 | // Get new buffer |
| 35 | buf := bytebufferpool.Get() |
| 36 | |
| 37 | // Default output when no custom Format or io.Writer is given |
| 38 | if cfg.Format == DefaultFormat { |
| 39 | // Format error if exist |
| 40 | formatErr := "" |
| 41 | if cfg.areColorsEnabled { |
| 42 | if data.ChainErr != nil { |
| 43 | formatErr = colors.Red + " | " + data.ChainErr.Error() + colors.Reset |
| 44 | } |
| 45 | fmt.Fprintf( |
| 46 | buf, |
| 47 | "%s |%s %3d %s| %13v | %15s |%s %-7s %s| %-"+data.ErrPaddingStr+"s %s\n", |
| 48 | data.Timestamp, |
| 49 | statusColor(c.Response().StatusCode(), &colors), c.Response().StatusCode(), colors.Reset, |
| 50 | data.Stop.Sub(data.Start), |
| 51 | c.IP(), |
| 52 | methodColor(c.Method(), &colors), c.Method(), colors.Reset, |
| 53 | c.Path(), |
| 54 | formatErr, |
| 55 | ) |
| 56 | } else { |
| 57 | if data.ChainErr != nil { |
| 58 | formatErr = " | " + data.ChainErr.Error() |
| 59 | } |
| 60 | |
| 61 | // Helper function to append fixed-width string with padding |
| 62 | fixedWidth := func(s string, width int, rightAlign bool) { |
| 63 | if rightAlign { |
| 64 | for i := len(s); i < width; i++ { |
| 65 | buf.WriteByte(' ') |
| 66 | } |
| 67 | buf.WriteString(s) |
| 68 | } else { |
| 69 | buf.WriteString(s) |
| 70 | for i := len(s); i < width; i++ { |
| 71 | buf.WriteByte(' ') |
| 72 | } |
| 73 | } |
| 74 | } |