Skip to content

[MonologBridge] Use composition instead of inheritance in monolog bridge #35740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2020

Conversation

pvgnd
Copy link
Contributor

@pvgnd pvgnd commented Feb 16, 2020

Q A
Branch? master
Bug fix? no
New feature? no
Deprecations? yes
Tickets
License MIT
Doc PR

In order to allow better customization in monolog activation strategy, this PR introduce composition instead of inheritance for these 2 classes :

  • HttpCodeActivationStrategy
  • NotFoundActivationStrategy

@pvgnd pvgnd changed the title WIP: Use composition instead of inheritance in monolog bridge WIP: [MonologBridge] Use composition instead of inheritance in monolog bridge Feb 16, 2020
@pvgnd pvgnd changed the title WIP: [MonologBridge] Use composition instead of inheritance in monolog bridge [MonologBridge] Use composition instead of inheritance in monolog bridge Feb 16, 2020
@pvgnd pvgnd force-pushed the activation-strategy-composition branch from 51cb2f1 to 0abcc9f Compare February 16, 2020 12:27
@nicolas-grekas nicolas-grekas added this to the next milestone Feb 18, 2020
@nicolas-grekas
Copy link
Member

Can you please give more details about the envisioned benefits?
This is a BC break so it likely won't be accepted.

@stof
Copy link
Member

stof commented Feb 18, 2020

Well, the benefit is that you can combine behavior of various activation strategies when using composition.

@pvgnd
Copy link
Contributor Author

pvgnd commented Feb 18, 2020

What I have in mind is to provide (in a dedicated bundle) a new ActivationStrategy implementation to be able to change dynamicaly (through endpoint or cli command or whatever) the actionLevel of ErrorLevelActivationStrategy or ChannelLevelActivationStrategy, and keep compatibility with HttpCodeActivationStrategy.
The use case would be to enable debug log for a short term period to be able to understand what is going wrong on the application, then switch back to the original log level.

@derrabus
Copy link
Member

You've basically implemented the decorator pattern for activation strategies. I see the benefit of stacking/decorating strategies, but the current implementation might break code that relies on the current inheritance. What we could do:

  • Keep the inheritance, but stop calling the parent methods. Since we override all methods anyway, that shouldn't be much of a problem. In Symfony 6, we can drop the parent class and switch to the interface. The downside is that we cannot trigger proper deprecations on this, can we?

  • Create your decorators as new classes and deprecate the current strategy classes. For instance, we would just deprecate HttpCodeActivationStrategy and create a new class HttpCodeActivationStrategyDecorator that should be used instead.

@pvgnd
Copy link
Contributor Author

pvgnd commented Feb 18, 2020

I see the point. Let me work on your second proposal. Additionally it will be more convenient to handle in monolog-bundle during the deprecated period.

@pvgnd
Copy link
Contributor Author

pvgnd commented Feb 27, 2020

@derrabus @stof @nicolas-grekas Here is a new proposal regarding your feedback

Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add "symfony/deprecation-contracts": "^2.1" to the bridge's composer.json.

@derrabus
Copy link
Member

Looks good to me.

I wonder if we should finalize the new decorator classes. We should rather encourage people to stack decorators than to extend ours.

@pvgnd pvgnd force-pushed the activation-strategy-composition branch 2 times, most recently from 3f63171 to 5f0efd3 Compare February 29, 2020 15:48
@pvgnd pvgnd force-pushed the activation-strategy-composition branch from 5f0efd3 to 0eb3578 Compare March 19, 2020 17:58
@pvgnd
Copy link
Contributor Author

pvgnd commented Mar 19, 2020

Just rebased on master, anything else I can do ?

@pvgnd pvgnd force-pushed the activation-strategy-composition branch from 0eb3578 to f53661a Compare March 19, 2020 18:00
@pvgnd pvgnd force-pushed the activation-strategy-composition branch from f53661a to 02c582b Compare March 29, 2020 09:40
@pvgnd pvgnd force-pushed the activation-strategy-composition branch from 02c582b to 1faa688 Compare April 5, 2020 13:54
@pvgnd pvgnd force-pushed the activation-strategy-composition branch from 1faa688 to 205e644 Compare April 18, 2020 09:18
@pvgnd pvgnd force-pushed the activation-strategy-composition branch from 205e644 to e871564 Compare April 25, 2020 10:44
@fabpot
Copy link
Member

fabpot commented Aug 16, 2020

Instead of creating new classes, I would instead accept both constructor signatures (we've done that multiple times in the past). The main benefit is to avoid having new class names.

@fabpot
Copy link
Member

fabpot commented Sep 6, 2020

@pvgnd Do you still want to work on this PR? What do you think of my proposal?

@pvgnd
Copy link
Contributor Author

pvgnd commented Sep 6, 2020

Hello @fabpot, your proposal was my first attempt before comments from other contributors. Introducing new classes allows us to make them final, which will discourage to break the decorator pattern. This will also make easier the deprecated code cleaning.

If you prefer to keep old classes and manage 2 constructor signatures, I can rework the PR. Please let me know and I'll give some time to rework on this.

@fabpot
Copy link
Member

fabpot commented Sep 11, 2020

If the goal was to make these classes final, you can mark them as such via the @final annotation at first and we would make them final in 6.0.

@pvgnd
Copy link
Contributor Author

pvgnd commented Sep 13, 2020

OK @fabpot I will update the PR

@pvgnd pvgnd force-pushed the activation-strategy-composition branch 3 times, most recently from deed44d to 8e00615 Compare September 17, 2020 15:46
@ghost
Copy link

ghost commented Sep 17, 2020

@fabpot Can you have a look please and tell me if it's OK for you?

*/
class HttpCodeActivationStrategy extends ErrorLevelActivationStrategy
/* final */ class HttpCodeActivationStrategy extends ErrorLevelActivationStrategy implements ActivationStrategyInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use a @final phpdoc instead.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -1,6 +1,11 @@
CHANGELOG
=========

5.2.0
-----
* The `$actionLevel` constructor argument of `Symfony\Bridge\Monolog\Handler\FingersCrossed\NotFoundActivationStrategy` has been deprecated and replaced by the `$inner` one which expects an ActivationStrategyInterface to decorate instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing blank line above

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I also mentioned that classes will become final in 6.0

*/
class NotFoundActivationStrategy extends ErrorLevelActivationStrategy
/* final */ class NotFoundActivationStrategy extends ErrorLevelActivationStrategy implements ActivationStrategyInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use a @final phpdoc instead.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@fabpot fabpot force-pushed the activation-strategy-composition branch from 745760d to 0fb1439 Compare September 18, 2020 08:50
@fabpot
Copy link
Member

fabpot commented Sep 18, 2020

Thank you @pvgnd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants