-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
π₯ feat(logger): Add predefined log formats #3359
π₯ feat(logger): Add predefined log formats #3359
Conversation
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.
Thanks for opening this pull request! π Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
WalkthroughThis pull request introduces predefined logging formats for the Fiber logger middleware. It enhances the logger configuration by adding new fields ( Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant LoggerMiddleware
participant FormatMap
Client->>LoggerMiddleware: Send HTTP Request
LoggerMiddleware->>FormatMap: Lookup cfg.Format
alt Format exists in LoggerConfig
FormatMap-->>LoggerMiddleware: Return corresponding format string
LoggerMiddleware->>LoggerMiddleware: Update log format dynamically
else
LoggerMiddleware->>LoggerMiddleware: Use default format (DefaultFormat)
end
LoggerMiddleware->>Client: Process request and output log
Poem
π Recent review detailsConfiguration used: CodeRabbit UI π Files selected for processing (3)
π§ Files skipped from review as they are similar to previous changes (2)
Note π Summarized by CodeRabbit FreeYour organization has reached its limit of developer seats under the Pro Plan. For new users, CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please add seats to your subscription by visiting https://app.coderabbit.ai/login.If you believe this is a mistake and have available seats, please assign one to the pull request author through the subscription management page using the link above. πͺ§ TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
π§Ή Nitpick comments (2)
docs/middleware/logger.md (2)
171-182
: Fix tab characters in markdownThe default config example uses tabs instead of spaces which is flagged by the markdown linter.
- 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, + 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,π§° Tools
πͺ markdownlint-cli2 (0.17.2)
171-171: Hard tabs
Column: 1(MD010, no-hard-tabs)
172-172: Hard tabs
Column: 1(MD010, no-hard-tabs)
173-173: Hard tabs
Column: 1(MD010, no-hard-tabs)
174-174: Hard tabs
Column: 1(MD010, no-hard-tabs)
175-175: Hard tabs
Column: 1(MD010, no-hard-tabs)
176-176: Hard tabs
Column: 1(MD010, no-hard-tabs)
177-177: Hard tabs
Column: 1(MD010, no-hard-tabs)
178-178: Hard tabs
Column: 1(MD010, no-hard-tabs)
179-179: Hard tabs
Column: 1(MD010, no-hard-tabs)
180-180: Hard tabs
Column: 1(MD010, no-hard-tabs)
181-181: Hard tabs
Column: 1(MD010, no-hard-tabs)
185-192
: Great addition of predefined formats sectionThe predefined formats table provides excellent documentation for the new feature, clearly showing the formats and their purposes.
Fix the table formatting issues:
- Add a blank line before the table (line 187)
- Add a trailing pipe to line 192 to match the table style
## 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. +| json | `FormatJSON` | `"{time: ${time}, ip: ${ip}, method: ${method}, url: ${url}, status: ${status}, bytesSent: ${bytesSent}}\n"` | JSON format for structured logging. |π§° Tools
πͺ markdownlint-cli2 (0.17.2)
187-187: Tables should be surrounded by blank lines
null(MD058, blanks-around-tables)
192-192: Table pipe style
Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe(MD055, table-pipe-style)
π Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
π Files selected for processing (7)
docs/middleware/logger.md
(3 hunks)docs/whats_new.md
(1 hunks)middleware/logger/config.go
(2 hunks)middleware/logger/default_logger.go
(1 hunks)middleware/logger/format.go
(1 hunks)middleware/logger/logger.go
(1 hunks)middleware/logger/logger_test.go
(1 hunks)
π§° Additional context used
𧬠Code Definitions (4)
middleware/logger/default_logger.go (1)
middleware/logger/format.go (1) (1)
FormatDefault
(5-5)
middleware/logger/logger.go (1)
middleware/logger/format.go (1) (1)
LoggerConfig
(17-22)
middleware/logger/config.go (1)
middleware/logger/format.go (1) (1)
FormatDefault
(5-5)
middleware/logger/logger_test.go (2)
middleware/logger/config.go (1) (1)
Config
(12-91)middleware/logger/format.go (3) (3)
FormatCommonLog
(7-7)FormatCombined
(9-9)FormatJSON
(11-11)
πͺ markdownlint-cli2 (0.17.2)
docs/middleware/logger.md
171-171: Hard tabs
Column: 1
(MD010, no-hard-tabs)
172-172: Hard tabs
Column: 1
(MD010, no-hard-tabs)
173-173: Hard tabs
Column: 1
(MD010, no-hard-tabs)
174-174: Hard tabs
Column: 1
(MD010, no-hard-tabs)
175-175: Hard tabs
Column: 1
(MD010, no-hard-tabs)
176-176: Hard tabs
Column: 1
(MD010, no-hard-tabs)
177-177: Hard tabs
Column: 1
(MD010, no-hard-tabs)
178-178: Hard tabs
Column: 1
(MD010, no-hard-tabs)
179-179: Hard tabs
Column: 1
(MD010, no-hard-tabs)
180-180: Hard tabs
Column: 1
(MD010, no-hard-tabs)
181-181: Hard tabs
Column: 1
(MD010, no-hard-tabs)
187-187: Tables should be surrounded by blank lines
null
(MD058, blanks-around-tables)
192-192: Table pipe style
Expected: leading_and_trailing; Actual: leading_only; Missing trailing pipe
(MD055, table-pipe-style)
π Additional comments (15)
middleware/logger/default_logger.go (1)
31-31
: Clean refactoring to use the new format constant.The change updates the conditional check to use
FormatDefault
constant instead of what was likely a variable calleddefaultFormat
. This aligns with the new predefined formats implementation and improves code maintainability by using named constants.middleware/logger/format.go (2)
3-12
: Well-structured predefined log format constants.These constants clearly define the various logging formats:
FormatDefault
- Fiber's default log formatFormatCommonLog
- Apache-style common log formatFormatCombined
- Combined log format with referrer and user-agentFormatJSON
- JSON structured logging formatEach format is properly documented with a descriptive comment. The implementation aligns with the PR objectives to introduce multiple standardized logging formats.
14-22
: Good implementation of the format mapping.The
LoggerConfig
map provides a clean way to map string identifiers to the format constants. This allows users to configure the logger using simple string names like "common" or "json" instead of having to specify the full format string.This approach aligns with the PR objectives and enhances usability.
middleware/logger/logger.go (1)
34-36
: Excellent addition for dynamic format resolution.This conditional block checks if the configured format string matches a key in the
LoggerConfig
map and updates it to the corresponding format if found. This allows users to specify formats by name (e.g., "common", "combined", "json") rather than using the full format string.The placement is correct - after the timestamp is initialized and before the routine that updates it every 500ms.
docs/whats_new.md (1)
940-961
: Great documentation for the new predefined formats feature.This section clearly explains the predefined formats feature and provides concise examples showing how to use them. The examples demonstrate both string-based format specification ("common") and constant-based specification (logger.FormatCombined).
The documentation is well-structured and aligns perfectly with the implementation, making it easy for users to understand and use this new feature.
middleware/logger/config.go (2)
53-66
: Well-documented format options!The expanded documentation for the
Format
field is comprehensive and informative, providing clear examples of both custom formats and predefined options. This will make it easier for users to configure logging according to their needs.
119-119
: Good refactoring to use the constantReplacing the hardcoded default format string with the
FormatDefault
constant improves maintainability and ensures consistency across the codebase.docs/middleware/logger.md (2)
92-104
: Great examples for the new predefined formatsThe added examples clearly demonstrate how to use each of the predefined formats, showing both string-based configuration and constant-based configuration options.
159-159
: Updated documentation reflects new featuresThe
Format
field description now correctly describes the support for predefined formats, which aligns with the implementation changes.middleware/logger/logger_test.go (6)
470-493
: Good test for Common Log Format with nameThis test effectively verifies that the logger middleware correctly formats output when using the "common" format string. The test validates both the configuration and the expected output format.
495-518
: Good test for Common Log Format with constantThis test complements the previous one by verifying functionality when using the
FormatCommonLog
constant instead of the string name.
520-549
: Good test for Combined Log Format with nameThis test effectively verifies that the logger middleware correctly handles the "combined" format, including additional fields like referrer and user agent.
551-580
: Good test for Combined Log Format with constantThis test complements the previous one by verifying functionality when using the
FormatCombined
constant instead of the string name.
582-610
: Good test for JSON Format with nameThis test effectively verifies that the logger middleware correctly formats output as JSON when using the "json" format string.
612-640
: Good test for JSON Format with constantThis test complements the previous one by verifying functionality when using the
FormatJSON
constant instead of the string name.
Codecov ReportAll modified and coverable lines are covered by tests β
Additional details and impacted files@@ Coverage Diff @@
## main #3359 +/- ##
=======================================
Coverage 83.73% 83.74%
=======================================
Files 118 118
Lines 11728 11728
=======================================
+ Hits 9821 9822 +1
+ Misses 1481 1480 -1
Partials 426 426
Flags with carried forward coverage won't be shown. Click here to find out more. β View full report in Codecov by Sentry. π New features to boost your workflow:
|
can we also provide the ECS format |
Potential implementation: const FormatECS = "{\"@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" |
do you think we should add this or not, because it is too specific ? |
We should add it while we are at it. This will make parsing Fiber logs easier |
@edvardsanta can you do this and also check the review hints |
Sure, I'll open a PR solving the issues and adding ECS log format. |
you can simply extend the current one, a new PR is not necessary |
Sorry, not a new PR my brain isnβt working very well this morning hahaha π |
β¦og-formats' into feat/add-predefined-log-formats
This commit introduces a `CustomFormat` option to the `Config` struct, allowing users to specify a predefined format (like "common", "combined", "json", or "ecs")
- Removed `CustomFormat` field from `Config`. - Removed `LoggerConfig` map. - Rename predefined formats constants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π LGTM
Congrats on merging your first pull request! π We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
Description
This PR introduces support for multiple logging formats in the logger middleware. Users can now specify the format as:
Observation: I called them just the common and combined log formats because, in the official documentation for Apacheand Nginx, they use this formats respectively. Both web servers support extensions and customizations to these formats, but I have avoided naming them explicitly as "Nginx" and "Apache" formats since that would be redundant. For reference
Fixes #3345
Changes introduced
List the new features or adjustments introduced in this pull request. Provide details on benchmarks, documentation updates, changelog entries, and if applicable, the migration guide.
Type of change
Please delete options that are not relevant.
Checklist
Before you submit your pull request, please make sure you meet these requirements:
/docs/
directory for Fiber's documentation.Commit formatting
Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md