PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/editor/tinymce/plugins/spellchecker/classes/utils/Logger.php

https://bitbucket.org/moodle/moodle
PHP | 278 lines | 181 code | 54 blank | 43 comment | 14 complexity | 5459195b7ea1e14afda3c2d3438371db MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. /**
  3. * $Id: Logger.class.php 10 2007-05-27 10:55:12Z spocke $
  4. *
  5. * @package MCFileManager.filesystems
  6. * @author Moxiecode
  7. * @copyright Copyright © 2005, Moxiecode Systems AB, All rights reserved.
  8. */
  9. // File type contstants
  10. define('MC_LOGGER_DEBUG', 0);
  11. define('MC_LOGGER_INFO', 10);
  12. define('MC_LOGGER_WARN', 20);
  13. define('MC_LOGGER_ERROR', 30);
  14. define('MC_LOGGER_FATAL', 40);
  15. /**
  16. * Logging utility class. This class handles basic logging with levels, log rotation and custom log formats. It's
  17. * designed to be compact but still powerful and flexible.
  18. */
  19. class Moxiecode_Logger {
  20. // Private fields
  21. var $_path;
  22. var $_filename;
  23. var $_maxSize;
  24. var $_maxFiles;
  25. var $_maxSizeBytes;
  26. var $_level;
  27. var $_format;
  28. /**
  29. * Constructs a new logger instance.
  30. */
  31. public function __construct() {
  32. $this->_path = "";
  33. $this->_filename = "{level}.log";
  34. $this->setMaxSize("100k");
  35. $this->_maxFiles = 10;
  36. $this->_level = MC_LOGGER_DEBUG;
  37. $this->_format = "[{time}] [{level}] {message}";
  38. }
  39. /**
  40. * Old syntax of class constructor. Deprecated in PHP7.
  41. *
  42. * @deprecated since Moodle 3.1
  43. */
  44. public function Moxiecode_Logger() {
  45. debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  46. self::__construct();
  47. }
  48. /**
  49. * Sets the current log level, use the MC_LOGGER constants.
  50. *
  51. * @param int $level Log level instance for example MC_LOGGER_DEBUG.
  52. */
  53. function setLevel($level) {
  54. if (is_string($level)) {
  55. switch (strtolower($level)) {
  56. case "debug":
  57. $level = MC_LOGGER_DEBUG;
  58. break;
  59. case "info":
  60. $level = MC_LOGGER_INFO;
  61. break;
  62. case "warn":
  63. case "warning":
  64. $level = MC_LOGGER_WARN;
  65. break;
  66. case "error":
  67. $level = MC_LOGGER_ERROR;
  68. break;
  69. case "fatal":
  70. $level = MC_LOGGER_FATAL;
  71. break;
  72. default:
  73. $level = MC_LOGGER_FATAL;
  74. }
  75. }
  76. $this->_level = $level;
  77. }
  78. /**
  79. * Returns the current log level for example MC_LOGGER_DEBUG.
  80. *
  81. * @return int Current log level for example MC_LOGGER_DEBUG.
  82. */
  83. function getLevel() {
  84. return $this->_level;
  85. }
  86. function setPath($path) {
  87. $this->_path = $path;
  88. }
  89. function getPath() {
  90. return $this->_path;
  91. }
  92. function setFileName($file_name) {
  93. $this->_filename = $file_name;
  94. }
  95. function getFileName() {
  96. return $this->_filename;
  97. }
  98. function setFormat($format) {
  99. $this->_format = $format;
  100. }
  101. function getFormat() {
  102. return $this->_format;
  103. }
  104. function setMaxSize($size) {
  105. // Fix log max size
  106. $logMaxSizeBytes = intval(preg_replace("/[^0-9]/", "", $size));
  107. // Is KB
  108. if (strpos((strtolower($size)), "k") > 0)
  109. $logMaxSizeBytes *= 1024;
  110. // Is MB
  111. if (strpos((strtolower($size)), "m") > 0)
  112. $logMaxSizeBytes *= (1024 * 1024);
  113. $this->_maxSizeBytes = $logMaxSizeBytes;
  114. $this->_maxSize = $size;
  115. }
  116. function getMaxSize() {
  117. return $this->_maxSize;
  118. }
  119. function setMaxFiles($max_files) {
  120. $this->_maxFiles = $max_files;
  121. }
  122. function getMaxFiles() {
  123. return $this->_maxFiles;
  124. }
  125. function debug($msg) {
  126. $args = func_get_args();
  127. $this->_logMsg(MC_LOGGER_DEBUG, implode(', ', $args));
  128. }
  129. function info($msg) {
  130. $args = func_get_args();
  131. $this->_logMsg(MC_LOGGER_INFO, implode(', ', $args));
  132. }
  133. function warn($msg) {
  134. $args = func_get_args();
  135. $this->_logMsg(MC_LOGGER_WARN, implode(', ', $args));
  136. }
  137. function error($msg) {
  138. $args = func_get_args();
  139. $this->_logMsg(MC_LOGGER_ERROR, implode(', ', $args));
  140. }
  141. function fatal($msg) {
  142. $args = func_get_args();
  143. $this->_logMsg(MC_LOGGER_FATAL, implode(', ', $args));
  144. }
  145. function isDebugEnabled() {
  146. return $this->_level >= MC_LOGGER_DEBUG;
  147. }
  148. function isInfoEnabled() {
  149. return $this->_level >= MC_LOGGER_INFO;
  150. }
  151. function isWarnEnabled() {
  152. return $this->_level >= MC_LOGGER_WARN;
  153. }
  154. function isErrorEnabled() {
  155. return $this->_level >= MC_LOGGER_ERROR;
  156. }
  157. function isFatalEnabled() {
  158. return $this->_level >= MC_LOGGER_FATAL;
  159. }
  160. function _logMsg($level, $message) {
  161. $roll = false;
  162. if ($level < $this->_level)
  163. return;
  164. $logFile = $this->toOSPath($this->_path . "/" . $this->_filename);
  165. switch ($level) {
  166. case MC_LOGGER_DEBUG:
  167. $levelName = "DEBUG";
  168. break;
  169. case MC_LOGGER_INFO:
  170. $levelName = "INFO";
  171. break;
  172. case MC_LOGGER_WARN:
  173. $levelName = "WARN";
  174. break;
  175. case MC_LOGGER_ERROR:
  176. $levelName = "ERROR";
  177. break;
  178. case MC_LOGGER_FATAL:
  179. $levelName = "FATAL";
  180. break;
  181. }
  182. $logFile = str_replace('{level}', strtolower($levelName), $logFile);
  183. $text = $this->_format;
  184. $text = str_replace('{time}', date("Y-m-d H:i:s"), $text);
  185. $text = str_replace('{level}', strtolower($levelName), $text);
  186. $text = str_replace('{message}', $message, $text);
  187. $message = $text . "\r\n";
  188. // Check filesize
  189. if (file_exists($logFile)) {
  190. $size = @filesize($logFile);
  191. if ($size + strlen($message) > $this->_maxSizeBytes)
  192. $roll = true;
  193. }
  194. // Roll if the size is right
  195. if ($roll) {
  196. for ($i=$this->_maxFiles-1; $i>=1; $i--) {
  197. $rfile = $this->toOSPath($logFile . "." . $i);
  198. $nfile = $this->toOSPath($logFile . "." . ($i+1));
  199. if (@file_exists($rfile))
  200. @rename($rfile, $nfile);
  201. }
  202. @rename($logFile, $this->toOSPath($logFile . ".1"));
  203. // Delete last logfile
  204. $delfile = $this->toOSPath($logFile . "." . ($this->_maxFiles + 1));
  205. if (@file_exists($delfile))
  206. @unlink($delfile);
  207. }
  208. // Append log line
  209. if (($fp = @fopen($logFile, "a")) != null) {
  210. @fputs($fp, $message);
  211. @fflush($fp);
  212. @fclose($fp);
  213. }
  214. }
  215. /**
  216. * Converts a Unix path to OS specific path.
  217. *
  218. * @param String $path Unix path to convert.
  219. */
  220. function toOSPath($path) {
  221. return str_replace("/", DIRECTORY_SEPARATOR, $path);
  222. }
  223. }
  224. ?>