matomo-for-wordpress icon indicating copy to clipboard operation
matomo-for-wordpress copied to clipboard

PSR-3 logger injection

Open Pierre-Lannoy opened this issue 3 years ago • 9 comments

Hello all!

This is just a question.

Do you know how (if it's doable) to inject a logger to be used by all processes ?

If I refer to matomo/app/core/Log.php, I see the Log class is deprecated. In the header it is specified I can inject a class or instance (not clear) implementing Psr\Log\LoggerInterface. Great! I have one. But the only way I found, is to do it in matomo/app/config/global.php which seems to be used everywhere a logger need to be instantiated ($this->logger = $logger ?: StaticContainer::get('Psr\Log\LoggerInterface');). It's not instance injection, but it is better than nothing… Nevertheless, doing so has a huge drawback: it is not update-proof!

As I have not found hooks that allow to dynamically do it, I came to the conclusion that I was missing something obvious…

Can anyone tell me where to look for / how to do that?

Thanks (and sorry for this noisy question)

Pierre-Lannoy avatar Jan 28 '22 17:01 Pierre-Lannoy

Hi @Pierre-Lannoy

I haven't tested it but you should be able to create a "Matomo" plugin see https://developer.matomo.org/guides/wordpress/matomo-plugin . This is effectively a regular WordPress plugin with some extra code that registers itself as a Matomo plugin.

Within this plugin, it should be possible to create a config/config.php file. There you could return an array in this config file to change the Logger dependency. Like below

<?php

return [
	'Psr\Log\LoggerInterface' => DI\get('\MyClass\MyLogger'),
];

Depending if you are familiar with https://php-di.org/ you could also use other ways like "Decorator"

	'Psr\Log\LoggerInterface' => DI\decorate(function ($previous) {
                   // some custom logic
		return $previous;
	})

Again, I haven't tested it and it's not clear what you are trying to achieve. Hoping this helps @Pierre-Lannoy ?

tsteur avatar Jan 31 '22 20:01 tsteur

Hi @tsteur Thank you so much for your answer. And sorry if I was not clear. Here is the story… I'm maintaining a WP plugin called DecaLog. Its main purpose is to log events, traces and metrics about WordPress (and plugins and themes). It is based on Monolog.

Regarding events logging, there's currently 3 ways DecaLog can "catch" events:

  • using the DecaLog SDK - in this case, the "observed" plugin or theme must embed the SDK, and call it by itself;
  • catching actions or filters implemented by the observed plugin or theme - in this case, it is the sole responsibility of DecaLog to implement the ad-hoc listener;
  • an implicit "contract" between DecaLog and the observed plugin or theme - in this case the observed plugin must implements a method to accept a PSR-3 logger.

I'm currently investigating to make the Matomo for WP plugin observable and catch as many events it can trigger (let's drop traces and metrics for now).

As I can't ask you to go with the first method, and as the second one is not really possible (due to the lack of events related actions in Matomo for WP plugin), I'm trying to do it with the 3rd method.

It's why, I'm trying to understand how to dynamically inject a PSR-3 logger to you plugin.

Does this make more sense to you?

Pierre-Lannoy avatar Jan 31 '22 21:01 Pierre-Lannoy

That makes more sense @Pierre-Lannoy . Do I understand it right to make the second method work, we could call something like do_action() and you could listen to it?

tsteur avatar Jan 31 '22 21:01 tsteur

Haha. Yes, but it must be done each time the plugin have an events to signal… So a huge refactoring. Hence the idea of injection (because DecaLog loggers are PSR-3 loggers) and it seems a little more "non-intrusive" from your plugin point of view.

Pierre-Lannoy avatar Jan 31 '22 21:01 Pierre-Lannoy

@Pierre-Lannoy just to double check, we can't simply do it here? https://github.com/matomo-org/matomo-for-wordpress/blob/4.6.0/classes/WpMatomo/Logger.php#L42

tsteur avatar Jan 31 '22 21:01 tsteur

It can be done here, that's the convenient place to call a simple do_action(). In this case there will be only the message and a severity in ['debug', 'info']. And the listener I will do in DecaLog will implement a small parser to "fix" severity for messages starting with 'Matomo error: '. But the next time you refactor your logging mechanism I will have to refactor my listener.

Nevermind, that's a good start. Do you want I submit a PR?

Pierre-Lannoy avatar Jan 31 '22 21:01 Pierre-Lannoy

Be great if you could submit a PR @Pierre-Lannoy

tsteur avatar Jan 31 '22 21:01 tsteur

Thanks @tsteur, I will do that today…

Pierre-Lannoy avatar Feb 01 '22 07:02 Pierre-Lannoy

Hi @Pierre-Lannoy

Will you finally submit a PR or I can close this issue right now?

Kind regards

Mat

mattmary avatar Jun 26 '22 23:06 mattmary

Hi @Pierre-Lannoy

I close this issue. Feel free to submit your PR when you want, we'll be happy to include it in Matomo. Kind regards

Mat

mattmary avatar Sep 13 '22 01:09 mattmary