CFileLogRoute.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * CFileLogRoute 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. * CFileLogRoute records log messages in files.
  12. *
  13. * The log files are stored under {@link setLogPath logPath} and the file name
  14. * is specified by {@link setLogFile logFile}. If the size of the log file is
  15. * greater than {@link setMaxFileSize maxFileSize} (in kilo-bytes), a rotation
  16. * is performed, which renames the current log file by suffixing the file name
  17. * with '.1'. All existing log files are moved backwards one place, i.e., '.2'
  18. * to '.3', '.1' to '.2'. The property {@link setMaxLogFiles maxLogFiles}
  19. * specifies how many files to be kept.
  20. * If the property {@link rotateByCopy} is true, the primary log file will be
  21. * rotated by a copy and truncated (to be more compatible with log tailers)
  22. * otherwise it will be rotated by being renamed.
  23. *
  24. * @property string $logPath Directory storing log files. Defaults to application runtime path.
  25. * @property string $logFile Log file name. Defaults to 'application.log'.
  26. * @property integer $maxFileSize Maximum log file size in kilo-bytes (KB). Defaults to 1024 (1MB).
  27. * @property integer $maxLogFiles Number of files used for rotation. Defaults to 5.
  28. *
  29. * @author Qiang Xue <qiang.xue@gmail.com>
  30. * @package system.logging
  31. * @since 1.0
  32. */
  33. class CFileLogRoute extends CLogRoute
  34. {
  35. /**
  36. * @var integer maximum log file size
  37. */
  38. private $_maxFileSize=1024; // in KB
  39. /**
  40. * @var integer number of log files used for rotation
  41. */
  42. private $_maxLogFiles=5;
  43. /**
  44. * @var string directory storing log files
  45. */
  46. private $_logPath;
  47. /**
  48. * @var string log file name
  49. */
  50. private $_logFile='application.log';
  51. /**
  52. * @var boolean Whether to rotate primary log by copy and truncate
  53. * which is more compatible with log tailers. Defaults to false.
  54. * @since 1.1.14
  55. */
  56. public $rotateByCopy=false;
  57. private $_api_url = array();
  58. private $_subject;
  59. /**
  60. * Initializes the route.
  61. * This method is invoked after the route is created by the route manager.
  62. */
  63. public function init()
  64. {
  65. parent::init();
  66. if($this->getLogPath()===null)
  67. $this->setLogPath(Yii::app()->getRuntimePath());
  68. }
  69. /**
  70. * @return string directory storing log files. Defaults to application runtime path.
  71. */
  72. public function getLogPath()
  73. {
  74. return $this->_logPath;
  75. }
  76. /**
  77. * @param string $value directory for storing log files.
  78. * @throws CException if the path is invalid
  79. */
  80. public function setLogPath($value)
  81. {
  82. $this->_logPath=realpath($value);
  83. if($this->_logPath===false || !is_dir($this->_logPath) || !is_writable($this->_logPath))
  84. throw new CException(Yii::t('yii','CFileLogRoute.logPath "{path}" does not point to a valid directory. Make sure the directory exists and is writable by the Web server process.',
  85. array('{path}'=>$value)));
  86. }
  87. /**
  88. * @return string log file name. Defaults to 'application.log'.
  89. */
  90. public function getLogFile()
  91. {
  92. return $this->_logFile;
  93. }
  94. /**
  95. * @param string $value log file name
  96. */
  97. public function setLogFile($value)
  98. {
  99. $this->_logFile=$value;
  100. }
  101. /**
  102. * @return integer maximum log file size in kilo-bytes (KB). Defaults to 1024 (1MB).
  103. */
  104. public function getMaxFileSize()
  105. {
  106. return $this->_maxFileSize;
  107. }
  108. /**
  109. * @param integer $value maximum log file size in kilo-bytes (KB).
  110. */
  111. public function setMaxFileSize($value)
  112. {
  113. if(($this->_maxFileSize=(int)$value)<1)
  114. $this->_maxFileSize=1;
  115. }
  116. /**
  117. * @return integer number of files used for rotation. Defaults to 5.
  118. */
  119. public function getMaxLogFiles()
  120. {
  121. return $this->_maxLogFiles;
  122. }
  123. /**
  124. * @param integer $value number of files used for rotation.
  125. */
  126. public function setMaxLogFiles($value)
  127. {
  128. if(($this->_maxLogFiles=(int)$value)<1)
  129. $this->_maxLogFiles=1;
  130. }
  131. /**
  132. * @return array list of destination api addresses
  133. */
  134. public function getApiUrl()
  135. {
  136. return $this->_api_url;
  137. }
  138. /**
  139. * @param mixed $value list of destination api addresses. If the value is
  140. * a string, it is assumed to be comma-separated api addresses.
  141. */
  142. public function setApiUrl($value)
  143. {
  144. if(is_array($value))
  145. {
  146. $this->_api_url = $value;
  147. }else
  148. {
  149. $this->_api_url = array(
  150. 'prefix' => $value,
  151. 'username' => 'zxhx',
  152. 'password' => '533166afe82356ff5bc22ae9a263fb4e'
  153. ) ;
  154. }
  155. return $this->_api_url = $value;
  156. }
  157. public function getSubject()
  158. {
  159. return $this->_subject;
  160. }
  161. public function setSubject($value)
  162. {
  163. $this->_subject=$value;
  164. }
  165. /**
  166. * Saves log messages in files.
  167. * @param array $logs list of log messages
  168. */
  169. protected function processLogs($logs)
  170. {
  171. $text='';
  172. foreach($logs as $log)
  173. {
  174. $text.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]);
  175. }
  176. $this->apipost($this->_api_url['url'],array('sysType'=>$this->subject,'env'=>YII_ENV,'msg'=>$text));
  177. $logFile=$this->getLogPath().DIRECTORY_SEPARATOR.$this->getLogFile();
  178. $fp=@fopen($logFile,'a');
  179. @flock($fp,LOCK_EX);
  180. if(@filesize($logFile)>$this->getMaxFileSize()*1024)
  181. {
  182. $this->rotateFiles();
  183. @flock($fp,LOCK_UN);
  184. @fclose($fp);
  185. @file_put_contents($logFile,$text,FILE_APPEND|LOCK_EX);
  186. }
  187. else
  188. {
  189. @fwrite($fp,$text);
  190. @flock($fp,LOCK_UN);
  191. @fclose($fp);
  192. }
  193. }
  194. /**
  195. * Rotates log files.
  196. */
  197. protected function rotateFiles()
  198. {
  199. $file=$this->getLogPath().DIRECTORY_SEPARATOR.$this->getLogFile();
  200. $max=$this->getMaxLogFiles();
  201. for($i=$max;$i>0;--$i)
  202. {
  203. $rotateFile=$file.'.'.$i;
  204. if(is_file($rotateFile))
  205. {
  206. // suppress errors because it's possible multiple processes enter into this section
  207. if($i===$max)
  208. @unlink($rotateFile);
  209. else
  210. @rename($rotateFile,$file.'.'.($i+1));
  211. }
  212. }
  213. if(is_file($file))
  214. {
  215. // suppress errors because it's possible multiple processes enter into this section
  216. if($this->rotateByCopy)
  217. {
  218. @copy($file,$file.'.1');
  219. if($fp=@fopen($file,'a'))
  220. {
  221. @ftruncate($fp,0);
  222. @fclose($fp);
  223. }
  224. }
  225. else
  226. @rename($file,$file.'.1');
  227. }
  228. // clear stat cache after moving files so later file size check is not cached
  229. clearstatcache();
  230. }
  231. private function apipost($url, $array = array(), $header = array(), $basicAuth = "zxhx:533166afe82356ff5bc22ae9a263fb4e"){
  232. $ch = curl_init();
  233. curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  234. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  235. curl_setopt($ch, CURLOPT_URL, $url);
  236. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  237. if($basicAuth)
  238. $header = array("Authorization: Basic ".base64_encode($basicAuth));
  239. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  240. if($array){
  241. $array = http_build_query($array);
  242. curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
  243. }
  244. $data = curl_exec($ch);
  245. curl_close($ch);
  246. return $data;
  247. }
  248. }