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

🔥 feat(logger): Add predefined log formats #3359

Merged
merged 16 commits into from
Mar 21, 2025
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(logger): Add CustomFormat option
This commit introduces a `CustomFormat` option to the `Config` struct, allowing users to specify a predefined format (like "common", "combined", "json", or "ecs")
edvardsanta committed Mar 21, 2025
commit 7136389e5828d9187bd3079e62d7af62da1a1735
12 changes: 8 additions & 4 deletions middleware/logger/config.go
Original file line number Diff line number Diff line change
@@ -57,14 +57,18 @@ type Config struct {
// The full list of available placeholders can be found in 'tags.go' or at
// 'https://docs.gofiber.io/api/middleware/logger/#constants'.
//
// Alternatively, you can use one of the predefined formats:
// If no format is specified, the default format is used: "[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}"
// If both `Format` and `CustomFormat` are provided, the `CustomFormat` will be used, and the `Format` field will be ignored.
Format string

// You can use one of the predefined formats:
// - "default" → Uses the default log format: "[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}"
// - "common" → Uses the Common Log Format (CLF): "${ip} - - [${time}] "${method} ${url} ${protocol}" ${status} ${bytesSent}"
// - "combined" → Uses the Combined Log Format: "${ip} - - [${time}] "${method} ${url} ${protocol}" ${status} ${bytesSent} "${referer}" "${ua}""
// - "json" → Uses the JSON structured log format: "{"time":"${time}","ip":"${ip}","method":"${method}","url":"${url}","status":${status},"bytesSent":${bytesSent}}"
//
// If no format is specified, the default format is used: "[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}"
Format string
// - "ecs" → Uses the Elastic Common Schema (ECS) log format: "{"@timestamp":"${time}","ecs":{"version":"1.6.0"},"client":{"ip":"${ip}"},"http":{"request":{"method":"${method}","url":"${url}","protocol":"${protocol}"},"response":{"status_code":${status},"body":{"bytes":${bytesSent}}}}, "log":{"level":"INFO","logger":"fiber"},"message":"${method} ${url} responded with ${status}"}"
// If both `Format` and `CustomFormat` are provided, the `CustomFormat` will be used, and the `Format` field will be ignored.
CustomFormat string

// TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html
//
8 changes: 4 additions & 4 deletions middleware/logger/format.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package logger

const (
// Fiber's default logger `
// Fiber's default logger
FormatDefault = "[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n"
// Common log format
// Apache Common Log Format (CLF)
FormatCommonLog = "${ip} - - [${time}] \"${method} ${url} ${protocol}\" ${status} ${bytesSent}\n"
// Combined log format
// Apache Combined Log Format
FormatCombined = "${ip} - - [${time}] \"${method} ${url} ${protocol}\" ${status} ${bytesSent} \"${referer}\" \"${ua}\"\n"
// JSON log formats
FormatJSON = "{\"time\":\"${time}\",\"ip\":\"${ip}\",\"method\":\"${method}\",\"url\":\"${url}\",\"status\":${status},\"bytesSent\":${bytesSent}}\n"
// ECS Log format
// Elastic Common Schema (ECS) Log Format
FormatECS = "{\"@timestamp\":\"${time}\",\"ecs\":{\"version\":\"1.6.0\"},\"client\":{\"ip\":\"${ip}\"},\"http\":{\"request\":{\"method\":\"${method}\",\"url\":\"${url}\",\"protocol\":\"${protocol}\"},\"response\":{\"status_code\":${status},\"body\":{\"bytes\":${bytesSent}}}},\"log\":{\"level\":\"INFO\",\"logger\":\"fiber\"},\"message\":\"${method} ${url} responded with ${status}\"}\n"
)

2 changes: 1 addition & 1 deletion middleware/logger/logger.go
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ func New(config ...Config) fiber.Handler {
// Create correct timeformat
timestamp.Store(time.Now().In(cfg.timeZoneLocation).Format(cfg.TimeFormat))

if logFormat, exists := LoggerConfig[cfg.Format]; exists {
if logFormat, exists := LoggerConfig[cfg.CustomFormat]; exists {
cfg.Format = logFormat
}

16 changes: 8 additions & 8 deletions middleware/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -475,8 +475,8 @@ func Test_Logger_CLF_Format_With_Name(t *testing.T) {
app := fiber.New()

app.Use(New(Config{
Format: "common",
Stream: buf,
CustomFormat: "common",
Stream: buf,
}))

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil))
@@ -525,8 +525,8 @@ func Test_Logger_Combined_CLF_Format_With_Name(t *testing.T) {
app := fiber.New()

app.Use(New(Config{
Format: "combined",
Stream: buf,
CustomFormat: "combined",
Stream: buf,
}))
const expectedUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
const expectedReferer = "http://example.com"
@@ -587,8 +587,8 @@ func Test_Logger_Json_Format_With_Name(t *testing.T) {
app := fiber.New()

app.Use(New(Config{
Format: "json",
Stream: buf,
CustomFormat: "json",
Stream: buf,
}))

req := httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil)
@@ -647,8 +647,8 @@ func Test_Logger_ECS_Format_With_Name(t *testing.T) {
app := fiber.New()

app.Use(New(Config{
Format: "ecs",
Stream: buf,
CustomFormat: "ecs",
Stream: buf,
}))

req := httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil)