Skip to content

Improve HookLogFactory::fromString() - #124

Merged
gmazzap merged 2 commits into
3.xfrom
fix/123-context-extraction-from-array
Apr 17, 2026
Merged

Improve HookLogFactory::fromString() #124
gmazzap merged 2 commits into
3.xfrom
fix/123-context-extraction-from-array

Conversation

@gmazzap

@gmazzap gmazzap commented Apr 17, 2026

Copy link
Copy Markdown
Contributor

The issue

The documentation states that when the log record's “context” array has a key "channel", Wonolog uses that context value as the log record channel.

This is true when an action hook uses only one argument as array, for example:

do_action('wonolog.log', ['message' => 'My message', 'channel' => '...']);

but it is not true when passing the message as first argument as string:

do_action('wonolog.log', 'My message', ['channel' => '...']);

Reported via #123

The cause

The problem is that the source for "channel" (but also "level") when creating a log entry from a hook's arguments is the first argument passed to the hook.

The solution

Iit makes sense that when exactly 2 arguments are passed, and the second argument is an array containing relevant info, we use that info. So, the two snippets above are equivalent.

However, when multiple arguments are passed, or when the second argument is an array that does not contain relevant info ("level", "channel", "context") nothing changes, keeping backward compatibility.

This PR addresses this, adding unit tests.


Note: a second commit in this PR addresses some PHPStan errors in a n unrelated test file (AdvancedConfigTest.php) so that the CI could be green, facilitating the merge.

gmazzap added 2 commits April 17, 2026 17:47
When there are exactly 2 arguments, and the second is an array, make it
equivalent to  HookLogFactory::fromArray() where the message key is the
original 1st argument.

See #123
@gmazzap
gmazzap merged commit 264655a into 3.x Apr 17, 2026
82 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant