Skip to content

Commit 8117e71

Browse files
committed
update defaultformat
1 parent cc550f9 commit 8117e71

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

docs/api/middleware/logger.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,27 @@ app.Use(logger.New(logger.Config{
9292

9393
### Config
9494

95-
| Property | Type | Description | Default |
96-
|:-----------------|:---------------------------|:---------------------------------------------------------------------------------------------------------------------------------|:-------------------------------------------------------|
97-
| Next | `func(*fiber.Ctx) bool` | Next defines a function to skip this middleware when returned true. | `nil` |
98-
| 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` |
99-
| CustomTags | `map[string]LogFunc` | tagFunctions defines the custom tag action. | `map[string]LogFunc` |
100-
| Format | `string` | Format defines the logging tags. | `[${time}] ${status} - ${latency} ${method} ${path}\n` |
101-
| TimeFormat | `string` | TimeFormat defines the time format for log timestamps. | `15:04:05` |
102-
| TimeZone | `string` | TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc | `"Local"` |
103-
| TimeInterval | `time.Duration` | TimeInterval is the delay before the timestamp is updated. | `500 * time.Millisecond` |
104-
| Output | `io.Writer` | Output is a writer where logs are written. | `os.Stdout` |
105-
| DisableColors | `bool` | DisableColors defines if the logs output should be colorized. | `false` |
106-
| enableColors | `bool` | Internal field for enabling colors in the log output. (This is not a user-configurable field) | - |
107-
| enableLatency | `bool` | Internal field for enabling latency measurement in logs. (This is not a user-configurable field) | - |
108-
| timeZoneLocation | `*time.Location` | Internal field for the time zone location. (This is not a user-configurable field) | - |
95+
| Property | Type | Description | Default |
96+
| :--------------- | :------------------------- | :----------------------------------------------------------- | :----------------------------------------------------------- |
97+
| Next | `func(*fiber.Ctx) bool` | Next defines a function to skip this middleware when returned true. | `nil` |
98+
| 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` |
99+
| CustomTags | `map[string]LogFunc` | tagFunctions defines the custom tag action. | `map[string]LogFunc` |
100+
| Format | `string` | Format defines the logging tags. | `${time} | ${status} | ${latency} | ${method} | ${path} | ${error}\n` |
101+
| TimeFormat | `string` | TimeFormat defines the time format for log timestamps. | `15:04:05` |
102+
| TimeZone | `string` | TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc | `"Local"` |
103+
| TimeInterval | `time.Duration` | TimeInterval is the delay before the timestamp is updated. | `500 * time.Millisecond` |
104+
| Output | `io.Writer` | Output is a writer where logs are written. | `os.Stdout` |
105+
| DisableColors | `bool` | DisableColors defines if the logs output should be colorized. | `false` |
106+
| enableColors | `bool` | Internal field for enabling colors in the log output. (This is not a user-configurable field) | - |
107+
| enableLatency | `bool` | Internal field for enabling latency measurement in logs. (This is not a user-configurable field) | - |
108+
| timeZoneLocation | `*time.Location` | Internal field for the time zone location. (This is not a user-configurable field) | - |
109109

110110
## Default Config
111111
```go
112112
var ConfigDefault = Config{
113113
Next: nil,
114114
Done: nil,
115-
Format: "[${time}] ${status} - ${latency} ${method} ${path}\n",
115+
Format: "${time} | ${status} | ${latency} | ${method} | ${path} | ${error}\n",
116116
TimeFormat: "15:04:05",
117117
TimeZone: "Local",
118118
TimeInterval: 500 * time.Millisecond,

middleware/logger/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Config struct {
2828

2929
// Format defines the logging tags
3030
//
31-
// Optional. Default: [${time}] ${status} - ${latency} ${method} ${path}\n
31+
// Optional. Default: ${time} | ${status} | ${latency} | ${method} | ${path} | ${error}\n
3232
Format string
3333

3434
// 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
8686
var ConfigDefault = Config{
8787
Next: nil,
8888
Done: nil,
89-
Format: "[${time}] ${status} - ${latency} ${method} ${path}\n",
89+
Format: "${time} | ${status} | ${latency} | ${method} | ${path} | ${error}\n",
9090
TimeFormat: "15:04:05",
9191
TimeZone: "Local",
9292
TimeInterval: 500 * time.Millisecond,

middleware/logger/logger.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func New(config ...Config) fiber.Handler {
5858
errHandler fiber.ErrorHandler
5959

6060
dataPool = sync.Pool{New: func() interface{} { return new(Data) }}
61+
62+
methodColorFmt string
63+
statusCodeFmt string
64+
colorReset string
6165
)
6266

6367
// If colors are enabled, check terminal compatibility
@@ -151,20 +155,22 @@ func New(config ...Config) fiber.Handler {
151155

152156
// Construct the log format
153157
logFormat := "%s |%s %3d %s| %7v | %15s |%s %-7s %s| %-" + errPaddingStr + "s %s\n"
154-
if !cfg.enableColors {
155-
logFormat = "%s | %3d | %7v | %15s | %-7s | %-" + errPaddingStr + "s %s\n"
156-
}
157158

158-
// Write log entry to buffer
159-
_, _ = buf.WriteString(fmt.Sprintf(logFormat,
159+
if cfg.enableColors {
160+
colorReset = colors.Reset
161+
statusCodeFmt = statusColor(c.Response().StatusCode(), colors)
162+
methodColorFmt = methodColor(c.Method(), colors)
163+
}
164+
_, _ = buf.WriteString(fmt.Sprintf( //nolint:errcheck // This will never fail
165+
logFormat,
160166
timestamp.Load().(string),
161-
statusColor(c.Response().StatusCode(), colors), c.Response().StatusCode(), colors.Reset,
162-
data.Stop.Sub(data.Start).Round(time.Millisecond),
167+
statusCodeFmt, c.Response().StatusCode(), colorReset,
168+
data.Stop.Sub(data.Start),
163169
c.IP(),
164-
methodColor(c.Method(), colors), c.Method(), colors.Reset,
170+
methodColorFmt, c.Method(), colorReset,
165171
c.Path(),
166172
formatErr,
167-
)) //nolint:errcheck // This will never fail
173+
))
168174

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

middleware/logger/tags.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ func createTagMap(cfg *Config) map[string]LogFunc {
139139
},
140140
TagError: func(output Buffer, c *fiber.Ctx, data *Data, extraParam string) (int, error) {
141141
if data.ChainErr != nil {
142+
if cfg.enableColors {
143+
colors := c.App().Config().ColorScheme
144+
return output.WriteString(fmt.Sprintf("%s %s %s", colors.Red, data.ChainErr.Error(), colors.Reset))
145+
}
146+
142147
return output.WriteString(data.ChainErr.Error())
143148
}
144149
return output.WriteString("-")

0 commit comments

Comments
 (0)