CDbLogRoute.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * CDbLogRoute class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CDbLogRoute stores log messages in a database table.
  12. *
  13. * To specify the database table for storing log messages, set {@link logTableName} as
  14. * the name of the table and specify {@link connectionID} to be the ID of a {@link CDbConnection}
  15. * application component. If they are not set, a SQLite3 database named 'log-YiiVersion.db' will be created
  16. * and used under the application runtime directory.
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @package system.logging
  20. * @since 1.0
  21. */
  22. class CDbLogRoute extends CLogRoute
  23. {
  24. /**
  25. * @var string the ID of CDbConnection application component. If not set, a SQLite database
  26. * will be automatically created and used. The SQLite database file is
  27. * <code>protected/runtime/log-YiiVersion.db</code>.
  28. */
  29. public $connectionID;
  30. /**
  31. * @var string the name of the DB table that stores log content. Defaults to 'YiiLog'.
  32. * If {@link autoCreateLogTable} is false and you want to create the DB table manually by yourself,
  33. * you need to make sure the DB table is of the following structure:
  34. * <pre>
  35. * (
  36. * id INTEGER NOT NULL PRIMARY KEY,
  37. * level VARCHAR(128),
  38. * category VARCHAR(128),
  39. * logtime INTEGER,
  40. * message TEXT
  41. * )
  42. * </pre>
  43. * Note, the 'id' column must be created as an auto-incremental column.
  44. * In MySQL, this means it should be <code>id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY</code>;
  45. * In PostgreSQL, it is <code>id SERIAL PRIMARY KEY</code>.
  46. * @see autoCreateLogTable
  47. */
  48. public $logTableName='YiiLog';
  49. /**
  50. * @var boolean whether the log DB table should be automatically created if not exists. Defaults to true.
  51. * @see logTableName
  52. */
  53. public $autoCreateLogTable=true;
  54. /**
  55. * @var CDbConnection the DB connection instance
  56. */
  57. private $_db;
  58. /**
  59. * Initializes the route.
  60. * This method is invoked after the route is created by the route manager.
  61. */
  62. public function init()
  63. {
  64. parent::init();
  65. if($this->autoCreateLogTable)
  66. {
  67. $db=$this->getDbConnection();
  68. try
  69. {
  70. $db->createCommand()->delete($this->logTableName,'0=1');
  71. }
  72. catch(Exception $e)
  73. {
  74. $this->createLogTable($db,$this->logTableName);
  75. }
  76. }
  77. }
  78. /**
  79. * Creates the DB table for storing log messages.
  80. * @param CDbConnection $db the database connection
  81. * @param string $tableName the name of the table to be created
  82. */
  83. protected function createLogTable($db,$tableName)
  84. {
  85. $db->createCommand()->createTable($tableName, array(
  86. 'id'=>'pk',
  87. 'level'=>'varchar(128)',
  88. 'category'=>'varchar(128)',
  89. 'logtime'=>'integer',
  90. 'message'=>'text',
  91. ));
  92. }
  93. /**
  94. * @return CDbConnection the DB connection instance
  95. * @throws CException if {@link connectionID} does not point to a valid application component.
  96. */
  97. protected function getDbConnection()
  98. {
  99. if($this->_db!==null)
  100. return $this->_db;
  101. elseif(($id=$this->connectionID)!==null)
  102. {
  103. if(($this->_db=Yii::app()->getComponent($id)) instanceof CDbConnection)
  104. return $this->_db;
  105. else
  106. throw new CException(Yii::t('yii','CDbLogRoute.connectionID "{id}" does not point to a valid CDbConnection application component.',
  107. array('{id}'=>$id)));
  108. }
  109. else
  110. {
  111. $dbFile=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'log-'.Yii::getVersion().'.db';
  112. return $this->_db=new CDbConnection('sqlite:'.$dbFile);
  113. }
  114. }
  115. /**
  116. * Stores log messages into database.
  117. * @param array $logs list of log messages
  118. */
  119. protected function processLogs($logs)
  120. {
  121. $command=$this->getDbConnection()->createCommand();
  122. foreach($logs as $log)
  123. {
  124. $command->insert($this->logTableName,array(
  125. 'level'=>$log[1],
  126. 'category'=>$log[2],
  127. 'logtime'=>(int)$log[3],
  128. 'message'=>$log[0],
  129. ));
  130. }
  131. }
  132. }