10000 Symfony autowiring monolog channels · Issue #278 · symfony/monolog-bundle · GitHub
[go: up one dir, main page]

Skip to content

Symfony autowiring monolog channels #278

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

Closed
MaksSlesarenko opened this issue Aug 27, 2018 · 6 comments · Fixed by #315
Closed

Symfony autowiring monolog channels #278

MaksSlesarenko opened this issue Aug 27, 2018 · 6 comments · Fixed by #315
Labels
Milestone

Comments

@MaksSlesarenko
Copy link

Hi everyone,
While looking for a way to autowire monolog channels, and I got stumbled upon StackOverflow thread where guy named "DrafFter" suggests adding LoggerFactory.
https://stackoverflow.com/questions/43788114/symfony-autowiring-monolog-channels

Maybe this can be implemented within this bundle? Or is there a better way to do autowiring?

@nicolas-grekas
Copy link
Member

In Symfony 4.2, symfony/symfony#28234 will allow this. (will require a PR here to use registerAliasForArgument())

@Seldaek Seldaek added this to the 3.4 milestone Dec 29, 2018
@adrenalinkin
Copy link
Contributor
adrenalinkin commented Apr 21, 2019

@MaksSlesarenko @nicolas-grekas @Seldaek Hi there!
Recently I was implement single point access to the all registered loggers by MonologBundle.
And also I tried to do some better solution - and did auto-generated logger decorators. Each class decorates one object of one of the registered monolog channel.

P.S.: Link to the bundle https://github.com/adrenalinkin/monolog-autowire-bundle

@lyrixx
Copy link
Member
lyrixx commented May 18, 2019

Do someone want to submit a PR since Symfony 4.2 is released?

@adrenalinkin
Copy link
Contributor

@lyrixx Hello Grégoire!
I am interested and will be proud submit Pull Request. But first of all, please, can you review my implementation - monolog-autowire-bundle.
Is that conception can be applied by Symfony?

@weaverryan
Copy link
Member

Hey @adrenalinkin!

Your implementation is great, but in 4.2, this is now much easier - should be basically one line of new code - see https://github.com/symfony/symfony/blob/4.4/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php#L684 for an example

This would be awesome to have :)

@adrenalinkin
Copy link
Contributor

Hello, @weaverryan!

Thanks you for your opinion, nice to hear that.

Correct me if I miss-understand you. Instead of creation different logger decorator classes we will focuses on the parameter names.
Now time for example:

  • Some channels:
monolog:
    handlers:
        doctrine:
            type:   stream
            path:   "%kernel.logs_dir%/%kernel.environment%.doctrine.log"
            level:  info
            channels:
                - "doctrine"

        acme:
            type:   stream
            path:   "%kernel.logs_dir%/%kernel.environment%.acme_channel.log"
            level:  info
            channels:
                - "acme_channel"
  • And some specific naming for the parameters to auto-wire these channels:
<?php declare(strict_types=1);

use Psr\Log\LoggerInterface;

class AcmeLoggerAware
{
    /**
     * @var LoggerInterface
     */
    private $acmeLogLogger;

    /**
     * @var LoggerInterface
     */
    private $doctrineLogger;

    public function __construct(LoggerInterface $monologChannelDoctrine, LoggerInterface $monologChannelAcme)
    {
        $this->acmeLogLogger = $monologChannelAcme;
        $this->doctrineLogger = $monologChannelDoctrine;
    }
}

First Question

Am I right understand main idea?


And one drawback of the implementation described above, in my opinion, - we don't have clear auto-complete in the modern IDE. All programmers should keep in mind specific naming rule.

On other side we can generate different logger decorator classes and then we will have useful and clear auto-complete. But drawback, I think, - more difficult realization with code generation.

<?php declare(strict_types=1);

use Linkin\Bundle\MonologAutowireBundle\Logger\ChannelAcmeLogLogger;
use Linkin\Bundle\MonologAutowireBundle\Logger\ChannelDoctrineLogger;
use Psr\Log\LoggerInterface;

class AcmeLoggerAware
{
    /**
     * @var ChannelDoctrineLogger
     */
    private $acmeLogLogger;

    /**
     * @var ChannelDoctrineLogger
     */
    private $doctrineLogger;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @param ChannelAcmeLogLogger $acmeLogLogger
     * @param ChannelDoctrineLogger $doctrineLogger
     * @param LoggerInterface $logger
     */
    public function __construct(
        ChannelAcmeLogLogger $acmeLogLogger,
        ChannelDoctrineLogger $doctrineLogger,
        LoggerInterface $logger
    ) {
        $this->acmeLogLogger = $acmeLogLogger;
        $this->doctrineLogger = $doctrineLogger;
        $this->logger = $logger;
    }
}

Second Question

What do you think about drawbacks of each implementations and what implementation should we apply?

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

Successfully merging a pull request may close this issue.

6 participants
0