Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ [Refactor]: logger/middleware to Eliminate Code Duplication #2581

Closed
3 tasks done
sixcolors opened this issue Aug 16, 2023 · 1 comment
Closed
3 tasks done

Comments

@sixcolors
Copy link
Member

Feature Description

Refactor the logger middleware in Fiber to adher to the "Don't Repeat Yourself" (DRY) principle. During recent discussions #2561, it was identified that the current implementation involves duplicated code for custom and default logger functions.

Additional Context (optional)

Code Refactoring: The primary focus is to eliminate code duplication by consolidating the common logic shared between custom and default logger functions. This not only improves code maintainability but also ensures consistent behavior across different configurations.

Code Snippet (optional)

// Default output when no custom Format or io.Writer is given
		if cfg.Format == ConfigDefault.Format {
			// Format error if exist
			formatErr := ""
			if cfg.enableColors {
				if chainErr != nil {
					formatErr = colors.Red + " | " + chainErr.Error() + colors.Reset
				}
				_, _ = buf.WriteString( //nolint:errcheck // This will never fail
					fmt.Sprintf("%s |%s %3d %s| %7v | %15s |%s %-7s %s| %-"+errPaddingStr+"s %s\n",
						timestamp.Load().(string),
						statusColor(c.Response().StatusCode(), colors), c.Response().StatusCode(), colors.Reset,
						data.Stop.Sub(data.Start).Round(time.Millisecond),
						c.IP(),
						methodColor(c.Method(), colors), c.Method(), colors.Reset,
						c.Path(),
						formatErr,
					),
				)
			} else {
				if chainErr != nil {
					formatErr = " | " + chainErr.Error()
				}
				_, _ = buf.WriteString( //nolint:errcheck // This will never fail
					fmt.Sprintf("%s | %3d | %7v | %15s | %-7s | %-"+errPaddingStr+"s %s\n",
						timestamp.Load().(string),
						c.Response().StatusCode(),
						data.Stop.Sub(data.Start).Round(time.Millisecond),
						c.IP(),
						c.Method(),
						c.Path(),
						formatErr,
					),
				)
			}

			// Write buffer to output
			_, _ = cfg.Output.Write(buf.Bytes()) //nolint:errcheck // This will never fail

			if cfg.Done != nil {
				cfg.Done(c, buf.Bytes())
			}

			// Put buffer back to pool
			bytebufferpool.Put(buf)

			// End chain
			return nil
		}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my suggestion prior to opening this one.
  • I understand that improperly formatted feature requests may be closed without explanation.
@sixcolors sixcolors self-assigned this Aug 16, 2023
@sixcolors sixcolors changed the title 🚀 [Refactor]: logger/middleware to Eliminate Code Duplication ♻️ [Refactor]: logger/middleware to Eliminate Code Duplication Aug 17, 2023
@Skyenought Skyenought self-assigned this Aug 22, 2023
@brunodmartins
Copy link
Contributor

This whole block of code showed on the snippet was removed on #2730

@efectn efectn closed this as completed Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants