Clément Andraud
Clément Andraud

Reputation: 9269

Symfony2, log exception in specific file

I'm working on an API with Symfony and FosRestBundle.

In my ApiController, I want to log all exception in a specific file: api.log

Here an example of my code:

   public function getUserAction($param){
        if(is_int($param)){
            $user = $this->getDoctrine()->getRepository('ProjectBundle:Account')->findOneById($param);
        }
        else{
            $user = $this->getDoctrine()->getRepository('ProjectBundle:Account')->findOneByUsername($param);
        }

        if(!is_object($user)){
            return "User Not Found";
        }

        return $user;
    }

Where i need to catch and log the exeption ? In if(!is_object($user)){ or when i retrieve $user ? And How can i save and log the exeption in my file api.log ?

Thanks !

Upvotes: 0

Views: 120

Answers (1)

Nextar
Nextar

Reputation: 1098

In my opinion should add a kernel.exception event listener, which writes the exception in the api.log file

http://symfony.com/doc/current/cookbook/service_container/event_listener.html

Upvotes: 1

Related Questions