mirror of https://github.com/gofiber/fiber.git
feat(logger): Document and exemplify predefined formats
parent
7e944894df
commit
a67fc2a3ea
|
@ -88,6 +88,20 @@ app.Use(logger.New(logger.Config{
|
|||
app.Use(logger.New(logger.Config{
|
||||
DisableColors: true,
|
||||
}))
|
||||
|
||||
|
||||
// Using predefined formats: common, combined, json
|
||||
app.Use(logger.New(logger.Config{
|
||||
Format: "common", // or logger.FormatCommon,
|
||||
}))
|
||||
|
||||
app.Use(logger.New(logger.Config{
|
||||
Format: "combined", // or logger.FormatCombined,
|
||||
}))
|
||||
|
||||
app.Use(logger.New(logger.Config{
|
||||
Format: "json", // or logger.FormatJSON,
|
||||
}))
|
||||
```
|
||||
|
||||
### Use Logger Middleware with Other Loggers
|
||||
|
@ -142,7 +156,7 @@ Writing to os.File is goroutine-safe, but if you are using a custom Stream that
|
|||
| Skip | `func(fiber.Ctx) bool` | Skip is a function to determine if logging is skipped or written to Stream. | `nil` |
|
||||
| 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` |
|
||||
| CustomTags | `map[string]LogFunc` | tagFunctions defines the custom tag action. | `map[string]LogFunc` |
|
||||
| Format | `string` | Format defines the logging tags. | `[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n` |
|
||||
| Format | `string` | Format defines the logging tags. Supports predefined formats: `default`,`common`, `combined`, `json`. | `[${time}] ${ip} ${status} - ${latency} ${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` |
|
||||
|
@ -154,19 +168,29 @@ Writing to os.File is goroutine-safe, but if you are using a custom Stream that
|
|||
|
||||
```go
|
||||
var ConfigDefault = Config{
|
||||
Next: nil,
|
||||
Skip nil,
|
||||
Done: nil,
|
||||
Format: "[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n",
|
||||
TimeFormat: "15:04:05",
|
||||
TimeZone: "Local",
|
||||
TimeInterval: 500 * time.Millisecond,
|
||||
Stream: os.Stdout,
|
||||
DisableColors: false,
|
||||
LoggerFunc: defaultLoggerInstance,
|
||||
Next: nil,
|
||||
Skip: nil,
|
||||
Done: nil,
|
||||
Format: FormatDefault,
|
||||
TimeFormat: "15:04:05",
|
||||
TimeZone: "Local",
|
||||
TimeInterval: 500 * time.Millisecond,
|
||||
Stream: os.Stdout,
|
||||
BeforeHandlerFunc: beforeHandlerFunc,
|
||||
LoggerFunc: defaultLoggerInstance,
|
||||
enableColors: true,
|
||||
}
|
||||
```
|
||||
|
||||
## Predefined Formats
|
||||
Logger provides predefined formats that you can use by name or directly by specifying the format string.
|
||||
| **Format Name** | **Format Constant** | **Format String** | **Description** |
|
||||
|-------------------|---------------------|--------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------|
|
||||
| default | `FormatDefault` | `"[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n"` | Fiber's default logger format. |
|
||||
| common | `FormatCommonLog` | `"${ip} - - [${time}] "${method} ${url} ${protocol}" ${status} ${bytesSent}\n"` | Common Log Format (CLF) used in web server logs. |
|
||||
| combined | `FormatCombined` | `"${ip} - - [${time}] "${method} ${url} ${protocol}" ${status} ${bytesSent} "${referer}" "${ua}"\n"` | CLF format plus the `referer` and `user agent` fields. |
|
||||
| json | `FormatJSON` | `"{time: ${time}, ip: ${ip}, method: ${method}, url: ${url}, status: ${status}, bytesSent: ${bytesSent}}\n"` | JSON format for structured logging.
|
||||
|
||||
## Constants
|
||||
|
||||
```go
|
||||
|
|
|
@ -937,6 +937,28 @@ app.Use(logger.New(logger.Config{
|
|||
|
||||
</details>
|
||||
|
||||
#### Predefined Formats
|
||||
|
||||
Logger provides predefined formats that you can use by name or directly by specifying the format string.
|
||||
<details>
|
||||
|
||||
<summary>Example Usage</summary>
|
||||
|
||||
```go
|
||||
app.Use(logger.New(logger.Config{
|
||||
Format: "common", // or logger.FormatCommon,
|
||||
}))
|
||||
|
||||
app.Use(logger.New(logger.Config{
|
||||
Format: "combined", // or logger.FormatCombined,
|
||||
}))
|
||||
|
||||
app.Use(logger.New(logger.Config{
|
||||
Format: "json", // or logger.FormatJSON,
|
||||
}))
|
||||
```
|
||||
</details>
|
||||
|
||||
### Filesystem
|
||||
|
||||
We've decided to remove filesystem middleware to clear up the confusion between static and filesystem middleware.
|
||||
|
|
|
@ -1,13 +1,19 @@
|
|||
package logger
|
||||
|
||||
const (
|
||||
FormatDefault = "[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n"
|
||||
// Fiber's default logger `
|
||||
FormatDefault = "[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n"
|
||||
// Common log format
|
||||
FormatCommonLog = "${ip} - - [${time}] \"${method} ${url} ${protocol}\" ${status} ${bytesSent}\n"
|
||||
FormatCombined = "${ip} - - [${time}] \"${method} ${url} ${protocol}\" ${status} ${bytesSent} \"${referer}\" \"${ua}\"\n"
|
||||
FormatJSON = "{\"time\":\"${time}\",\"ip\":\"${ip}\",\"method\":\"${method}\",\"url\":\"${url}\",\"status\":${status},\"bytesSent\":${bytesSent}}\n"
|
||||
// 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"
|
||||
)
|
||||
|
||||
// LoggerConfig provides a mapping of predefined formats
|
||||
// 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,
|
||||
|
|
Loading…
Reference in New Issue