FileLog.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  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 (!empty($config['file'])) {
  99. $this->_file = $config['file'];
  100. if (substr($this->_file, -4) !== '.log') {
  101. $this->_file .= '.log';
  102. }
  103. }
  104. if (!empty($config['size'])) {
  105. if (is_numeric($config['size'])) {
  106. $this->_size = (int)$config['size'];
  107. } else {
  108. $this->_size = CakeNumber::fromReadableSize($config['size']);
  109. }
  110. }
  111. return $this->_config;
  112. }
  113. /**
  114. * Implements writing to log files.
  115. *
  116. * @param string $type The type of log you are making.
  117. * @param string $message The message you want to log.
  118. * @return boolean success of write.
  119. */
  120. public function write($type, $message) {
  121. $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n";
  122. $filename = $this->_getFilename($type);
  123. if (!empty($this->_size)) {
  124. $this->_rotateFile($filename);
  125. }
  126. $pathname = $this->_path . $filename;
  127. if (empty($this->_config['mask'])) {
  128. return file_put_contents($pathname, $output, FILE_APPEND);
  129. }
  130. $exists = file_exists($pathname);
  131. $r = file_put_contents($pathname, $output, FILE_APPEND);
  132. static $selfError = false;
  133. if (!$selfError && !$exists && !chmod($pathname, (int)$this->_config['mask'])) {
  134. $selfError = true;
  135. trigger_error(__d(
  136. 'cake_dev', 'Could not apply permission mask "%s" on log file "%s"',
  137. array($pathname, $this->_config['mask'])), E_USER_WARNING);
  138. $selfError = false;
  139. }
  140. }
  141. /**
  142. * Get filename
  143. * @param string $type The type of log.
  144. * @return string File name
  145. */
  146. protected function _getFilename($type) {
  147. $debugTypes = array('notice', 'info', 'debug');
  148. if (!empty($this->_file)) {
  149. $filename = $this->_file;
  150. } elseif ($type == 'error' || $type == 'warning') {
  151. $filename = 'error.log';
  152. } elseif (in_array($type, $debugTypes)) {
  153. $filename = 'debug.log';
  154. } else {
  155. $filename = $type . '.log';
  156. }
  157. return $filename;
  158. }
  159. /**
  160. * Rotate log file if size specified in config is reached.
  161. * Also if `rotate` count is reached oldest file is removed.
  162. *
  163. * @param string $filename Log file name
  164. * @return mixed True if rotated successfully or false in case of error.
  165. * Void if file doesn't need to be rotated.
  166. */
  167. protected function _rotateFile($filename) {
  168. $filepath = $this->_path . $filename;
  169. if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
  170. clearstatcache(true, $filepath);
  171. } else {
  172. clearstatcache();
  173. }
  174. if (!file_exists($filepath) ||
  175. filesize($filepath) < $this->_size
  176. ) {
  177. return;
  178. }
  179. if ($this->_config['rotate'] === 0) {
  180. return unlink($filepath);
  181. }
  182. if ($this->_config['rotate']) {
  183. $files = glob($filepath . '.*');
  184. if (count($files) === $this->_config['rotate']) {
  185. unlink(array_shift($files));
  186. }
  187. }
  188. return rename($filepath, $filepath . '.' . time());
  189. }
  190. }