Symfony mongodb logger

January 9th, 2010 by Jayson Leave a reply »

Thanks for the elegant design of symfony framework, we can easily create a new logger by extending sfLogger.

/**
 * opMongoLogger logs messages in a mongo database.
 *
 * @author     Jayson Xu(http://www.doupie.com) 
*/
class opMongoLogger extends sfLogger
{
  protected
    $timeFormat = '%b %d %H:%M:%S';
  /**
   * Initializes this logger.
   *
   * Available options:
   *
   * - database:      a database name to store the cache in
   * - host:         the host of database
   * - port:         the host of port
   * - collection:   the host of collection
   *
   * @param  sfEventDispatcher $dispatcher  A sfEventDispatcher instance
   * @param  array             $options     An array of options.
   *
   * @return Boolean      true, if initialization completes successfully, otherwise false.
   */
  public function initialize(sfEventDispatcher $dispatcher, $options = array())
  {

    if (!class_exists('Mongo'))
    {
      throw new sfInitializationException(sprintf('the mongodb extension is not installed or enable, cannot use opMongoLogger.'));
    }
    parent::initialize($dispatcher, $options);

    if (!$this->hasOption('database'))
    {
      throw new sfInitializationException(sprintf('You must provide a database name to store the cache in'));
    }
    $this->type = $this->getOption('type','m');
    $this->database = $this->getOption('database');
    $this->host = $this->getOption('host','127.0.0.1');
    $this->port = $this->getOption('port','27017');
    $this->collection = $this->getOption('collection','logs');
    try
    {
      $this->handler = new mongo($this->host.":".$this->port);
      $this->coll_db = $this->handler->selectDB($this->database)->selectCollection($this->collection);
    }
    catch (Exception $e)
    {
      throw new sfInitializationException(sprintf('Could not connect to mongo database : ', $e->getMessage()));
    }

    if (isset($options['time_format']))
    {
      $this->timeFormat = $options['time_format'];
    }
    return true;
  }

  /**
   * Logs a message.
   *
   * @param string $message   Message
   * @param string $priority  Message priority
   */
  protected function doLog($message, $priority)
  {
    $log = array(
      'type'      => $this->type,
      'message'   => $message,
      'time'      => strftime($this->timeFormat),
      'priority'  => $this->getPriority($priority),
      );
    if(sfContext::getInstance())
    {
      $log['module'] = sfContext::getInstance()->getRequest()->getParameter('module');
      $log['action'] = sfContext::getInstance()->getRequest()->getParameter('action');
    }
    $this->coll_db->insert($log);
  }

  /**
   * Returns the priority string to use in log messages.
   *
   * @param  string $priority The priority constant
   *
   * @return string The priority to use in log messages
   */
  protected function getPriority($priority)
  {
    return sfLogger::getPriorityName($priority);
  }

  /**
   * Executes the shutdown method.
   */
  public function shutdown()
  {
    $this->handler->close();
  }

  public function hasOption($option)
  {
    return array_key_exists($option, $this->options);
  }

    /**
   * Gets an option value.
   *
   * @param string $name    The option name
   * @param mixed  $default The default value
   *
   * @return mixed The option value
   */
  public function getOption($name, $default = null)
  {
    return isset($this->options[$name]) ? $this->options[$name] : $default;
  }
}

in opMongoLogger, if the logger is called from action, it will log the module and action too.

Advertisement

2 comments

  1. Thanks for the info… i’ll put it to good use :)

Trackbacks /
Pingbacks

  1. » Tus logs de symfony en Twitter

Leave a Reply

You must be logged in to post a comment.