From 5529ad20dfb5f452070e4ebd890737b45c89e070 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Werner?= Date: Fri, 22 Dec 2023 15:23:22 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20logger/middleware=20colori?= =?UTF-8?q?ze=20logger=20error=20message=20#2593?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/api/middleware/logger.md | 30 +++++++++++++++--------------- middleware/logger/config.go | 4 ++-- middleware/logger/tags.go | 4 ++++ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/docs/api/middleware/logger.md b/docs/api/middleware/logger.md index d9e4fadad8..0a23441aa3 100644 --- a/docs/api/middleware/logger.md +++ b/docs/api/middleware/logger.md @@ -92,27 +92,27 @@ app.Use(logger.New(logger.Config{ ### Config -| Property | Type | Description | Default | -|:-----------------|:---------------------------|:---------------------------------------------------------------------------------------------------------------------------------|:-------------------------------------------------------| -| Next | `func(*fiber.Ctx) bool` | Next defines a function to skip this middleware when returned true. | `nil` | -| Done | `func(*fiber.Ctx, []byte)` | Done is a function that is called after the log string for a request is written to Output, and pass the log string as parameter. | `nil` | -| CustomTags | `map[string]LogFunc` | tagFunctions defines the custom tag action. | `map[string]LogFunc` | -| Format | `string` | Format defines the logging tags. | `${time} | ${status} | ${latency} | ${ip} | ${method} | ${path}` | -| TimeFormat | `string` | TimeFormat defines the time format for log timestamps. | `15:04:05` | -| TimeZone | `string` | TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc | `"Local"` | -| TimeInterval | `time.Duration` | TimeInterval is the delay before the timestamp is updated. | `500 * time.Millisecond` | -| Output | `io.Writer` | Output is a writer where logs are written. | `os.Stdout` | -| DisableColors | `bool` | DisableColors defines if the logs output should be colorized. | `false` | -| enableColors | `bool` | Internal field for enabling colors in the log output. (This is not a user-configurable field) | - | -| enableLatency | `bool` | Internal field for enabling latency measurement in logs. (This is not a user-configurable field) | - | -| timeZoneLocation | `*time.Location` | Internal field for the time zone location. (This is not a user-configurable field) | - | +| Property | Type | Description | Default | +|:-----------------|:---------------------------|:---------------------------------------------------------------------------------------------------------------------------------|:-------------------------| +| Next | `func(*fiber.Ctx) bool` | Next defines a function to skip this middleware when returned true. | `nil` | +| Done | `func(*fiber.Ctx, []byte)` | Done is a function that is called after the log string for a request is written to Output, and pass the log string as parameter. | `nil` | +| CustomTags | `map[string]LogFunc` | tagFunctions defines the custom tag action. | `map[string]LogFunc` | +| Format | `string` | Format defines the logging tags. | `${time} | ${status} | ${latency} | ${ip} | ${method} | ${path} | ${error}\n` | +| TimeFormat | `string` | TimeFormat defines the time format for log timestamps. | `15:04:05` | +| TimeZone | `string` | TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc | `"Local"` | +| TimeInterval | `time.Duration` | TimeInterval is the delay before the timestamp is updated. | `500 * time.Millisecond` | +| Output | `io.Writer` | Output is a writer where logs are written. | `os.Stdout` | +| DisableColors | `bool` | DisableColors defines if the logs output should be colorized. | `false` | +| enableColors | `bool` | Internal field for enabling colors in the log output. (This is not a user-configurable field) | - | +| enableLatency | `bool` | Internal field for enabling latency measurement in logs. (This is not a user-configurable field) | - | +| timeZoneLocation | `*time.Location` | Internal field for the time zone location. (This is not a user-configurable field) | - | ## Default Config ```go var ConfigDefault = Config{ Next: nil, Done: nil, - Format: "${time} | ${status} | ${latency} | ${ip} | ${method} | ${path}", + Format: "${time} | ${status} | ${latency} | ${ip} | ${method} | ${path} | ${error}\n", TimeFormat: "15:04:05", TimeZone: "Local", TimeInterval: 500 * time.Millisecond, diff --git a/middleware/logger/config.go b/middleware/logger/config.go index 06a1a41846..b84714cad5 100644 --- a/middleware/logger/config.go +++ b/middleware/logger/config.go @@ -28,7 +28,7 @@ type Config struct { // Format defines the logging tags // - // Optional. Default: ${time} | ${status} | ${latency} | ${ip} | ${method} | ${path} + // Optional. Default: ${time} | ${status} | ${latency} | ${ip} | ${method} | ${path} | ${error}\n Format string // TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html @@ -86,7 +86,7 @@ type LogFunc func(output Buffer, c *fiber.Ctx, data *Data, extraParam string) (i var ConfigDefault = Config{ Next: nil, Done: nil, - Format: "${time} | ${status} | ${latency} | ${ip} | ${method} | ${path}\n", + Format: "${time} | ${status} | ${latency} | ${ip} | ${method} | ${path} | ${error}\n", TimeFormat: "15:04:05", TimeZone: "Local", TimeInterval: 500 * time.Millisecond, diff --git a/middleware/logger/tags.go b/middleware/logger/tags.go index 67ccbb83a2..9a10279eca 100644 --- a/middleware/logger/tags.go +++ b/middleware/logger/tags.go @@ -139,6 +139,10 @@ func createTagMap(cfg *Config) map[string]LogFunc { }, TagError: func(output Buffer, c *fiber.Ctx, data *Data, extraParam string) (int, error) { if data.ChainErr != nil { + if cfg.enableColors { + colors := c.App().Config().ColorScheme + return output.WriteString(fmt.Sprintf("%s%s%s", colors.Red, data.ChainErr.Error(), colors.Reset)) + } return output.WriteString(data.ChainErr.Error()) } return output.WriteString("-")