logger()->info([...]) throws a TypeError although the docs show array messages

未關閉
#613 0 則留言 0 個 reaction 已指派 1 人 在 GitHub 檢視

維護者通常 1 天內回覆

@armanist 已經在處理了。

開始於 2026年10月27日。

  • #625 來自 @armanist —— 已關閉,未合併

評估

難度
4/5
預估耗時
3-5 天
新手友好度
42/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
活躍
技術堆疊
php
領域
backend

研究方向

Read Logger::log(), the nine level methods, ReportableInterface::report(), and LoggerTrait::report() to trace where the array is rejected; compare that behavior with packages/logger/usage.md, packages/logger/adapters.md, architecture.md, and packages/logger/helpers.md. The issue leaves two incompatible approaches open, so first clarify which behavior should be supported. Done means the chosen behavior and docs agree, the corresponding file-adapter test passes, and the listed checks pass on PHP 8.1–8.3 with a changelog entry.

由索引模型根據 Issue 內容生成。

描述

bug

Summary

Passing an array to a Logger method throws a TypeError, but the Logger docs describe array messages as a supported way to log structured data.

What happens

logger()->info(['order' => 1042, 'status' => 'paid']);
TypeError: Quantum\Logger\Logger::info(): Argument #1 ($message) must be of type Stringable|string, array given

I reproduced it on 3.1.0 with a stub adapter. Before 3.1 the Logger methods had no parameter type, but ReportableInterface::report() already required string|Stringable, so the same call could not have worked there either.

What the docs say

  • packages/logger/usage.md, "Log a structured payload": the example calls logger()->info([...]).
  • packages/logger/adapters.md and architecture.md: "If you pass an array message, the package JSON-encodes it with pretty printing before writing."
  • packages/logger/helpers.md: use logger() for "structured array messages".

Cause

  • Logger::log() and the nine level methods take string|Stringable $message, and so does ReportableInterface::report().
  • LoggerTrait::report() still contains is_array($message) ? json_encode($message, JSON_PRETTY_PRINT) : (string) $message. That branch can never run, because the parameter type rejects an array first.
  • Logger implements Psr\Log\LoggerInterface, where the message is string|Stringable. PSR-3 puts structured data in $context, but the docs state that no context rendering happens ("No placeholder interpolation or structured context rendering happens").

Options

  1. Accept arrays. Widen the message type to string|Stringable|array on Logger and ReportableInterface::report(), so the existing JSON branch works. PHP allows widening a parameter type in an override, so the PSR-3 interface still holds. This keeps the docs as they are.
  2. Keep the PSR-3 shape. Remove the array promises from the docs and the dead branch from LoggerTrait, and decide how structured data should be written, for example by rendering $context in the file adapters.

Acceptance criteria

  • the behavior and the docs agree, whichever option is chosen
  • a test logs an array through logger() with a file adapter and checks what is written (option 1), or a test checks that the chosen alternative works (option 2)
  • composer test, composer cs:check, composer phpstan and composer rector:check pass on PHP 8.1 to 8.3
  • the changelog lists the change
主要語言
PHP
星號
43
分支
22
平均合併
54 分鐘
30 天內合併 PR
57

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

quantum-php/framework 的其他 Issue

查看 quantum-php/framework 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。