MCPcopy
hub / github.com/gofiber/fiber / defaultLoggerInstance

Function defaultLoggerInstance

middleware/logger/default_logger.go:17–136  ·  view source on GitHub ↗

default logger for fiber

(c fiber.Ctx, data *Data, cfg *Config)

Source from the content-addressed store, hash-verified

15
16// default logger for fiber
17func 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 }

Callers 2

Test_Logger_WithLatencyFunction · 0.85

Calls 15

ExecuteChainsFunction · 0.92
statusColorFunction · 0.85
methodColorFunction · 0.85
writeLogFunction · 0.85
ConfigMethod · 0.65
AppMethod · 0.65
GetMethod · 0.65
ErrorMethod · 0.65
ResponseMethod · 0.65
IPMethod · 0.65
MethodMethod · 0.65
PathMethod · 0.65

Tested by 2

Test_Logger_WithLatencyFunction · 0.68