Review main Abandoned

70842[FEATURE] Add monolog as alternative logger implementation

Benjamin Franzke CR 0 V 0 Expert +1017 −22 updated 06/11/23, 22:58

Commit message

Both the (now) legacy TYPO3 logging framework and monolog are PSR-3
compatible, thus there is no change in actual log calls. However,
there is a change regarding the configuration of monolog in TYPO3.

The monolog configuration (and logging) format is different to
the current TYPO3 logging implementation.

The configuration and logging service definition format is
inspired by the symfony monolog-bundle format.

Monolog processors and channels are defined via Service tags.
Monolog handlers are configured via TYPO3_CONF_VARS. The
handler configuration defines the handler type, destination,
related channels, and the desired formatter.

Executed commands:

  composer require monolog/monolog:"^2.3"

Review

  • Code review CR 0
  • Verified V 0

Issues 2

Files 22

Review log 2336

Show 611 earlier messages
  1. Benjamin Franzke uploaded patch set 1 08/31/21, 14:50

  2. core-ci voted on patch set 1 V -1 08/31/21, 15:15 Core CI is not happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/15943

  3. Benjamin Franzke uploaded patch set 2 08/31/21, 15:33

  4. core-ci voted on patch set 2 V +1 08/31/21, 15:53 Core CI is happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/15954

  5. Benjamin Franzke uploaded patch set 3 08/31/21, 20:36

  6. core-ci voted on patch set 3 V -1 08/31/21, 20:41 Core CI is not happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/15969

  7. Benjamin Franzke uploaded patch set 4 08/31/21, 20:42

  8. core-ci voted on patch set 4 V +1 08/31/21, 20:59 Core CI is happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/15971

  9. Benjamin Franzke uploaded patch set 5 09/02/21, 00:07

  10. core-ci voted on patch set 5 V -1 09/02/21, 00:25 Core CI is not happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/16041

  11. Benjamin Franzke uploaded patch set 6 09/02/21, 01:04

  1. core-ci voted on patch set 6 V +1 09/02/21, 01:26 Core CI is happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/16043

  2. Benjamin Franzke uploaded patch set 7 09/02/21, 14:35

  3. core-ci voted on patch set 7 V -1 09/02/21, 15:13 Core CI is not happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/16088

  4. Benjamin Franzke uploaded patch set 8 09/02/21, 21:27

  5. Benjamin Franzke uploaded patch set 9 09/02/21, 21:45

  6. core-ci voted on patch set 8 V -1 09/02/21, 21:54 Core CI is not happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/16140

  7. core-ci voted on patch set 9 V -1 09/02/21, 22:06 Core CI is not happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/16141

  8. Benjamin Franzke uploaded patch set 10 09/03/21, 06:26

  9. core-ci voted on patch set 10 V +1 09/03/21, 06:43 Core CI is happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/16153

  10. crell commented on patch set 10 09/03/21, 17:41

    HandlerFactoryRegistry.php:30

    What's the difference between handler factories and handler services? This needs docs.

    HandlerFactoryRegistry.php:49

    $config['disabled'] ?? false is checked twice in just a few lines. It would be cleaner (for both this and channels) to keep it in the array and use ??= the first time to ensure it has a legit value. Viz, the line earlier changes to: $config['disabled'] ??= false; And then just use $config['disabled'] thereafter. Alternatively, since there's several of them, you can do it in a single operation: $config += [ 'disabled' => false, 'channels' => [], 'excluded_channels' => [], // ... ];

    LazyLogger.php:45

    This could also be inlined with ??= $logger = $this->logger ??= $this->logManager->getLoggerInstance($this->name);

    LogManager.php:76

    Shouldn't a Monolog-based LogManager be an alternate implementation of LogManagerInterface? The interface is there; this is exactly what alternate implementations are for.

    LoggerComponentClosure.php:21

    I... have no idea what this class is for, except that it is not a closure nor does it use one. So at minimum the name is bad. It needs a more descriptive name and likely a docblock to explain why it exists.

    DefaultConfiguration.php:1559

    I'm honestly not sure if the line formatter is appropriate for the DB handler or not. I had been planning to skip it and just use the raw data. Formatting should be done on display, not on storing into the DB.

    Services.yaml:430

    Is the intent to comment these out for committing, or remove them, or...? If they're left in then they're not really a demo.

    Feature-85506-AddMonologAsAlternativeLoggerImplementation.rst:20

    Period, not comma.

    Feature-85506-AddMonologAsAlternativeLoggerImplementation.rst:71

    Brackets missing, as this should be an array.

    Feature-85506-AddMonologAsAlternativeLoggerImplementation.rst:74

    It's not obvious to me if the attribute channel mechanism also works to create channels on the fly. It was already committed, but I'm not entirely clear if it works with this Monolog implementation. If it does, that should be included in this feature file for clarity.

    composer.json:41

    Why 2.0 and not 2.3? This will still get the 2.3 version of course but why not set the minimum at 2.3?
  11. Benni Mack commented on patch set 10 09/03/21, 20:36

    Interesting approach, feels like a in-between mix of Symfony and TYPO3's current solution. Two things I wonder (and I want to experiment with): 1. Setting up the handlers / configuration of the handlers -> I think we should set up the handlers "on the fly", taking the configuration from TYPO3_CONF_VARS -> I dislike the autowiring magic, and I'm more in favor of how Laravel is just configuring it. 2. Having a solution (e.g. a trait) that allows our Logger to inherit from Monolog's Logger and first do Monologs logging solution (with an array), and then run TYPO3's processors and writers. What do you think about this approach? Never-the-less: Most important part is that our Writers should handle if it should be logged, not the Logger themselves as we do it right now. Maybe that would be something to be tackled as a pre-patch.

    DatabaseHandlerFactory.php:32

    I guess we'd need a Factory for all of Monolog's processors now, which have dependencies. Maybe that's the reason Symfony's Monolog Bridge is so massive. I'm not a fan of this...

    LogManager.php:76 Reply

    Argh, this is bad. We use Loggers in the Error Handler, which are registered before the DI container is built. this change seems like a bad work-around, I wonder if we could do the "switching" in the bootstrap, and use another LogManager. Or if we should re-set the ErrorHandlers after the DI container is done, and just hand in NullLoggers to the ErrorHandler until the DI container is available. I mean, that's a more general topic (out-of-scope here). don't know if we can resolve this
  12. Benjamin Franzke uploaded patch set 11 09/04/21, 15:24

  13. Benjamin Franzke commented on patch set 11 09/04/21, 15:24

    DatabaseHandlerFactory.php:32 Reply Unresolved

    TL;DR no, only for the common ones. Yea, well, therefore I've added the `monolog.handler` tag which allows to create *AND* configure a new handler via Services.yaml, without the need for a factory. my.whatever.I.name.it.stderr.handler: class: Monolog\Handler\StreamHandler arguments: $path: 'php://stderr' $level: 'debug' tags: - name: monolog.handler excluded_channels: foobar So we only need factories for the very basic handlers that need to be dynamically configurable via CONF_VARS.. @Benni: Fine by this?

    HandlerFactoryRegistry.php:30 Reply

    See .rst file for the reason/difference: The respective type needs to be implemented by a HandlerFactory. TYPO3 doesn't provide a factory for every monolog handler, as handlers can also be defined via dependency injection service definition Anyway, moved the $handlerServices out here (that was stupid to put it into handlerFACTORYregistry at all), as they can of course directly be wired in the compiler pass.

    HandlerFactoryRegistry.php:49 Reply

    Ack, fixed, thank you.

    LazyLogger.php:45 Reply

    Ack,done.

    LogManager.php:76 Reply

    @Larry: sure, thought about it, but no chance: Throughout the core the `LogManager` is used *directly* throughout the core, and third party extensions do so as well. The LogManager is old, there were no DI, so noone used the interface as GeneralUtility::makeInstance couldn't resolve that.. …and I don't want to deprecate it and say please use "LogManagerInterface" instead, as I don't want anyone to use it directly, but to use dependency injection instead. Therefore we are bound to the "one" LogManager implementation. @Benni: This change isn't about the early bootstrap, it is in order to respect the feature toogle. This change already incorporates NullLogger for early bootstrap of error handlers and then injects new logger instances after container is setup (see Bootstrap.php).

    LoggerComponentClosure.php:21 Reply

    Preface: This is to support the legacy TYPO3 logging of the "component" (class). Yea well, a closure binds a function to it's scope – although in PHP you need to do this explicitly. So PHP closures are shit anyway and no real closures. But still, it is the intention of this class to be like a Closure, just for an object. This class binds the logger to the scope (=className/component), therefore I found this name to fit somehow. If one get's confused with real \Closure, well, then we should find a better name. I could also have used an anonymous class instead, then naming the class wouldn't have been a problem. I hope you agree, that an anonymous class is much like a Closure, but just for objects… I therefore simply switched to an anonymous class. Issue resolved ;)

    DefaultConfiguration.php:1559 Reply

    Right, thanks for bringing this up. The DatabaseHandler didn't use the formatted result at all, but already used the raw data. I've included this setting as AbstractProcessableHandlers default to using the LineFormatter if nothing is defined explicitly. Therefore I included it in this line to demonstrate this fact (should have put a comment in here). Of course, it made no sense in this case, as we don't use the result of the formatting. Conclusion: * I removed the needless formatter setting * I adapted the DatabaseHandler to not implement FormattableHandlerInterface at all, as it makes to sense to run a unused formatting here…

    Services.yaml:430 Reply

    Yup, intent to remove. Did so right now (and rather moved into my local demo test extension)…

    Feature-85506-AddMonologAsAlternativeLoggerImplementation.rst:20 Reply

    Ack

    Feature-85506-AddMonologAsAlternativeLoggerImplementation.rst:71 Reply

    Arrays are not supported by symfony DI tags. Therefore, the channels string is exploded by comma into an array.

    Feature-85506-AddMonologAsAlternativeLoggerImplementation.rst:74 Reply

    It doesn't autocreate channels – a channel needs to be defined explicitly. Yes, should be documented more clearly if this isn't clear enough. Maybe "have to be" or "MUST be" instead of "can be?" Changed to "have to be" for now.

    composer.json:41 Reply

    Oh, well, I didn't care, rebased an old patch and didn't have a deeper look into the current version. I'm fine to incease the minimum requirement, and did so in the latest patchset.
  14. Benjamin Franzke uploaded patch set 12 09/04/21, 15:26

  15. Benjamin Franzke uploaded patch set 13 09/04/21, 15:28

  16. core-ci voted on patch set 13 V +1 09/04/21, 15:44 Core CI is happy: https://gitlab.typo3.org/typo3/CI/cms/-/pipelines/16259

  17. crell commented on patch set 13 09/07/21, 15:29

    Just some minor additional notes.

    LogManager.php:167

    This may well be the first use of an anonymous class I've seen outside of a test case setup. :-) (Congrats?)

    Feature-85506-AddMonologAsAlternativeLoggerImplementation.rst:24

    I think this is a grammar/translation issue. As written, this implies that the traditional framework is deprecated now, and will be undeprecated in 11.5. I don't think that's the intent. I think you mean "deprecated in 11.5 LTS", not "until." (Assuming that's the intent.)
  18. Benjamin Franzke commented on patch set 13 09/13/21, 15:57

    LogManager.php:167 Reply Unresolved

    No, look at or MiddlewareDispatcher, it uses an anonymous class as well, in order to bundle the stack-context into the PSR-15 request handler. And well, that's also authored by me, so I take the (likely ironical?) congratulations anyway…

    LogManager.php:179 Unresolved

    Note by Helmut Hummel: We should probably add a way to filter handlers by the TYPO3_COMPONENT context/extra data. Note by me: Could be implemented by a bubbling/non-bubbling (dependening on filter-state) Handler that is pushed above(before) other monolog handlers. => would be good to add such a ComponentFilterHandler(?) with and easy way to attach to channels or handlers (to be decided).

    Feature-85506-AddMonologAsAlternativeLoggerImplementation.rst:24 Reply Unresolved

    I meant "until [the release of] 11.5 LTS". I mean, I'm not a native english speaker, maybe that's still wrong grammar/usage of until… I think the sentence was pretty clear due to the introduction to say "it is likely that" ;) Anyway, now that the patch didn't end up in 11.4 that sentence would be removed anyway. Thanks for the heads up.

    Feature-85506-AddMonologAsAlternativeLoggerImplementation.rst:40 Unresolved

    Remark by Helmut Hummel: Example for configuration file location is missing. (Note: That is the key 'path') Should be included here.
  19. Benni Mack wrote 11/29/21, 10:09

    Change destination moved from master to main
  20. Benni Mack updated the commit message in patch set 14 11/29/21, 13:27

  21. core-ci voted on patch set 14 V +1 11/29/21, 14:21 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/20032

  22. Benni Mack uploaded patch set 15 12/18/21, 07:46

  23. core-ci voted on patch set 15 V -1 12/18/21, 07:57 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/21229

  24. Christian Kuhn commented on patch set 15 06/11/23, 22:58

    hey. the logging topic seems to be stuck for quite a while (again). This patch stalled and it looks as if nobody is taking care of it at the moment. I'll abandon here for now. We could pick up later in case this gains traction again.
  25. Christian Kuhn abandoned the change 06/11/23, 22:58