94827[!!!][FEATURE] Log writers support dependency injection
Commit message
Test this change
Run a command in your clone of the TYPO3 Core. Then try what the change does and vote on Verified in Gerrit. How to review a patch
- Check out patch set 5
-
git fetch https://review.typo3.org/Packages/TYPO3.CMS refs/changes/27/94827/5 && git checkout FETCH_HEAD - Cherry-pick onto main
-
git fetch https://review.typo3.org/Packages/TYPO3.CMS refs/changes/27/94827/5 && git cherry-pick FETCH_HEAD
Review
-
Code review CR +1 Needs a vote
- Georg Ringer +1
-
Verified V +1 Needs a vote
- Georg Ringer +1
- core-ci +1 CI
Merge conflicts 9
- main 97630[!!!][FEATURE] Support dependency injection for routing aspects
- main 97554[FEATURE] Introduce remote media providers
- main 96543[WIP][POC][TASK] Write and read records by the same rules and rights
- main 96661[TASK] Add capability invoker and registry
- main 96201[FEATURE] Introduce upload constraints for file uploads
- main 95637[TASK] Introduce FieldValidationService for TCA field validation
- main 84620[BUGFIX] Drop constructor arguments if not set
- main 83623[WIP][FEATURE] Introduce System Settings API & Editor
- main 70183[WIP][FEATURE] Log module should show last lines of FileWriters
Issues 1
Files 20
- typo3/sysext/core/Classes/DependencyInjection/LogWriterPass.php4new+72 −0
- typo3/sysext/core/Classes/Log/Writer/AbstractWriter.php4+38 −2
- typo3/sysext/core/Classes/Log/Writer/ConfigurableWriterInterface.php2new+35 −0
- typo3/sysext/core/Classes/Log/Writer/DatabaseWriter.php3+4 −0
- typo3/sysext/core/Classes/Log/Writer/FileWriter.php2+4 −5
- typo3/sysext/core/Classes/Log/Writer/RotatingFileWriter.php+2 −7
- typo3/sysext/core/Classes/Log/Writer/SyslogWriter.php+9 −1
- typo3/sysext/core/Classes/Log/LogManager.php5+86 −4
- typo3/sysext/core/Configuration/Services.php+5 −0
- typo3/sysext/core/Documentation/Changelog/15.0/Breaking-110222-LogWritersInstantiatedViaDependencyInjection.rst2new+86 −0
- typo3/sysext/core/Documentation/Changelog/15.0/Deprecation-110222-LogWriterOptionsConstructor.rstnew+65 −0
- typo3/sysext/core/Documentation/Changelog/15.0/Feature-110222-LogWritersSupportDependencyInjection.rstnew+80 −0
- typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_logger/Classes/ConstructorInjectionWriter.phpnew+36 −0
- typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_logger/Classes/DirectInterfaceWriter.phpnew+35 −0
- typo3/sysext/core/Tests/Functional/Log/EarlyBootWriterTest.phpnew+69 −0
- typo3/sysext/core/Tests/Functional/Log/WriterDependencyInjectionTest.phpnew+81 −0
- typo3/sysext/core/Tests/Unit/Log/Writer/AbstractWriterTest.php+2 −2
- typo3/sysext/core/Tests/Unit/Log/Writer/FileWriterTest.php+3 −1
- typo3/sysext/core/Tests/Unit/Log/Writer/RotatingFileWriterTest.php+22 −6
- typo3/sysext/core/Tests/Unit/Log/LogManagerTest.php+19 −0
Review log 1015
-
Benni Mack uploaded patch set 1 07/17/26, 20:09
-
core-ci voted on patch set 1 V +1 07/17/26, 20:18 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/104912
-
Benni Mack rebased onto patch set 2
V +107/17/26, 20:30 -
core-ci voted on patch set 2 V +1 07/17/26, 20:40 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/104914
-
Stefan Bürk commented on patch set 2 CR -1 07/17/26, 22:54
Thanks — good continuation of the series, the construction/configuration split reads well. I left inline comments; the two I'd like resolved before +1 are the **DatabaseWriter constructor being breaking for subclasses** (not only for `new DatabaseWriter()` — the Breaking changelog needs the subclassing case) and the **broadened `catch (\Error)`** that turns those and other construction failures into silent warnings. The rest (options dropped for direct `WriterInterface` writers, `afterConfiguration()` re-runs, a few doc/cleanup nits) are inline.LogWriterPass.php:50Core writer and `AbstractWriter` (I assume most implementation would use the abstract anyway over interfaces) does not uses `SingletonInterface` at all, so did you added this here ? Keep writer shared (based on the SingeletonInterface) is not good and would lead to the effect that if that writer is used (configured) multiple times (for sub classes and similar) that the "last" instanciation and setOption() would win for "all occurances") and literally ignore the dedicaded options. Should we better throw a runtime exception here to really make users of this dangerous usage aware ? Or did I missed something ?LogWriterPass.php:53Two keying edge cases worth a short comment or guard: `$serviceMap` is keyed by `$definition->getClass() ?? $id`, so two service ids resolving to the same class collide (last wins), and a factory-defined service with a `null` class keys by its id — which won't match the class-name lookup `LogManager` does from the `LOG` config, silently falling back to `makeInstance`. Not sure if that is fine for writers in practice, at least it is non-obvious.LogManager.php:175Adding `\Error` here also catches `ArgumentCountError`/`TypeError` thrown while constructing a writer, downgrading them to a warning. Two effects worth weighing: (1) a misconfigured or legacy writer e.g. a `DatabaseWriter` subclass forwarding options to the parent, see the DatabaseWriter comment - fails silently instead of surfacing (2) `DatabaseWriter` resolved before the container is available (early boot / failsafe) falls back to `makeInstance(DatabaseWriter::class)` without a `ConnectionPool` → `ArgumentCountError` → silently no DB logging for that phase. Could we narrow this to `\ArgumentCountError`, or document why the broad `\Error` is intentional?LogManager.php:211Options are applied only for `ConfigurableWriterInterface` writers. A custom writer that implements `WriterInterface` **directly** (not via `AbstractWriter`) and historically received its options through the constructor now loses them silently: it is tag-collected, built from the locator on line 196 with no constructor arguments, and never gets `setOptions()`. Minimal repro — a writer with `public function __construct(array $options = [])` configured as `['LOG']['writerConfiguration'][level][MyWriter::class] = ['foo' => 'bar']` receives `[]` (before this change it got `['foo' => 'bar']` via makeInstance($class, $options)`). Consider applying options to any writer that exposes a compatible setter, or calling this out in the Breaking notes.AbstractWriter.php:33The `@deprecated` tag is on the whole constructor, but the no-argument constructor is the supported DI entry point and subclasses legitimately call `parent::__construct()` (e.g. `SyslogWriter`). Deprecation-aware tooling may flag those supported calls. Consider scoping the wording to "passing options to the constructor is deprecated".AbstractWriter.php:62`afterConfiguration()` runs on every `setOptions()` call. A writer registered as a singleton (`SingletonInterface`, kept shared by `LogWriterPass`) that is configured under several loggers/levels receives `setOptions()` more than once on the same instance, so `afterConfiguration()` runs repeatedly e.g. `SyslogWriter` would `openlog()` again, `FileWriter` re-evaluates its default-file fallback. Intended? A run-once guard or a note that it must be idempotent would help. On the otherhand a shared instance (which SingletonInterface is litteraly also) would assign the "same" instace over and over again - or not ? I'm not sure - I guess that I need to look on that again (or someone else) as I'm to tired now :/ConfigurableWriterInterface.php:34```suggestion public function setOptions(array $options): static; ``` Nit (new v15 API, so worth getting right now): `self` in an interface fixes the return type to the interface; `static` is the conventional choice for a fluent setter and lets subclasses return their own type.DatabaseWriter.php:28`DatabaseWriter` is neither `final` nor `@internal`, so by our API policy it is public, extensible API — extensions may subclass it, and before this change a subclass could be constructed through the inherited `AbstractWriter(array $options = [])` constructor. Adding a required `ConnectionPool` argument breaks existing subclasses in two ways: - `class MyWriter extends DatabaseWriter {}` → `new MyWriter()` now throws `ArgumentCountError` (worked before). - `class MyWriter extends DatabaseWriter { public function __construct(array $options = []) { parent::__construct($options); } }` — a supported, idiomatic pre-v15 pattern — now passes an array into `__construct(ConnectionPool)` → `TypeError`, a hard fatal with no deprecation. When `LogManager` builds such a writer, that `TypeError` is additionally swallowed by the broadened `catch (\Error)`, so the writer silently stops logging. This is breaking beyond "instantiate DatabaseWriter directly". Please either document the subclassing break in the Breaking changelog (both variants + the `parent::__construct($options)` fatal), or, if subclassing should no longer be allowed, add an explicit `final`/`@internal` deprecation for it rather than changing the signature silently.FileWriter.php:67Now that `FileWriter`'s constructor is gone in favour of `afterConfiguration()`, `RotatingFileWriter` (not touched in this change) is left with a redundant `__construct(array $options = []) { parent::__construct($options); }`, and its `setInterval()`/`setMaxFiles()` docblocks still read "Internal setter called by FileWriter constructor", which is no longer true (driven by `setOptions()` now). Worth cleaning up here.Breaking-110222-LogWritersInstantiatedViaDependencyInjection.rst:43This covers instantiating `DatabaseWriter` directly, but not **subclasses**. As `DatabaseWriter` is not `final`/`@internal`, extensions may extend it, and the new required constructor argument breaks (a) a subclass with no own constructor (`new Sub()` → `ArgumentCountError`) and (b) a subclass overriding the constructor and calling `parent::__construct($options)` (array into `ConnectionPool` → `TypeError`). Please add both to Impact/Migration so extension authors know what to change. -
Benni Mack uploaded patch set 3
CR -1V +107/21/26, 11:24 -
core-ci voted on patch set 3 V +1 07/21/26, 11:33 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/105095
-
Benni Mack commented on patch set 3 07/21/26, 13:40
LogWriterPass.php:50Reply UnresolvedAdded an explanatory comment. I didn't throw: the pass runs at compile time and can't see the runtime LOG config, so it can't know whether a singleton writer is configured more than once. No core writer implements SingletonInterface, so this is opt-in behaviour — if someone deliberately marks a writer as a singleton, honouring that (one shared instance, last setOptions() wins) is the documented trade-off rather than a surprise. Happy to escalate to a runtime exception in LogManager if you feel strongly.LogWriterPass.php:53Reply UnresolvedAdded a comment covering all three: writers are keyed by class name because LogManager looks them up by the class name from LOG config; a factory service with no resolvable class keeps its id as key and falls back to makeInstance(); two ids resolving to the same class → last wins. These are non-obvious but not practically reachable for writers, so I documented rather than guarded.LogManager.php:175ReplyDone. Narrowed to \ArgumentCountError. A TypeError (e.g. a subclass forwarding parent::__construct($options) into the injected ConnectionPool) now surfaces instead of being downgraded to a warning. I kept \ArgumentCountError caught on purpose: that is the benign early-boot/failsafe case where DatabaseWriter falls back to makeInstance() without a ConnectionPool — there is no DB in that phase anyway, so a warning is the right outcome rather than a fatal.LogManager.php:211Reply UnresolvedDone, and made non-silent. A writer resolved from the locator that does not implement ConfigurableWriterInterface but is configured with options now throws InvalidLogWriterConfigurationException instead of quietly discarding them (it becomes a visible warning via the catch). The legacy makeInstance($class, $options) path for non-service writers is unchanged. Documented in the Breaking notes and covered by a new functional test (DirectInterfaceWriter fixture in test_logger).AbstractWriter.php:33Reply UnresolvedDone. Reworded the @deprecated tag to "Passing $options to a log writer constructor is deprecated …" and added prose clarifying that the no-argument constructor is the supported DI entry point and parent::__construct() stays fine. The RST is NotScanned, so the deprecation scanner won't flag the supported calls either.AbstractWriter.php:62Reply UnresolvedDocumented the idempotency contract on the hook rather than adding a run-once guard: re-running after an options change is actually desired, so the requirement is that the side effects be idempotent. In practice the only shared case is a writer marked SingletonInterface, and the core side-effecting impls are safe under repeat calls (SyslogWriter isn't a singleton, so it gets a fresh instance per config anyway; FileWriter guards on $logFile === ''). A run-once flag would be wrong here because it would freeze the first configuration.ConfigurableWriterInterface.php:34Reply UnresolvedDone in both the interface and AbstractWriter::setOptions().DatabaseWriter.php:28ReplyDone — documented rather than sealing the class, to avoid an extra breaking restriction. The Breaking changelog now covers both variants (bare subclass → ArgumentCountError; parent::__construct($options) → TypeError) in Impact, plus a before/after migration dropping the options-forwarding constructor and letting ConnectionPool be injected. Left DatabaseWriter non-final so the DI-based subclassing pattern keeps working.FileWriter.php:67Reply UnresolvedDone. Removed the now-redundant __construct(array $options = []) and updated the setInterval()/setMaxFiles() docblocks to "invoked while applying options via setOptions()". -
Sebastian Michaelsen commented on patch set 3 V -1 10/01/26, 12:25
With an existing configuration of $GLOBALS['TYPO3_CONF_VARS']['LOG']['writerConfiguration']['warning'][\TYPO3\CMS\Core\Log\Writer\DatabaseWriter::class] = []; and just calling the TYPO3 backend login page I get multiple warnings in typo3_*.log, like: ``` [WARNING] component="TYPO3.CMS.Core.Error.ErrorHandler": Instantiation of LogWriter "TYPO3\CMS\Core\Log\Writer\DatabaseWriter" failed for logger TYPO3.CMS.Core.Error.ErrorHandler - ArgumentCountError: Too few arguments to function TYPO3\CMS\Core\Log\Writer\DatabaseWriter::__construct(), 0 passed ... and exactly 1 expected ``` The loggers for the error/exception handlers are created in Bootstrap before the DI container exists and in that situation we can no longer build a DatabaseWriter because it now needs a ConnectionPool in its constructor. Previously the writer fetched the ConnectionPool lazily at write time, so it worked in that window. -
Sebastian Michaelsen uploaded patch set 4
V +110/01/26, 14:23 -
core-ci voted on patch set 4 V +1 10/01/26, 14:33 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/109810
-
Sebastian Michaelsen commented on patch set 4 10/01/26, 14:33
Reply
DoneLogManager.php:175ReplyDoneDatabaseWriter.php:28ReplyDoneBreaking-110222-LogWritersInstantiatedViaDependencyInjection.rst:43ReplyDone -
Georg Ringer uploaded patch set 5
V +111/03/26, 17:52 -
Georg Ringer commented on patch set 5 CR +1 V +1 11/03/26, 17:53
This change is ready for review. -
core-ci voted on patch set 5 V +1 11/03/26, 18:19 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/113199