Add customizable context format and value conversion#140
Add customizable context format and value conversion#140WarLikeLaux wants to merge 5 commits intoyiisoft:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #140 +/- ##
===========================================
Coverage 100.00% 100.00%
- Complexity 163 178 +15
===========================================
Files 11 11
Lines 449 494 +45
===========================================
+ Hits 449 494 +45 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Adds new configuration hooks to customize how log context is rendered and how context values are converted to strings, enabling user-defined context ordering and formatting while keeping the default output unchanged.
Changes:
- Add
setConvertToString(),setContextTemplate(), andsetContextFormat()APIs onTarget/Formatterand implement precedence rules in formatting. - Update formatter context assembly to support template-based section ordering and fully custom rendering via callable.
- Add/extend tests and documentation covering the new customization options.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
src/Message/Formatter.php |
Implements custom context formatting/template logic and pluggable value-to-string conversion. |
src/Target.php |
Exposes new formatter customization methods at the target level. |
tests/Message/FormatterTest.php |
Adds coverage for new formatter customization behaviors and precedence/error cases. |
tests/TargetTest.php |
Adds target-level tests for new customization methods. |
tests/TestAsset/DummyTarget.php |
Ensures test target mirrors new formatter customization settings for exported formatting. |
README.md |
Documents new formatting customization options and examples. |
CHANGELOG.md |
Records the new feature in the upcoming release notes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@WarLikeLaux resolve conflicts, please |
There was a problem hiding this comment.
What about use constructor parameters instead of methods?
There was a problem hiding this comment.
What about use constructor parameters instead of methods?
In another PR, mark existing methods as deprecated and also move to constructor?
@samdark WDYT?
| * | ||
| * @see Formatter::setConvertToString() | ||
| */ | ||
| private $convertToString; |
There was a problem hiding this comment.
| private $convertToString; | |
| private $stringConverter; |
| */ | ||
| private function convertToString(mixed $value): string | ||
| { | ||
| if ($this->convertToString !== null) { |
There was a problem hiding this comment.
Move default converter to separate class and use it as default value for convertToString.
| * | ||
| * @see Formatter::setContextTemplate() | ||
| */ | ||
| private ?string $contextTemplate = null; |
There was a problem hiding this comment.
What about merge $contextTemplate and $contextFormatter to one property string|callable|null $contextFormat?
What does this PR do?
Add three new methods to
FormatterandTargetfor customizing context output:setConvertToString(callable)- custom value-to-string conversionsetContextTemplate(string)- string template for reordering context sectionssetContextFormat(callable)- full control over context rendering via callableUse cases
setConvertToStringallows replacing the default VarDumper-based conversion. For example, to JSON-encode all context values:setContextTemplatereorders sections via{trace},{message},{common}placeholders. Empty sections are omitted automatically:setContextFormatgives full control over context rendering, including custom headers:Priority:
setContextFormat>setContextTemplate> default behavior.No BC break: new methods, default output unchanged.