0
votes

I have the following class:

namespace AppBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;


class UserRegistrationListener implements EventSubscriberInterface
{

     protected $logger;
     public function __construct($logger)
     {
           $this->logger = $logger;
     }

    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_INITIALIZE => 'onRegistrationInit',
        );
    }

    /**
     * take action when registration is initialized
     * set the username to a unique id
     * @param \FOS\UserBundle\Event\FormEvent $event
     */
    public function onRegistrationInit(UserEvent $userevent)
    {
        $this->logger->info("Log Something");

        $user = $userevent->getUser();

        $user->setUsername(uniqid());
    }
}

and I have been trying for hours to log something with monolog from within it but have had no luck.

I have read much of the documentation and I believe I need to somehow 'Inject' monolog as a service. What I have read however does not seem to be clear to me.

Some details:

#config_dev.yml
monolog:
    channels: [chris]
    handlers:
        mylog:
            type:   stream
            path:   "%kernel.logs_dir%/%kernel.environment%_chris.log"
            channels: chris
            formatter: monolog.my_line_formatter

.

#services.yml   
 services:
        monolog.my_line_formatter: 
            class: Monolog\Formatter\LineFormatter
            arguments: [~, ~, true]

        app.user_registration:
            class: AppBundle\EventListener\UserRegistrationListener
            arguments: [@logger] ## changed to [@monolog.logger.chris] to us custom channel
            tags:
                - { name: kernel.event_subscriber }

What do I have to do to get Monolog working with my formatter inside this class?

UPDATE: @griotteau I have done what you have posted in your answer but I still get an error:

CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Warning: Missing argument 1 for AppBundle\EventListener\UserRegistrationListener::__construct(), called in ...filepath...\app\cache\dev\appDevDebugProjectContainer.php on line 384 and defined" at ...filepath...\src\AppBundle\EventListener\UserRegistrationListener.php line 18

SOLVED ERROR I already had a service with the same class (not shown in ym question). @griotteau 's answer is correct.

1
Why did you create a custom channel for this? Is the log working on the standard Monolog channel? So far, I can't see any log invocation in your event class...Yann Eugoné
Post code in which you use Monolog to log what you want.Michael Sivolobov
@Michael Sivolobov I have added this to the question.Chris
@Yann Eugoné I created custom channel to log to a different file. I did this because I want to output formatted arrays to a log while debugging and tail -f the log file. trying to grep formatted arrays out from the standard log file was proving quite difficult.Chris

1 Answers

6
votes

You can pass arguments when you declare your service

In services.yml :

 app.user_registration:
    class: AppBundle\EventListener\UserRegistrationListener
    arguments: [@logger]
    tags:
        - { name: kernel.event_subscriber }  

In your class, add a constructor :

protected $logger;
public function __construct($logger)
{
        $this->logger = $logger;        
}

So when you want to add a log :

$this->logger->info(...);