Home / Function/ defaultLoggerInstance() — fiber Function Reference

defaultLoggerInstance() — fiber Function Reference

Architecture documentation for the defaultLoggerInstance() function in default_logger.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  62447a6b_d8b7_117c_6c48_f8d314f87d51["defaultLoggerInstance()"]
  bbe7abe4_a8ff_16f2_cc00_3fc20e652f9f["default_logger.go"]
  62447a6b_d8b7_117c_6c48_f8d314f87d51 -->|defined in| bbe7abe4_a8ff_16f2_cc00_3fc20e652f9f
  54409f15_997f_e1a8_e74a_200d306ec8ea["writeLog()"]
  62447a6b_d8b7_117c_6c48_f8d314f87d51 -->|calls| 54409f15_997f_e1a8_e74a_200d306ec8ea
  style 62447a6b_d8b7_117c_6c48_f8d314f87d51 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/logger/default_logger.go lines 17–150

func defaultLoggerInstance(c fiber.Ctx, data *Data, cfg *Config) error {
	if cfg == nil {
		cfg = &Config{
			Stream:       os.Stdout,
			Format:       DefaultFormat,
			enableColors: true,
		}
	}
	// Check if Skip is defined and call it.
	// Now, if Skip(c) == true, we SKIP logging:
	if cfg.Skip != nil && cfg.Skip(c) {
		return nil // Skip logging if Skip returns true
	}

	// Alias colors
	colors := c.App().Config().ColorScheme

	// Get new buffer
	buf := bytebufferpool.Get()

	// Default output when no custom Format or io.Writer is given
	if cfg.Format == DefaultFormat {
		// Format error if exist
		formatErr := ""
		if cfg.enableColors {
			if data.ChainErr != nil {
				formatErr = colors.Red + " | " + data.ChainErr.Error() + colors.Reset
			}
			fmt.Fprintf(buf,
				"%s |%s %3d %s| %13v | %15s |%s %-7s %s| %-"+data.ErrPaddingStr+"s %s\n",
				data.Timestamp.Load().(string), //nolint:forcetypeassert,errcheck // Timestamp is always a string
				statusColor(c.Response().StatusCode(), &colors), c.Response().StatusCode(), colors.Reset,
				data.Stop.Sub(data.Start),
				c.IP(),
				methodColor(c.Method(), &colors), c.Method(), colors.Reset,
				c.Path(),
				formatErr,
			)
		} else {
			if data.ChainErr != nil {
				formatErr = " | " + data.ChainErr.Error()
			}

			// Helper function to append fixed-width string with padding
			fixedWidth := func(s string, width int, rightAlign bool) {
				if rightAlign {
					for i := len(s); i < width; i++ {
						buf.WriteByte(' ')
					}
					buf.WriteString(s)
				} else {
					buf.WriteString(s)
					for i := len(s); i < width; i++ {
						buf.WriteByte(' ')
					}
				}
			}

			// Timestamp
			buf.WriteString(data.Timestamp.Load().(string)) //nolint:forcetypeassert,errcheck // Timestamp is always a string
			buf.WriteString(" | ")

			// Status Code with 3 fixed width, right aligned
			fixedWidth(strconv.Itoa(c.Response().StatusCode()), 3, true)
			buf.WriteString(" | ")

			// Duration with 13 fixed width, right aligned
			fixedWidth(data.Stop.Sub(data.Start).String(), 13, true)
			buf.WriteString(" | ")

			// Client IP with 15 fixed width, right aligned
			fixedWidth(c.IP(), 15, true)
			buf.WriteString(" | ")

			// HTTP Method with 7 fixed width, left aligned
			fixedWidth(c.Method(), 7, false)
			buf.WriteString(" | ")

			// Path with dynamic padding for error message, left aligned
			errPadding, _ := strconv.Atoi(data.ErrPaddingStr) //nolint:errcheck // It is fine to ignore the error
			fixedWidth(c.Path(), errPadding, false)

Domain

Subdomains

Calls

Frequently Asked Questions

What does defaultLoggerInstance() do?
defaultLoggerInstance() is a function in the fiber codebase, defined in middleware/logger/default_logger.go.
Where is defaultLoggerInstance() defined?
defaultLoggerInstance() is defined in middleware/logger/default_logger.go at line 17.
What does defaultLoggerInstance() call?
defaultLoggerInstance() calls 1 function(s): writeLog.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free