Skip to content

[Bridge/Monolog] Enhance the Console Handler #21705

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
Mar 6, 2017

Conversation

lyrixx
Copy link
Member

@lyrixx lyrixx commented Feb 21, 2017

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? -
Fixed tickets -
License MIT
Doc PR -

I extracted and enhanced the formatting logic from #21080.
Basically, The formatter now use the VarDumper & use more significant colors and has a more compact / readable format (IMHO).


I used the following code to generate before/after screenshots.

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $logger = $this->getContainer()->get('logger');
        $filesystem = $this->getContainer()->get('filesystem');

        $anObject = new \stdClass;
        $anObject->firstPpt = 'foo';
        $anObject->secondePpt = 'bar';

        $logger->log('notice', 'Hello {who}', [
            'who' => 'Wold',
            'an_object' => $anObject,
            'file_system' => $filesystem,
        ]);

        $r = new \ReflectionClass(LogLevel::class);
        foreach ($r->getConstants() as $level) {
            $logger = $logger->withName($level);
            $logger->log($level, 'log at level {level}', [
                'level' => $level,
            ]);
        }
    }

before:

screenshot12

After:

screenshot11

}
if (isset($args[1])) {
$options['date_format'] = $args[1];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What about the two last arguments?

Copy link
Member Author

Choose a reason for hiding this comment

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

They are not relevant anymore. So I don't need to "map" them.

Copy link
Member

Choose a reason for hiding this comment

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

shouldn't $allowInlineLineBreaks map to multiline ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed. I will fix it in the next pr. Thanks.

Copy link
Contributor

@mpdude mpdude Sep 14, 2018

Choose a reason for hiding this comment

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

So you intentionally removed the option to suppress emtpy context and extra data?

Update: Just saw that this was mentioned in #21705 (comment) as well.

{
parent::__construct($format, $dateFormat, $allowInlineLineBreaks, $ignoreEmptyContextAndExtra);
// BC Layer
Copy link
Contributor

@greg0ire greg0ire Feb 21, 2017

Choose a reason for hiding this comment

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

You might want to document what the signature you're emulating here looks like

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure ;) And I have to add a trigger_error too. I just wait some feedback before.

'multiline' => false,
), $options);

$this->cloner = new VarCloner();
Copy link
Contributor

@greg0ire greg0ire Feb 21, 2017

Choose a reason for hiding this comment

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

No DI here or line 83?

Copy link
Member Author

Choose a reason for hiding this comment

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

No indeed. IMHO it's useless and there are other part of the framework where the dumper / cloner is hard-coded like that.

About the line 83, I cannot anyway as I do need a CLIDumper and well configured.

@lyrixx lyrixx force-pushed the log-command-refacto branch from 4103b61 to 5fe4af5 Compare February 21, 2017 17:25
@lyrixx
Copy link
Member Author

lyrixx commented Feb 21, 2017

I made other attempts to find "the best" formatting. What do you prefer?

screenshot8

A == 👍
B == 😆
C == 🎉
D == ❤️

