FileLog.php 5.3 KB

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