mirror of https://github.com/gofiber/fiber.git
feat(logger): Remove CustomFormat option and renamed Format consts
- Removed `CustomFormat` field from `Config`. - Removed `LoggerConfig` map. - Rename predefined formats constants.pull/3359/head
parent
c325e3345f
commit
1743c6862c
|
@ -61,15 +61,6 @@ type Config struct {
|
|||
// 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}}"
|
||||
// - "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
|
||||
//
|
||||
// Optional. Default: 15:04:05
|
||||
|
@ -120,7 +111,7 @@ var ConfigDefault = Config{
|
|||
Next: nil,
|
||||
Skip: nil,
|
||||
Done: nil,
|
||||
Format: FormatDefault,
|
||||
Format: DefaultFormat,
|
||||
TimeFormat: "15:04:05",
|
||||
TimeZone: "Local",
|
||||
TimeInterval: 500 * time.Millisecond,
|
||||
|
|
|
@ -28,7 +28,7 @@ func defaultLoggerInstance(c fiber.Ctx, data *Data, cfg Config) error {
|
|||
buf := bytebufferpool.Get()
|
||||
|
||||
// Default output when no custom Format or io.Writer is given
|
||||
if cfg.Format == FormatDefault {
|
||||
if cfg.Format == DefaultFormat {
|
||||
// Format error if exist
|
||||
formatErr := ""
|
||||
if cfg.enableColors {
|
||||
|
|
|
@ -2,24 +2,13 @@ package logger
|
|||
|
||||
const (
|
||||
// Fiber's default logger
|
||||
FormatDefault = "[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n"
|
||||
DefaultFormat = "[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n"
|
||||
// Apache Common Log Format (CLF)
|
||||
FormatCommonLog = "${ip} - - [${time}] \"${method} ${url} ${protocol}\" ${status} ${bytesSent}\n"
|
||||
CommonFormat = "${ip} - - [${time}] \"${method} ${url} ${protocol}\" ${status} ${bytesSent}\n"
|
||||
// Apache Combined Log Format
|
||||
FormatCombined = "${ip} - - [${time}] \"${method} ${url} ${protocol}\" ${status} ${bytesSent} \"${referer}\" \"${ua}\"\n"
|
||||
CombinedFormat = "${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"
|
||||
JSONFormat = "{\"time\":\"${time}\",\"ip\":\"${ip}\",\"method\":\"${method}\",\"url\":\"${url}\",\"status\":${status},\"bytesSent\":${bytesSent}}\n"
|
||||
// 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"
|
||||
ECSFormat = "{\"@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"
|
||||
)
|
||||
|
||||
// LoggerConfig provides a mapping of predefined log format configurations
|
||||
// that can be used to customize log output styles. The map keys represent
|
||||
// different log format types, and the values are the corresponding format strings.
|
||||
var LoggerConfig = map[string]string{
|
||||
"default": FormatDefault,
|
||||
"common": FormatCommonLog,
|
||||
"combined": FormatCombined,
|
||||
"json": FormatJSON,
|
||||
"ecs": FormatECS,
|
||||
}
|
||||
|
|
|
@ -31,10 +31,6 @@ func New(config ...Config) fiber.Handler {
|
|||
// Create correct timeformat
|
||||
timestamp.Store(time.Now().In(cfg.timeZoneLocation).Format(cfg.TimeFormat))
|
||||
|
||||
if logFormat, exists := LoggerConfig[cfg.CustomFormat]; exists {
|
||||
cfg.Format = logFormat
|
||||
}
|
||||
|
||||
// Update date/time every 500 milliseconds in a separate go routine
|
||||
if strings.Contains(cfg.Format, "${"+TagTime+"}") {
|
||||
go func() {
|
||||
|
|
|
@ -467,7 +467,7 @@ func Test_Logger_All(t *testing.T) {
|
|||
require.Equal(t, expected, buf.String())
|
||||
}
|
||||
|
||||
func Test_Logger_CLF_Format_With_Name(t *testing.T) {
|
||||
func Test_Logger_CLF_Format(t *testing.T) {
|
||||
t.Parallel()
|
||||
buf := bytebufferpool.Get()
|
||||
defer bytebufferpool.Put(buf)
|
||||
|
@ -475,32 +475,7 @@ func Test_Logger_CLF_Format_With_Name(t *testing.T) {
|
|||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
CustomFormat: "common",
|
||||
Stream: buf,
|
||||
}))
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
||||
|
||||
expected := fmt.Sprintf("0.0.0.0 - - [%s] \"%s %s %s\" %d %d\n",
|
||||
time.Now().Format("15:04:05"),
|
||||
fiber.MethodGet, "/?foo=bar", "HTTP/1.1",
|
||||
fiber.StatusNotFound,
|
||||
0)
|
||||
logResponse := buf.String()
|
||||
require.Equal(t, expected, logResponse)
|
||||
}
|
||||
|
||||
func Test_Logger_CLF_Format_With_Const(t *testing.T) {
|
||||
t.Parallel()
|
||||
buf := bytebufferpool.Get()
|
||||
defer bytebufferpool.Put(buf)
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
Format: FormatCommonLog,
|
||||
Format: CommonFormat,
|
||||
Stream: buf,
|
||||
}))
|
||||
|
||||
|
@ -517,7 +492,7 @@ func Test_Logger_CLF_Format_With_Const(t *testing.T) {
|
|||
require.Equal(t, expected, logResponse)
|
||||
}
|
||||
|
||||
func Test_Logger_Combined_CLF_Format_With_Name(t *testing.T) {
|
||||
func Test_Logger_Combined_CLF_Format(t *testing.T) {
|
||||
t.Parallel()
|
||||
buf := bytebufferpool.Get()
|
||||
defer bytebufferpool.Put(buf)
|
||||
|
@ -525,38 +500,7 @@ func Test_Logger_Combined_CLF_Format_With_Name(t *testing.T) {
|
|||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
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"
|
||||
req := httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil)
|
||||
req.Header.Set("Referer", expectedReferer)
|
||||
req.Header.Set("User-Agent", expectedUA)
|
||||
resp, err := app.Test(req)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
||||
|
||||
expected := fmt.Sprintf("0.0.0.0 - - [%s] %q %d %d %q %q\n",
|
||||
time.Now().Format("15:04:05"),
|
||||
fmt.Sprintf("%s %s %s", fiber.MethodGet, "/?foo=bar", "HTTP/1.1"),
|
||||
fiber.StatusNotFound,
|
||||
0,
|
||||
expectedReferer,
|
||||
expectedUA)
|
||||
logResponse := buf.String()
|
||||
require.Equal(t, expected, logResponse)
|
||||
}
|
||||
|
||||
func Test_Logger_Combined_CLF_Format_With_Const(t *testing.T) {
|
||||
t.Parallel()
|
||||
buf := bytebufferpool.Get()
|
||||
defer bytebufferpool.Put(buf)
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
Format: FormatCombined,
|
||||
Format: CombinedFormat,
|
||||
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"
|
||||
|
@ -579,7 +523,7 @@ func Test_Logger_Combined_CLF_Format_With_Const(t *testing.T) {
|
|||
require.Equal(t, expected, logResponse)
|
||||
}
|
||||
|
||||
func Test_Logger_Json_Format_With_Name(t *testing.T) {
|
||||
func Test_Logger_Json_Format(t *testing.T) {
|
||||
t.Parallel()
|
||||
buf := bytebufferpool.Get()
|
||||
defer bytebufferpool.Put(buf)
|
||||
|
@ -587,37 +531,7 @@ func Test_Logger_Json_Format_With_Name(t *testing.T) {
|
|||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
CustomFormat: "json",
|
||||
Stream: buf,
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil)
|
||||
resp, err := app.Test(req)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
||||
|
||||
expected := fmt.Sprintf(
|
||||
"{\"time\":%q,\"ip\":%q,\"method\":%q,\"url\":%q,\"status\":%d,\"bytesSent\":%d}\n",
|
||||
time.Now().Format("15:04:05"),
|
||||
"0.0.0.0",
|
||||
fiber.MethodGet,
|
||||
"/?foo=bar",
|
||||
fiber.StatusNotFound,
|
||||
0,
|
||||
)
|
||||
logResponse := buf.String()
|
||||
require.Equal(t, expected, logResponse)
|
||||
}
|
||||
|
||||
func Test_Logger_Json_Format_With_Const(t *testing.T) {
|
||||
t.Parallel()
|
||||
buf := bytebufferpool.Get()
|
||||
defer bytebufferpool.Put(buf)
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
Format: FormatJSON,
|
||||
Format: JSONFormat,
|
||||
Stream: buf,
|
||||
}))
|
||||
|
||||
|
@ -639,7 +553,7 @@ func Test_Logger_Json_Format_With_Const(t *testing.T) {
|
|||
require.Equal(t, expected, logResponse)
|
||||
}
|
||||
|
||||
func Test_Logger_ECS_Format_With_Name(t *testing.T) {
|
||||
func Test_Logger_ECS_Format(t *testing.T) {
|
||||
t.Parallel()
|
||||
buf := bytebufferpool.Get()
|
||||
defer bytebufferpool.Put(buf)
|
||||
|
@ -647,39 +561,7 @@ func Test_Logger_ECS_Format_With_Name(t *testing.T) {
|
|||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
CustomFormat: "ecs",
|
||||
Stream: buf,
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(fiber.MethodGet, "/?foo=bar", nil)
|
||||
resp, err := app.Test(req)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
|
||||
|
||||
expected := fmt.Sprintf(
|
||||
"{\"@timestamp\":%q,\"ecs\":{\"version\":\"1.6.0\"},\"client\":{\"ip\":%q},\"http\":{\"request\":{\"method\":%q,\"url\":%q,\"protocol\":%q},\"response\":{\"status_code\":%d,\"body\":{\"bytes\":%d}}},\"log\":{\"level\":\"INFO\",\"logger\":\"fiber\"},\"message\":%q}\n",
|
||||
time.Now().Format("15:04:05"),
|
||||
"0.0.0.0",
|
||||
fiber.MethodGet,
|
||||
"/?foo=bar",
|
||||
"HTTP/1.1",
|
||||
fiber.StatusNotFound,
|
||||
0,
|
||||
fmt.Sprintf("%s %s responded with %d", fiber.MethodGet, "/?foo=bar", fiber.StatusNotFound),
|
||||
)
|
||||
logResponse := buf.String()
|
||||
require.Equal(t, expected, logResponse)
|
||||
}
|
||||
|
||||
func Test_Logger_ECS_Format_With_Const(t *testing.T) {
|
||||
t.Parallel()
|
||||
buf := bytebufferpool.Get()
|
||||
defer bytebufferpool.Put(buf)
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
Format: FormatECS,
|
||||
Format: ECSFormat,
|
||||
Stream: buf,
|
||||
}))
|
||||
|
||||
|
|
Loading…
Reference in New Issue