Since I always forget, here is a quick reference for throwing exceptions and adding log/debug messages throughout your symfony project.
Exceptions
This is how you can throw a new symfony exception.
throw new sfException('An error occurred. Please go back and try again.');
Logging
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):
sfContext::getInstance()->getLogger()->info($message); sfContext::getInstance()->getLogger()->err($message);
Debugging
// in an action $this->debugMessage($message); // in a template <?php echo debug_message($message) ?>
Reference: Symfony Docs: Debugging




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é
By herve on Sep 15, 2008
Hervé,
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
-Scott
By Scott Meves on Sep 15, 2008
Hi,
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!
Steve
By Steve B on Jan 5, 2009
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.
By Web Development on Aug 23, 2010