PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/station-games/vendor/cakephp/cakephp/src/Log/Engine/FileLog.php

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