From 7a0bafb195aa74083e7797e0cbabea35bd8a5a68 Mon Sep 17 00:00:00 2001 From: Arman <407448+armanist@users.noreply.github.com> Date: Mon, 23 Nov 2026 01:01:45 +0400 Subject: [PATCH] docs: document the log context and remove the array messages --- v3.0/packages/logger/adapters.md | 19 +++++++++++++++++-- v3.0/packages/logger/architecture.md | 6 +++--- v3.0/packages/logger/contracts.md | 10 +++------- v3.0/packages/logger/helpers.md | 4 ++-- v3.0/packages/logger/overview.md | 2 +- v3.0/packages/logger/usage.md | 13 +++++++++---- v3.0/upgrade-guide.md | 17 ++++++++++++++++- 7 files changed, 51 insertions(+), 20 deletions(-) diff --git a/v3.0/packages/logger/adapters.md b/v3.0/packages/logger/adapters.md index 22c37ca..a90b5f2 100644 --- a/v3.0/packages/logger/adapters.md +++ b/v3.0/packages/logger/adapters.md @@ -89,6 +89,21 @@ Both file adapters use the same line format: [YYYY-MM-DD HH:MM:SS] Level: message ``` -If you pass `['trace' => $trace]`, the trace is appended on the next line before the final newline. +When the call passes a context, it follows the message on the same line as one line of JSON: -If you pass an array message, the package JSON-encodes it with pretty printing before writing. +```php +logger()->info('Order paid', ['order' => 1042, 'status' => 'paid']); +``` + +```text +[2026-11-23 14:02:11] Info: Order paid {"order":1042,"status":"paid"} +``` + +A few keys and values are written in their own way: + +- `trace` that is text (a string or a `Stringable` object, such as `$exception->getTraceAsString()`) goes on the next lines, after the message and its context, instead of into the JSON. A `trace` that is not text, such as the array of `$exception->getTrace()`, stays in the JSON. +- An exception at the top level of the context is written as its `class`, `message`, `file` and `line`, and a `Stringable` value as its text. +- Slashes and non-ASCII characters are written as they are, invalid UTF-8 becomes `�`, and a value JSON cannot encode, such as a resource, becomes `null`. +- When a context value throws while it is converted, `{"context":"not encodable"}` replaces the context, so the log call still writes its line. + +Without a context, the line is only the message. Everything you put in the context reaches the log file, so leave passwords and tokens out of it. diff --git a/v3.0/packages/logger/architecture.md b/v3.0/packages/logger/architecture.md index c2744bc..3ee4794 100644 --- a/v3.0/packages/logger/architecture.md +++ b/v3.0/packages/logger/architecture.md @@ -72,11 +72,11 @@ This matters because the threshold is not stored per logger instance. File adapters use a shared formatter that: 1. prepends a timestamp and capitalized level -2. JSON-encodes array messages -3. appends `context['trace']` when present +2. appends the context as one line of JSON, without a `trace` that is text +3. appends a `trace` that is text on its own lines 4. appends the final string to the target file -All other context keys are ignored by file adapters. +The JSON conversion catches its own errors, so a context value that fails to convert replaces the context with a marker instead of stopping the log call. See [File output format](adapters.md#file-output-format). ## Message adapter flow diff --git a/v3.0/packages/logger/contracts.md b/v3.0/packages/logger/contracts.md index bcefe41..a8a4e0f 100644 --- a/v3.0/packages/logger/contracts.md +++ b/v3.0/packages/logger/contracts.md @@ -43,15 +43,11 @@ In debug mode, the opposite happens in the web app: the factory forces `message` So adapter selection is environment-sensitive, not purely call-driven. -## File logging uses only one special context key +## File logging writes the context -For file adapters, the only context key that changes output is: +The message is `string|Stringable`, as PSR-3 defines; an array message throws a `TypeError`. Structured data goes in the context: the file adapters write it after the message as one line of JSON, and a `trace` that is text on the following lines. A context value that cannot be converted never makes the log call fail. See [File output format](adapters.md#file-output-format) for the exact format. -- `trace` - -It is appended verbatim after the formatted message. - -No placeholder interpolation or structured context rendering happens. +No placeholder interpolation happens: `{key}` in the message is written as it is. ## The `message` adapter ignores the context diff --git a/v3.0/packages/logger/helpers.md b/v3.0/packages/logger/helpers.md index b5e490e..95739e1 100644 --- a/v3.0/packages/logger/helpers.md +++ b/v3.0/packages/logger/helpers.md @@ -48,7 +48,7 @@ A few limits are easy to miss: - the convenience helpers do not let you choose an adapter - there are no global helpers for `critical`, `alert`, or `emergency` -- the convenience helpers accept string messages, while `logger()` also lets you send array payloads through the PSR-3 methods +- the convenience helpers and `logger()` both take a string message and a context; structured data goes in the context - every helper call goes back through the shared factory -Use `logger()` when you need the full PSR-3 surface, adapter selection, or structured array messages. +Use `logger()` when you need the full PSR-3 surface or adapter selection. diff --git a/v3.0/packages/logger/overview.md b/v3.0/packages/logger/overview.md index 392045b..f9c5173 100644 --- a/v3.0/packages/logger/overview.md +++ b/v3.0/packages/logger/overview.md @@ -64,6 +64,6 @@ In debug mode, `Logger::log()` bypasses threshold filtering and reports every ca - The active log threshold is stored in static `LoggerConfig` state, so resolving a logger updates process-wide logging behavior. - `LoggerFactory` caches one `Logger` instance per adapter name inside the factory service. - The `message` adapter is not available outside debug mode. -- File adapters only use the `trace` context key for output formatting. Other context values are ignored by file logging. +- File adapters write the context after the message as JSON, and a `trace` that is text on its own lines; see [File output format](adapters.md#file-output-format). Everything in the context reaches the log file. - The package does not validate arbitrary custom level strings. Stick to standard PSR log levels for predictable filtering and output. - The `daily` adapter picks its `.log` file when the adapter is created. In long-running workers, recreate/re-resolve the logger after midnight if you need strict daily rollover. diff --git a/v3.0/packages/logger/usage.md b/v3.0/packages/logger/usage.md index ad07b88..1588362 100644 --- a/v3.0/packages/logger/usage.md +++ b/v3.0/packages/logger/usage.md @@ -43,17 +43,22 @@ That writes the formatted message first and then appends the trace block. ## Log a structured payload -When you want file logs to capture structured data, call the logger instance directly: +Say what happened in the message and put the data in the context: ```php -logger()->info([ +logger()->info('Catalog sync queued', [ 'job' => 'catalog-sync', - 'status' => 'queued', 'items' => 48, ]); ``` -File adapters JSON-format array messages before writing them. The global level helpers accept string messages, so `logger()` is the better fit for this style. +A file adapter writes the context as JSON after the message: + +```text +[2026-11-23 14:02:11] Info: Catalog sync queued {"job":"catalog-sync","items":48} +``` + +The message must be text, as PSR-3 defines: `logger()->info([...])` throws a `TypeError`. The global helpers take a context too, for example `info('Catalog sync queued', ['items' => 48])`. See [File output format](adapters.md#file-output-format) for how exceptions, a `trace` and other values are written. ## Check what was logged in debug mode diff --git a/v3.0/upgrade-guide.md b/v3.0/upgrade-guide.md index 130c206..7a9583e 100644 --- a/v3.0/upgrade-guide.md +++ b/v3.0/upgrade-guide.md @@ -95,7 +95,22 @@ The starter project's tests run on PHPUnit 11.5. If your project keeps them, req - replace `@dataProvider` doc comments with the `#[DataProvider('...')]` attribute (`use PHPUnit\Framework\Attributes\DataProvider;`); PHPUnit 12 no longer reads doc comments. -### 8. Check the upgrade +### 8. Log lines carry the context + +The `single` and `daily` log adapters write the context of a log call after the message, as one line of JSON; they ignored it before: + +```text +[2026-11-23 14:02:11] Info: Order paid {"order":1042} +``` + +- A line without a context is unchanged. A tool that parses your log lines with a fixed pattern must allow the JSON at the end. +- Everything you pass in the context reaches the log file: check your log calls for passwords, tokens or full request payloads. +- The message stays text: `logger()->info([...])` throws a `TypeError`, as in 3.1. Put structured data in the context. +- A log adapter of your own that uses `Quantum\Logger\Traits\LoggerTrait` and passes an array to `formatMessage()` gets a `TypeError`: pass a string. + +See [File output format](packages/logger/adapters.md#file-output-format). + +### 9. Check the upgrade Update the dependencies and run the checks you already use: