SUBSCRIBE VIA RSS


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.

Topics

Stack Overflow


The old fashioned way

RECENT TUNES

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.

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

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:

    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

  3. Steve B says:

    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

  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.