FileLog.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * File Storage stream for Logging
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  16. * @package Cake.Log.Engine
  17. * @since CakePHP(tm) v 1.3
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('BaseLog', 'Log/Engine');
  21. App::uses('Hash', 'Utility');
  22. App::uses('CakeNumber', 'Utility');
  23. /**
  24. * File Storage stream for Logging. Writes logs to different files
  25. * based on the type of log it is.
  26. *
  27. * @package Cake.Log.Engine
  28. */
  29. class FileLog extends BaseLog {
  30. /**
  31. * Default configuration values
  32. *
  33. * @var array
  34. * @see FileLog::__construct()
  35. */
  36. protected $_defaults = array(
  37. 'path' => LOGS,
  38. 'file' => null,
  39. 'types' => null,
  40. 'scopes' => array(),
  41. 'rotate' => 10,
  42. 'size' => 10485760, // 10MB
  43. 'mask' => null,
  44. );
  45. /**
  46. * Path to save log files on.
  47. *
  48. * @var string
  49. */
  50. protected $_path = null;
  51. /**
  52. * Log file name
  53. *
  54. * @var string
  55. */
  56. protected $_file = null;
  57. /**
  58. * Max file size, used for log file rotation.
  59. *
  60. * @var integer
  61. */
  62. protected $_size = null;
  63. /**
  64. * Constructs a new File Logger.
  65. *
  66. * Config
  67. *
  68. * - `types` string or array, levels the engine is interested in
  69. * - `scopes` string or array, scopes the engine is interested in
  70. * - `file` Log file name
  71. * - `path` The path to save logs on.
  72. * - `size` Used to implement basic log file rotation. If log file size
  73. * reaches specified size the existing file is renamed by appending timestamp
  74. * to filename and new log file is created. Can be integer bytes value or
  75. * human reabable string values like '10MB', '100KB' etc.
  76. * - `rotate` Log files are rotated specified times before being removed.
  77. * If value is 0, old versions are removed rather then rotated.
  78. * - `mask` A mask is applied when log files are created. Left empty no chmod
  79. * is made.
  80. *
  81. * @param array $options Options for the FileLog, see above.
  82. */
  83. public function __construct($config = array()) {
  84. $config = Hash::merge($this->_defaults, $config);
  85. parent::__construct($config);
  86. }
  87. /**
  88. * Sets protected properties based on config provided
  89. *
  90. * @param array $config Engine configuration
  91. * @return array
  92. */
  93. public function config($config = array()) {
  94. parent::config($config);
  95. if (!empty($config['path'])) {
  96. $this->_path = $config['path'];
  97. }
  98. if (Configure::read('debug') && !is_dir($this->_path)) {
  99. mkdir($this->_path, 0775, true);
  100. }
  101. if (!empty($config['file'])) {
  102. $this->_file = $config['file'];
  103. if (substr($this->_file, -4) !== '.log') {
  104. $this->_file .= '.log';
  105. }
  106. }
  107. if (!empty($config['size'])) {
  108. if (is_numeric($config['size'])) {
  109. $this->_size = (int)$config['size'];
  110. } else {
  111. $this->_size = CakeNumber::fromReadableSize($config['size']);
  112. }
  113. }
  114. return $this->_config;
  115. }
  116. /**
  117. * Implements writing to log files.
  118. *
  119. * @param string $type The type of log you are making.
  120. * @param string $message The message you want to log.
  121. * @return boolean success of write.
  122. */
  123. public function write($type, $message) {
  124. $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
  125. $filename = $this->_getFilename($type);
  126. if (!empty($this->_size)) {
  127. $this->_rotateFile($filename);
  128. }
  129. $pathname = $this->_path . $filename;
  130. if (empty($this->_config['mask'])) {
  131. return file_put_contents($pathname, $output, FILE_APPEND);
  132. }
  133. $exists = file_exists($pathname);
  134. $result = file_put_contents($pathname, $output, FILE_APPEND);
  135. static $selfError = false;
  136. if (!$selfError && !$exists && !chmod($pathname, (int)$this->_config['mask'])) {
  137. $selfError = true;
  138. trigger_error(__d(
  139. 'cake_dev', 'Could not apply permission mask "%s" on log file "%s"',
  140. array($this->_config['mask'], $pathname)), E_USER_WARNING);
  141. $selfError = false;
  142. }
  143. return $result;
  144. }
  145. /**
  146. * Get filename
  147. * @param string $type The type of log.
  148. * @return string File name
  149. */
  150. protected function _getFilename($type) {
  151. $debugTypes = array('notice', 'info', 'debug');
  152. if (!empty($this->_file)) {
  153. $filename = $this->_file;
  154. } elseif ($type == 'error' || $type == 'warning') {
  155. $filename = 'error.log';
  156. } elseif (in_array($type, $debugTypes)) {
  157. $filename = 'debug.log';
  158. } else {
  159. $filename = $type . '.log';
  160. }
  161. return $filename;
  162. }
  163. /**
  164. * Rotate log file if size specified in config is reached.
  165. * Also if `rotate` count is reached oldest file is removed.
  166. *
  167. * @param string $filename Log file name
  168. * @return mixed True if rotated successfully or false in case of error.
  169. * Void if file doesn't need to be rotated.
  170. */
  171. protected function _rotateFile($filename) {
  172. $filepath = $this->_path . $filename;
  173. if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
  174. clearstatcache(true, $filepath);
  175. } else {
  176. clearstatcache();
  177. }
  178. if (!file_exists($filepath) ||
  179. filesize($filepath) < $this->_size
  180. ) {
  181. return;
  182. }
  183. if ($this->_config['rotate'] === 0) {
  184. $result = unlink($filepath);
  185. } else {
  186. $result = rename($filepath, $filepath . '.' . time());
  187. }
  188. $files = glob($filepath . '.*');
  189. if ($files) {
  190. $filesToDelete = count($files) - $this->_config['rotate'];
  191. while ($filesToDelete > 0) {
  192. unlink(array_shift($files));
  193. $filesToDelete--;
  194. }
  195. }
  196. return $result;
  197. }
  198. }