EMongoLogRoute.php 995 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * EMongoLogRoute extends CLogRoute and provides logging
  4. * into MongoDB.
  5. * It is the mongodb equivalent of CDbLogRoute
  6. */
  7. class EMongoLogRoute extends CLogRoute
  8. {
  9. /**
  10. * @var string the connectionId of the EMongoClient component
  11. */
  12. public $connectionId = 'mongodb';
  13. /**
  14. * Name of the collection the logs should be stored to.
  15. * @var string
  16. */
  17. public $logCollectionName = 'YiiLog';
  18. /**
  19. * Get a MongoCollection object
  20. * @return MongoCollection - Instance of MongoCollection
  21. */
  22. public function getMongoConnection()
  23. {
  24. return Yii::app()->{$this->connectionId}->{$this->logCollectionName};
  25. }
  26. /**
  27. * Stores log messages into database.
  28. * @param array $logs list of log messages
  29. */
  30. public function processLogs($logs)
  31. {
  32. $collection = $this->getMongoConnection();
  33. foreach($logs as $log){
  34. $collection->insert(
  35. array(
  36. 'level' => $log[1],
  37. 'category' => $log[2],
  38. 'logtime' => (int)$log[3],
  39. 'message' => $log[0],
  40. )
  41. );
  42. }
  43. }
  44. }