@lyrixx lyrixx force-pushed the log-command-refacto branch from 5fe4af5 to 6823479 Compare February 21, 2017 20:23
*/
public function __construct($options = array())
{
// BC Layer, old constructor:
Copy link
Contributor

Choose a reason for hiding this comment

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

What I meant by "documenting what it looks like" was more sth like :

// BC layer for this signature : __construct($format = null, $dateFormat = null)

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, it's "documented" just bellow in the trigger error message.

For the record, I added what you asked for, then I added the deprecation, then I removed because it was a kind of duplicate. and now I notice I forgot to remove , old constructor:

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright!

{
// BC Layer, old constructor:
if (!is_array($options)) {
@trigger_error(sprintf('The constructor arguments $format, $dateFormat, $allowInlineLineBreaks, $ignoreEmptyContextAndExtra of %s are deprecated and will be removed in 4.0. Use $options insteand.', self::class), E_USER_DEPRECATED);
Copy link
Contributor

Choose a reason for hiding this comment

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

insteand => instead

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. Fixed.

@lyrixx lyrixx force-pushed the log-command-refacto branch from 6823479 to 2f01f9f Compare February 22, 2017 10:25
@greg0ire
Copy link
Contributor

In your commit message :

- Basically, The formatter now use the VarDumper & use more significant…
+ Basically, the formatter now uses the VarDumper & uses more significant…

@lyrixx lyrixx force-pushed the log-command-refacto branch 2 times, most recently from b516ef7 to 1ba3fcd Compare February 22, 2017 10:37
@lyrixx lyrixx force-pushed the log-command-refacto branch from 1ba3fcd to 7a2de64 Compare February 22, 2017 13:14
@lyrixx
Copy link
Member Author

lyrixx commented Feb 22, 2017

Looks like the C is the favorite one ! ❤️ it's mine too.

This PR is now ready for the review ;)

Copy link
Contributor

@romainneutron romainneutron left a comment

Choose a reason for hiding this comment

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

👍

{
// BC Layer
if (!is_array($options)) {
@trigger_error(sprintf('The constructor arguments $format, $dateFormat, $allowInlineLineBreaks, $ignoreEmptyContextAndExtra of %s are deprecated and will be removed in 4.0. Use $options instead.', self::class), E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

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

Missing quotes around %s (should be "%s")

Copy link
Member

Choose a reason for hiding this comment

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

are deprecated since 3.3

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks; fixed.

), $options);

if (!class_exists(VarCloner::class)) {
throw new \RuntimeException('To use the ConsoleFormatter you must install the symfony/var-dumper component.');
Copy link
Member

Choose a reason for hiding this comment

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

This could be moved to the top, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. Fixed.

@lyrixx lyrixx force-pushed the log-command-refacto branch from 7a2de64 to df24afc Compare February 22, 2017 16:29
@fabpot
Copy link
Member

fabpot commented Feb 22, 2017

Quick question: would it be possible to fallback to the old way when vardumper is not installed?

@lyrixx
Copy link
Member Author

lyrixx commented Feb 22, 2017

Quick question: would it be possible to fallback to the old way when vardumper is not installed?

It's not really easy (but not impossible) since the ConsoleFormatter does not extend the LineFormatter anymore. It just implements the interface.

We can create two ConsoleFormatter implementation (the new and the old one). Then the ConsoleHandler can choose and the "best" one. (What about the naming ?)

Another solution is to extends again the LineFormatter.

What is the best option to you?

const SIMPLE_DATE = 'H:i:s';

private static $levelColorMap = array(
100 => 'fg=white',
Copy link
Member

Choose a reason for hiding this comment

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

what's about using monolog's (or PSR) constants?

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. because the map will not be aligned ;) just kidding !
  2. PSR contant with int level does not exist

I updated the PR to use Monolog\Logger::CONSTANT

@lyrixx lyrixx force-pushed the log-command-refacto branch from df24afc to 5fa5041 Compare February 22, 2017 18:17
@nicolas-grekas nicolas-grekas added this to the 3.3 milestone Feb 25, 2017
@fabpot
Copy link
Member

fabpot commented Mar 1, 2017

@lyrixx, I think we still need to support the case where var-dumper is not installed as we are trying to limit the required dependencies.

@lyrixx lyrixx force-pushed the log-command-refacto branch from 5fa5041 to 4487f99 Compare March 6, 2017 16:19
@lyrixx
Copy link
Member Author

lyrixx commented Mar 6, 2017

@lyrixx, I think we still need to support the case where var-dumper is not installed as we are trying to limit the required dependencies.

I pushed a new version: Now if the var-dumper component is not installed, the context and the extra are not dumped at all.

I chose to do that in order to keep the code as simple as possible.

I could also extends the NormalizeFormatter, but there is lot of code in the LineFormatter. So I don't think it worth it.

private $dumper;

/**
* {@inheritdoc}
Copy link
Member

Choose a reason for hiding this comment

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

This should be removed as you don't extend anything anymore. Also, the phpdoc should document the available option keys.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. I updated the PHP Doc.

@lyrixx lyrixx force-pushed the log-command-refacto branch 2 times, most recently from a11d586 to 69ec738 Compare March 6, 2017 17:01
@fabpot
Copy link
Member

fabpot commented Mar 6, 2017

Last thing, can you add a note in the CHANGELOG?

Basically, the formatter now uses the VarDumper & uses more significant
colors and has a more compact / readable format (IMHO).
@lyrixx lyrixx force-pushed the log-command-refacto branch from 69ec738 to b663ab5 Compare March 6, 2017 17:10
@lyrixx
Copy link
Member Author

lyrixx commented Mar 6, 2017

Last thing, can you add a note in the CHANGELOG?

Done ;)

@fabpot
Copy link
Member

fabpot commented Mar 6, 2017

Thank you @lyrixx.

@fabpot fabpot merged commit b663ab5 into symfony:master Mar 6, 2017
fabpot added a commit that referenced this pull request Mar 6, 2017
This PR was merged into the 3.3-dev branch.

Discussion
----------

[Bridge/Monolog] Enhance the Console Handler

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | -
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

---

I extracted and enhanced the formatting logic from #21080.
Basically, The formatter now use the VarDumper & use more significant colors and has a more compact / readable format (IMHO).

---

I used the following code to generate before/after screenshots.

```php
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $logger = $this->getContainer()->get('logger');
        $filesystem = $this->getContainer()->get('filesystem');

        $anObject = new \stdClass;
        $anObject->firstPpt = 'foo';
        $anObject->secondePpt = 'bar';

        $logger->log('notice', 'Hello {who}', [
            'who' => 'Wold',
            'an_object' => $anObject,
            'file_system' => $filesystem,
        ]);

        $r = new \ReflectionClass(LogLevel::class);
        foreach ($r->getConstants() as $level) {
            $logger = $logger->withName($level);
            $logger->log($level, 'log at level {level}', [
                'level' => $level,
            ]);
        }
    }
```

before:

![screenshot12](https://cloud.githubusercontent.com/assets/408368/23183075/0bb21f80-f87b-11e6-8123-f6da70f2493b.png)

After:

![screenshot11](https://cloud.githubusercontent.com/assets/408368/23182971/b4022ed8-f87a-11e6-9d3b-de1a9d4ce9aa.png)

Commits
-------

b663ab5 [Bridge/Monolog] Enhanced the Console Handler
@lyrixx lyrixx deleted the log-command-refacto branch March 6, 2017 17:12
lyrixx added a commit to lyrixx/symfony-standard that referenced this pull request Mar 7, 2017
Related to symfony/symfony#21705

Note: It's not an issue to let monolog doing it, but if monolog do it
symfony can not do it. The best experience is when symfony do it.

So let's disable it.
fabpot added a commit to symfony/symfony-standard that referenced this pull request Mar 9, 2017
…symfony. (lyrixx)

This PR was merged into the 3.3-dev branch.

Discussion
----------

Do not process psr_3 log messages, as it's now done by symfony.

Related to symfony/symfony#21705

Note: It's not an issue to let monolog doing it, but if monolog do it
symfony can not do it. The best experience is when symfony do it.

So let's disable it.

Commits
-------

ebe2bad Do not process psr_3 log messages, as it's now done by symfony.
@mvrhov
Copy link

mvrhov commented Mar 9, 2017

This seems to be a memory hog. At least I was greeted with the php terminating script due to memory exhaustion (128M) instead of getting the stack dump. I have run the command with time to see the top memory consumption and it was around 150M-160M. Running same command with 3.2, the memory used is around 50M

@lyrixx
Copy link
Member Author

lyrixx commented Mar 9, 2017

@mvrhov Thanks for reporting this, but could you open a new issue and ping me to keep a trace of this issue?

@Tobion
Copy link
Contributor

Tobion commented May 20, 2017

Does this PR basically revert #11496? I don't see why.

nicolas-grekas added a commit that referenced this pull request Sep 20, 2018
…xt and extra data (mpdude)

This PR was squashed before being merged into the 3.4 branch (closes #28471).

Discussion
----------

[MonologBridge] Re-add option option to ignore empty context and extra data

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

In #11496, an option was added to `ConsoleFormatter` to ignore empty context and extra data. This setting was even turned on by default.

The `ConsoleHandler` was then overhauled in #21705. During this change, the option got lost.

Commits
-------

d1e7438 [MonologBridge] Re-add option option to ignore empty context and extra data
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.