You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* feat(logger): Add predefined log formats
This commit introduces predefined log formats for the logger middleware, enhancing its flexibility and ease of use. Users can now specify formats like "common", "combined", and "json" in addition to the default format.
Changes:
- Added a `format.go` file to store predefined log format constants.
- Updated `config.go` to include documentation for the `Format` configuration option, explaining the available placeholders and predefined formats.
- Modified `logger.go` to utilize the predefined formats based on the `Format` configuration.
- Added a new test case `Test_Logger_CLF` in `logger_test.go` to verify the "common" log format.
* feat(logger): Use predefined formats and fix default format
This commit updates the logger middleware to utilize the predefined log formats introduced in a previous commit. It also fixes the default format to use the `FormatDefault` constant.
Changes:
- Updated `config.go` to use `FormatDefault` constant for the default format.
- Updated `default_logger.go` to use `FormatDefault` constant for the default format.
- Added new test cases in `logger_test.go` to verify the "common", "combined" and "json" log formats.
- Updated `format.go` to add newline character to the end of the default format.
* feat(logger): Document and exemplify predefined formats
* fix(logger): Improve test assertions based on golangci-lint
* docs(logger): Improve documentation and formatting logger.md based on markdownlint-cli2
* docs(logger): Improve documentation based on markdownlint-cli2
* fix(logger): Improve combined and JSON format tests
* feat(logger): Add ECS log format
* feat(logger): Add CustomFormat option
This commit introduces a `CustomFormat` option to the `Config` struct, allowing users to specify a predefined format (like "common", "combined", "json", or "ecs")
* feat(logger): Add ECS log format to examples and config
* docs(logger): Update examples in whats_new.md
* feat(logger): Remove CustomFormat option and renamed Format consts
- Removed `CustomFormat` field from `Config`.
- Removed `LoggerConfig` map.
- Rename predefined formats constants.
* docs(logger): Update documentation and examples after format refactor
---------
Co-authored-by: Juan Calderon-Perez <[email protected]>
| 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`|
| 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`|
|`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. |
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"
0 commit comments