Hao Chun Chang 67021360e1
🔥 Feature: Add AllLogger to Config (#3153)
* 🔥 Feature: Add SetFlags to Logger Interface

🔥 Feature: Add fiberlog Logger field to config

* 🚨 Test: custom-defined Logger and LoggerFunc

* 📚 Doc: add LoggerFunc and Logger to middleware logger

* 🚨 Test: fine-tune custom Logger and LoggerFunc

* 📚 Doc: add Logger documentation

📚 Doc: add custom Logger example

* 🩹 fix: add default Logger field to default config

* 📚 Doc: remove Logger field in middleware logger

📚 Doc: add example of using fiber logger interface

* 🚨 Test: add tests for using fiber logger interface wrapper

* 📚 Doc: update custom logger example

* Update docs/middleware/logger.md

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* update

* update logger docs

* update what's new

* replace setflags with getloggerinstance

* fix linter

* update

* Fix markdownlint issues

* apply reviews & improve coverage

* fix linter

* rename controllogger

* Update whats_new.md

expandable example

---------

Co-authored-by: RW <rene@gofiber.io>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Muhammed Efe Cetin <efectn@protonmail.com>
Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>
2024-12-01 13:32:52 +01:00

89 lines
2.3 KiB
Go

package logger
import (
"io"
"github.com/gofiber/fiber/v3"
fiberlog "github.com/gofiber/fiber/v3/log"
"github.com/gofiber/utils/v2"
)
func methodColor(method string, colors fiber.Colors) string {
switch method {
case fiber.MethodGet:
return colors.Cyan
case fiber.MethodPost:
return colors.Green
case fiber.MethodPut:
return colors.Yellow
case fiber.MethodDelete:
return colors.Red
case fiber.MethodPatch:
return colors.White
case fiber.MethodHead:
return colors.Magenta
case fiber.MethodOptions:
return colors.Blue
default:
return colors.Reset
}
}
func statusColor(code int, colors fiber.Colors) string {
switch {
case code >= fiber.StatusOK && code < fiber.StatusMultipleChoices:
return colors.Green
case code >= fiber.StatusMultipleChoices && code < fiber.StatusBadRequest:
return colors.Blue
case code >= fiber.StatusBadRequest && code < fiber.StatusInternalServerError:
return colors.Yellow
default:
return colors.Red
}
}
type customLoggerWriter struct {
loggerInstance fiberlog.AllLogger
level fiberlog.Level
}
func (cl *customLoggerWriter) Write(p []byte) (int, error) {
switch cl.level {
case fiberlog.LevelTrace:
cl.loggerInstance.Trace(utils.UnsafeString(p))
case fiberlog.LevelDebug:
cl.loggerInstance.Debug(utils.UnsafeString(p))
case fiberlog.LevelInfo:
cl.loggerInstance.Info(utils.UnsafeString(p))
case fiberlog.LevelWarn:
cl.loggerInstance.Warn(utils.UnsafeString(p))
case fiberlog.LevelError:
cl.loggerInstance.Error(utils.UnsafeString(p))
default:
return 0, nil
}
return len(p), nil
}
// LoggerToWriter is a helper function that returns an io.Writer that writes to a custom logger.
// You can integrate 3rd party loggers such as zerolog, logrus, etc. to logger middleware using this function.
//
// Valid levels: fiberlog.LevelInfo, fiberlog.LevelTrace, fiberlog.LevelWarn, fiberlog.LevelDebug, fiberlog.LevelError
func LoggerToWriter(logger fiberlog.AllLogger, level fiberlog.Level) io.Writer {
// Check if customLogger is nil
if logger == nil {
fiberlog.Panic("LoggerToWriter: customLogger must not be nil")
}
// Check if level is valid
if level == fiberlog.LevelFatal || level == fiberlog.LevelPanic {
fiberlog.Panic("LoggerToWriter: invalid level")
}
return &customLoggerWriter{
level: level,
loggerInstance: logger,
}
}