Skip to content

Commit 6a78655

Browse files
Add docs from gofiber/fiber@e947e03
1 parent ef51a26 commit 6a78655

File tree

2 files changed

+70
-24
lines changed

2 files changed

+70
-24
lines changed

docs/core/middleware/logger.md

+54-24
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ app.Use(logger.New(logger.Config{
7979
TimeZone: "Asia/Shanghai",
8080
Done: func(c fiber.Ctx, logString []byte) {
8181
if c.Response().StatusCode() != fiber.StatusOK {
82-
reporter.SendToSlack(logString)
82+
reporter.SendToSlack(logString)
8383
}
8484
},
8585
}))
@@ -88,6 +88,23 @@ app.Use(logger.New(logger.Config{
8888
app.Use(logger.New(logger.Config{
8989
DisableColors: true,
9090
}))
91+
92+
// Use predefined formats
93+
app.Use(logger.New(logger.Config{
94+
Format: logger.FormatCommon,
95+
}))
96+
97+
app.Use(logger.New(logger.Config{
98+
Format: logger.FormatCombined,
99+
}))
100+
101+
app.Use(logger.New(logger.Config{
102+
Format: logger.FormatJSON,
103+
}))
104+
105+
app.Use(logger.New(logger.Config{
106+
Format: logger.FormatECS,
107+
}))
91108
```
92109

93110
### Use Logger Middleware with Other Loggers
@@ -136,37 +153,50 @@ Writing to os.File is goroutine-safe, but if you are using a custom Stream that
136153

137154
### Config
138155

139-
| Property | Type | Description | Default |
140-
|:-----------------|:---------------------------|:---------------------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------|
141-
| Next | `func(fiber.Ctx) bool` | Next defines a function to skip this middleware when returned true. | `nil` |
142-
| Skip | `func(fiber.Ctx) bool` | Skip is a function to determine if logging is skipped or written to Stream. | `nil` |
143-
| Done | `func(fiber.Ctx, []byte)` | Done is a function that is called after the log string for a request is written to Stream, and pass the log string as parameter. | `nil` |
144-
| CustomTags | `map[string]LogFunc` | tagFunctions defines the custom tag action. | `map[string]LogFunc` |
145-
| Format | `string` | Format defines the logging tags. | `[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n` |
146-
| TimeFormat | `string` | TimeFormat defines the time format for log timestamps. | `15:04:05` |
147-
| TimeZone | `string` | TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc | `"Local"` |
148-
| TimeInterval | `time.Duration` | TimeInterval is the delay before the timestamp is updated. | `500 * time.Millisecond` |
149-
| Stream | `io.Writer` | Stream is a writer where logs are written. | `os.Stdout` |
150-
| LoggerFunc | `func(c fiber.Ctx, data *Data, cfg Config) error` | Custom logger function for integration with logging libraries (Zerolog, Zap, Logrus, etc). Defaults to Fiber's default logger if not defined. | `see default_logger.go defaultLoggerInstance` |
151-
| DisableColors | `bool` | DisableColors defines if the logs output should be colorized. | `false` |
156+
| Property | Type | Description | Default |
157+
| :------------ | :------------------------------------------------ | :-------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------------------- |
158+
| Next | `func(fiber.Ctx) bool` | Next defines a function to skip this middleware when returned true. | `nil` |
159+
| Skip | `func(fiber.Ctx) bool` | Skip is a function to determine if logging is skipped or written to Stream. | `nil` |
160+
| Done | `func(fiber.Ctx, []byte)` | Done is a function that is called after the log string for a request is written to Stream, and pass the log string as parameter. | `nil` |
161+
| CustomTags | `map[string]LogFunc` | tagFunctions defines the custom tag action. | `map[string]LogFunc` |
162+
| `Format` | `string` | Defines the logging tags. See more in [Predefined Formats](#predefined-formats), or create your own using [Tags](#constants). | `[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n` (same as `DefaultFormat`) |
163+
| TimeFormat | `string` | TimeFormat defines the time format for log timestamps. | `15:04:05` |
164+
| TimeZone | `string` | TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc | `"Local"` |
165+
| TimeInterval | `time.Duration` | TimeInterval is the delay before the timestamp is updated. | `500 * time.Millisecond` |
166+
| Stream | `io.Writer` | Stream is a writer where logs are written. | `os.Stdout` |
167+
| LoggerFunc | `func(c fiber.Ctx, data *Data, cfg Config) error` | Custom logger function for integration with logging libraries (Zerolog, Zap, Logrus, etc). Defaults to Fiber's default logger if not defined. | `see default_logger.go defaultLoggerInstance` |
168+
| DisableColors | `bool` | DisableColors defines if the logs output should be colorized. | `false` |
152169

153170
## Default Config
154171

155172
```go
156173
var ConfigDefault = Config{
157-
Next: nil,
158-
Skip nil,
159-
Done: nil,
160-
Format: "[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n",
161-
TimeFormat: "15:04:05",
162-
TimeZone: "Local",
163-
TimeInterval: 500 * time.Millisecond,
164-
Stream: os.Stdout,
165-
DisableColors: false,
166-
LoggerFunc: defaultLoggerInstance,
174+
Next: nil,
175+
Skip: nil,
176+
Done: nil,
177+
Format: DefaultFormat,
178+
TimeFormat: "15:04:05",
179+
TimeZone: "Local",
180+
TimeInterval: 500 * time.Millisecond,
181+
Stream: os.Stdout,
182+
BeforeHandlerFunc: beforeHandlerFunc,
183+
LoggerFunc: defaultLoggerInstance,
184+
enableColors: true,
167185
}
168186
```
169187

188+
## Predefined Formats
189+
190+
Logger provides predefined formats that you can use by name or directly by specifying the format string.
191+
192+
| **Format Constant** | **Format String** | **Description** |
193+
|---------------------|--------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------|
194+
| `DefaultFormat` | `"[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n"` | Fiber's default logger format. |
195+
| `CommonFormat` | `"${ip} - - [${time}] "${method} ${url} ${protocol}" ${status} ${bytesSent}\n"` | Common Log Format (CLF) used in web server logs. |
196+
| `CombinedFormat` | `"${ip} - - [${time}] "${method} ${url} ${protocol}" ${status} ${bytesSent} "${referer}" "${ua}"\n"` | CLF format plus the `referer` and `user agent` fields. |
197+
| `JSONFormat` | `"{time: ${time}, ip: ${ip}, method: ${method}, url: ${url}, status: ${status}, bytesSent: ${bytesSent}}\n"` | JSON format for structured logging. |
198+
| `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"` | Elastic Common Schema (ECS) format for structured logging. |
199+
170200
## Constants
171201

172202
```go

docs/core/whats_new.md

+16
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,22 @@ app.Use(logger.New(logger.Config{
937937

938938
</details>
939939

940+
#### Predefined Formats
941+
942+
Logger provides predefined formats that you can use by name or directly by specifying the format string.
943+
<details>
944+
945+
<summary>Example Usage</summary>
946+
947+
```go
948+
app.Use(logger.New(logger.Config{
949+
Format: logger.FormatCombined,
950+
}))
951+
```
952+
953+
See more in [Logger](./middleware/logger.md#predefined-formats)
954+
</details>
955+
940956
### Filesystem
941957

942958
We've decided to remove filesystem middleware to clear up the confusion between static and filesystem middleware.

0 commit comments

Comments
 (0)