Add WithDefaultPrintLevel option to configure default log level for Print methods
Problem
When Echo framework uses its internal logging methods (such as for server startup messages), it calls the Print and Printf methods without specifying a log level. This results in logs showing ??? or - instead of a proper log level:
2024-11-24T18:36:10Z ??? ⇨ http server started on [::]:9151
This happens because the current implementation always uses zerolog.NoLevel with a hardcoded level string of "-" for these methods.
Solution
This PR adds a new configuration option WithDefaultPrintLevel(level log.Lvl) that allows users to configure a default log level for Echo's Print methods. When configured, these methods will use the specified level instead of showing ???.
Usage
e := echo.New()
e.Logger = lecho.New(
os.Stdout,
lecho.WithTimestamp(),
lecho.WithDefaultPrintLevel(log.INFO), // Print methods will use INFO level
)
Result:
2024-11-24T18:36:10Z INF ⇨ http server started on [::]:9151
Implementation Details
- Added
defaultPrintLevel *log.Lvlfield toOptionsandLoggerstructs - Created
WithDefaultPrintLevel(level log.Lvl)setter function - Added
printEvent()helper method to determine the appropriate zerolog event - Updated
Print,Printf, andPrintjmethods to use the configurable level - When
WithDefaultPrintLevelis not specified, behavior remains exactly the same (backward compatible)
Testing
- All existing tests pass, ensuring 100% backward compatibility
- Added comprehensive tests covering the new functionality
- Added specific backward compatibility verification tests
- Manual testing confirms the feature works as expected
Documentation
Updated README.md with:
- New option listed in "Available Options" section
- Dedicated "Default Print Level Configuration" section with examples
- Before/after comparison showing the improvement
This change is completely backward compatible and provides a clean solution to the "???" log level issue reported in the original issue.
Fixes ziflex/lecho#33
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.