Using Symfony 1.1's Error Levels Properly

Here’s how to log a message in your action (using the INFO=6 level):

$this->logMessage(‘account_id: ‘.$account->getAccountId(),‘INFO’);

And elsewhere (using the CRIT=2 level):

sfContext::getInstance()-&gt;getLogger()-&gt;log(&#8216;rsync returned error code (&#8217;.$return.&#8217;)&#8217;, &#8216;<span class="caps">CRIT</span>&#8217;);
sfContext::getInstance()-&gt;getLogger()-&gt;log(debug($output,0,1),&#8216;<span class="caps">CRIT</span>&#8217;); // for Arrays
sfContext::getInstance()-&gt;getLogger()-&gt;warning(&#8216;we should look at why this is happening&#8217;); // using the <span class="caps">WARNING</span>=4 level

Here’s the error levels that symfony gives us, ERR=3 being the default:

const EMERG   = 0; // System is unusable
const ALERT   = 1; // Immediate action required
const CRIT    = 2; // Critical conditions
const ERR     = 3; // Error conditions
const WARNING = 4; // Warning conditions
const NOTICE  = 5; // Normal but significant
const INFO    = 6; // Informational
const DEBUG   = 7; // Debug-level messages

I wrote a new logException method for us to have a standard way to log and deal with Exceptions. This should hopefully end up being a standard way to deal with Exceptions across the project. The dUtils::logException() method sets the message at the ERR=3 level by default. You can change that with the second argument.

class dUtils{
  public static function exceptionToString($e){
    return $e->getFile().” on line no. “.$e->getLine().” “.$e->getMessage().“nn”.$e->getTraceAsString();
  }
  public static function logException($e,$error=sfLogger::ERR){
    sfContext::getInstance()->getLogger()->log(dUtils::exceptionToString($e),$error);
  }
}

try {
  $sf_secure_user->save();
} catch (PropelException $e) { // problem with save dUtils::logException($e); $this->getUser()->setFlash(‘error’, ‘Account not created. Please try again.’); return sfView::SUCCESS;
}

And here’s how to set the logging level in factories.yml. Please be aware that the default level for all logs is set at the lowest level, DEBUG=7. On production, the level is set at ERR=3. This means that in the log file, we’ll see those errors above level DEBUG=7 or ERR=3, respectively.

  logger:
    class: sfAggregateLogger
    param:
      level: debug
      loggers:
        sf_web_debug:
          class: sfWebDebugLogger
          param:
            level: debug
            condition:      SF_WEB_DEBUG
            xdebug_logging: true
        sf_file_debug:
          class: sfFileLogger
          param:
            level: debug
            file: SF_LOG_DIR/%SF_APP%_%SF_ENVIRONMENT%.log