Subscribe to our feed

Symfony Experts

Symfony Experts
If you have an urgent question for a symfony-related issue, this is the place to ask.


Stack Overflow

The old fashioned way


June 12, 2007 – 4:12pm Symfony exceptions, logging, and debugging

Since I always forget, here is a quick reference for throwing exceptions and adding log/debug messages throughout your symfony project.

This is how you can throw a new symfony exception.

throw new sfException('An error occurred. Please go back and try again.');


Within an action, this is how you can log a message:

<?php use_helper('Debug') ?>
<?php if ($problem): ?>
  <?php echo log_message('{mainActions} been there', 'err') ?>
<?php endif ?>

And elsewhere within the code (model, etc):



// in an action
// in a template
<?php echo debug_message($message) ?>

Reference: Symfony Docs: Debugging

Posted by in  Web Development   |  

4 Responses to Symfony exceptions, logging, and debugging

  1. herve says:

    Hej, I am wondering:
    how could certain actions be logged in the db when executed? I d like, for instance, the save() actions of certain modules to be logged in the db to create an history table of what happens on the site.

    Except creating an history table and saving the informations I am interested in in every actions, The info logs don’t seem to be reusable. Could a propel behavior do that?

    Cool reading you,
    mvh, hervé

  2. Scott Meves says:


    If you want to log actions in the db when executed, I wouldn’t even use the sfLogger class and instead just add custom code to my objects save() methods.

    A propel behavior would be perfect for this kind of thing!!! I would take a look at some of the existing plugins that provide propel behaviors and then just dive in and write your own. It’d make a great plugin 😉

  3. Steve B says:

    Thanks for the very helpful but simple post. With regards to logging messages in the database, I’m pretty sure you’d be able to extend the sfLogger class or something to do this. Not sure how at the moment, but I understand symfony enough to guess this would be achievable!

  4. When it comes to exceptions, the best practice is usually to extend the symfony exception class and customise to your needs – allowing for better exception handling.