PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Admin/plugins/ckeditor/plugins/moxiemanager/classes/Util/Logger.php

https://gitlab.com/hoanghung.dev/phunuvadoisong.com
PHP | 456 lines | 213 code | 65 blank | 178 comment | 19 complexity | 776b25842004a414e02793db98c5efaa MD5 | raw file
  1. <?php
  2. /**
  3. * Logger.php
  4. *
  5. * Copyright 2003-2013, Moxiecode Systems AB, All rights reserved.
  6. */
  7. /**
  8. * Logging utility class. This class handles basic logging with levels, log rotation and custom log formats. It's
  9. * designed to be compact but still powerful and flexible.
  10. *
  11. * @package MOXMAN_Util
  12. */
  13. class MOXMAN_Util_Logger {
  14. /** @ignore */
  15. private $level, $path, $fileName, $format, $maxSize, $maxSizeBytes, $dateFormat, $filter;
  16. /**
  17. * Debug level.
  18. */
  19. const LEVEL_DEBUG = 0;
  20. /**
  21. * Info level.
  22. */
  23. const LEVEL_INFO = 10;
  24. /**
  25. * Warning level.
  26. */
  27. const LEVEL_WARN = 20;
  28. /**
  29. * Error level.
  30. */
  31. const LEVEL_ERROR = 30;
  32. /**
  33. * Fatal level.
  34. */
  35. const LEVEL_FATAL = 40;
  36. /**
  37. * Constructs a new logger instance.
  38. *
  39. * @param array $config Name/value array with config settings.
  40. */
  41. public function __construct($config = array()) {
  42. $config = array_merge(array(
  43. "path" => ".",
  44. "filename" => "{level}.log",
  45. "format" => "[{time}] [{level}] {message}",
  46. "max_size" => "100k",
  47. "max_files" => 10,
  48. "level" => "debug",
  49. "date_format" => "Y-m-d H:i:s",
  50. "filter" => ""
  51. ), $config);
  52. $this->setLevel($config["level"]);
  53. $this->setPath($config["path"]);
  54. $this->setFileName($config["filename"]);
  55. $this->setFormat($config["format"]);
  56. $this->setMaxSize($config["max_size"]);
  57. $this->setMaxFiles($config["max_files"]);
  58. $this->setDateFormat($config["date_format"]);
  59. $this->setFilter($config["filter"]);
  60. }
  61. /**
  62. * Sets the current log level, use the constants.
  63. *
  64. * @param int $level Log level instance for example DEBUG.
  65. */
  66. public function setLevel($level) {
  67. if (is_string($level)) {
  68. switch (strtolower($level)) {
  69. case "debug":
  70. $this->level = self::LEVEL_DEBUG;
  71. break;
  72. case "info":
  73. $this->level = self::LEVEL_INFO;
  74. break;
  75. case "warn":
  76. case "warning":
  77. $this->level = self::LEVEL_WARN;
  78. break;
  79. case "error":
  80. $this->level = self::LEVEL_ERROR;
  81. break;
  82. case "fatal":
  83. $this->level = self::LEVEL_FATAL;
  84. break;
  85. default:
  86. $this->level = self::LEVEL_FATAL;
  87. }
  88. }
  89. }
  90. /**
  91. * Returns the current log level for example LEVEL_DEBUG.
  92. *
  93. * @return int Current log level for example LEVEL_DEBUG.
  94. */
  95. public function getLevel() {
  96. return $this->level;
  97. }
  98. /**
  99. * Sets the current log directory path.
  100. *
  101. * @param string $path Path to directory to put the logs in.
  102. */
  103. public function setPath($path) {
  104. $this->path = $path;
  105. }
  106. /**
  107. * Returns the current log directory path.
  108. *
  109. * @return String Current log directory path.
  110. */
  111. public function getPath() {
  112. return $this->path;
  113. }
  114. /**
  115. * Sets the file name or file name pattern to use to save the log files.
  116. *
  117. * @param string $fileName File name or file name pattern to store the log data in.
  118. */
  119. public function setFileName($fileName) {
  120. $this->fileName = $fileName;
  121. }
  122. /**
  123. * Returns the current log file name or file name pattern.
  124. *
  125. * @return String Current log file name or file name pattern.
  126. */
  127. public function getFileName() {
  128. return $this->fileName;
  129. }
  130. /**
  131. * Set log format the items are: {level}, {time} and {message}
  132. *
  133. * @param string $format Log message format string.
  134. */
  135. public function setFormat($format) {
  136. $this->format = $format;
  137. }
  138. /**
  139. * Returns the log message format.
  140. *
  141. * @return String Log message format.
  142. */
  143. public function getFormat() {
  144. return $this->format;
  145. }
  146. /**
  147. * Sets the max log file size. If the log file gets larger than this size it will be rolled.
  148. *
  149. * @param string $size Size string to parse could be for example: 10mb, 10k or 10.
  150. */
  151. public function setMaxSize($size) {
  152. // Fix log max size
  153. $logMaxSizeBytes = intval(preg_replace("/[^0-9]/", "", $size));
  154. // Is KB
  155. if (strpos((strtolower($size)), "k") > 0) {
  156. $logMaxSizeBytes *= 1024;
  157. }
  158. // Is MB
  159. if (strpos((strtolower($size)), "m") > 0) {
  160. $logMaxSizeBytes *= (1024 * 1024);
  161. }
  162. $this->maxSizeBytes = $logMaxSizeBytes;
  163. $this->maxSize = $size;
  164. }
  165. /**
  166. * Returns the current filter regexp.
  167. *
  168. * @return String Filter regexp.
  169. */
  170. public function getFilter() {
  171. return $this->filter;
  172. }
  173. /**
  174. * Sets the filter regexp.
  175. *
  176. * @param string $filter Filter to apply.
  177. */
  178. public function setFilter($filter) {
  179. $this->filter = $filter;
  180. }
  181. /**
  182. * Returns the current max file size value.
  183. *
  184. * @return String Current max size value.
  185. */
  186. public function getMaxSize() {
  187. return $this->maxSizeBytes;
  188. }
  189. /**
  190. * Sets the number of files to roll before the last one gets deleted.
  191. *
  192. * @param int $maxFiles Number of files to roll.
  193. */
  194. public function setMaxFiles($maxFiles) {
  195. $this->maxFiles = $maxFiles;
  196. }
  197. /**
  198. * Returns the current max files value.
  199. *
  200. * @return int Current max files value.
  201. */
  202. public function getMaxFiles() {
  203. return $this->maxFiles;
  204. }
  205. /**
  206. * Sets the date format to put in the log files.
  207. *
  208. * @param string $format Data format string to use in log files.
  209. */
  210. public function setDateFormat($format) {
  211. $this->dateFormat = $format;
  212. }
  213. /**
  214. * Returns the current data format string.
  215. *
  216. * @return String Current date format string.
  217. */
  218. public function getDateFormat() {
  219. return $this->dateFormat;
  220. }
  221. /**
  222. * Adds a debug message to the log file.
  223. *
  224. * @param Mixed $msg One or more arguments to combine into a log message.
  225. */
  226. public function debug($msg) {
  227. $args = func_get_args();
  228. $this->logMsg(self::LEVEL_DEBUG, $args);
  229. }
  230. /**
  231. * Adds a info message to the log file.
  232. *
  233. * @param Mixed $msg One or more arguments to combine into a log message.
  234. */
  235. public function info($msg) {
  236. $args = func_get_args();
  237. $this->logMsg(self::LEVEL_INFO, $args);
  238. }
  239. /**
  240. * Adds a warning message to the log file.
  241. *
  242. * @param Mixed $msg One or more arguments to combine into a log message.
  243. */
  244. public function warn($msg) {
  245. $args = func_get_args();
  246. $this->logMsg(self::LEVEL_WARN, $args);
  247. }
  248. /**
  249. * Adds a error message to the log file.
  250. *
  251. * @param Mixed $msg One or more arguments to combine into a log message.
  252. */
  253. public function error($msg) {
  254. $args = func_get_args();
  255. $this->logMsg(self::LEVEL_ERROR, $args);
  256. }
  257. /**
  258. * Adds a fatal message to the log file.
  259. *
  260. * @param Mixed $msg One or more arguments to combine into a log message.
  261. */
  262. public function fatal($msg) {
  263. $args = func_get_args();
  264. $this->logMsg(self::LEVEL_FATAL, $args);
  265. }
  266. /**
  267. * Returns true/false if the debug level is available.
  268. *
  269. * @return Boolean true/false if the level is higher or equals to debug.
  270. */
  271. public function isDebugEnabled() {
  272. return $this->level <= self::LEVEL_DEBUG;
  273. }
  274. /**
  275. * Returns true/false if the info level is available.
  276. *
  277. * @return Boolean true/false if the level is higher or equals to info.
  278. */
  279. public function isInfoEnabled() {
  280. return $this->level <= self::LEVEL_INFO;
  281. }
  282. /**
  283. * Returns true/false if the warn level is available.
  284. *
  285. * @return Boolean true/false if the level is higher or equals to warn.
  286. */
  287. public function isWarnEnabled() {
  288. return $this->level <= self::LEVEL_WARN;
  289. }
  290. /**
  291. * Returns true/false if the error level is available.
  292. *
  293. * @return Boolean true/false if the level is higher or equals to error.
  294. */
  295. public function isErrorEnabled() {
  296. return $this->level <= self::LEVEL_ERROR;
  297. }
  298. /**
  299. * Returns true/false if the fatal level is available.
  300. *
  301. * @return Boolean true/false if the level is higher or equals to fatal.
  302. */
  303. public function isFatalEnabled() {
  304. return $this->level <= self::LEVEL_FATAL;
  305. }
  306. /** @ignore */
  307. private function logMsg($level, $args) {
  308. if ($level < $this->level) {
  309. return;
  310. }
  311. $message = "";
  312. foreach ($args as $arg) {
  313. // Separate arguments with commas
  314. if ($message) {
  315. $message .= ", ";
  316. }
  317. // Convert true/false to string
  318. if (is_bool($arg)) {
  319. $arg = $arg ? "true" : "false";
  320. }
  321. // Convert exception to string
  322. if ($arg instanceof Exception) {
  323. $arg = $arg->getMessage();
  324. }
  325. $message .= $arg;
  326. }
  327. // See if message matches the filter
  328. if ($this->filter && !preg_match($this->filter, $message)) {
  329. return;
  330. }
  331. $logFile = $this->path . "/" . $this->fileName;
  332. // Ignore if we have no write access
  333. // TODO: Might want to push out an exception (eternity loop?) here or just die.
  334. //if (!is_writable($logFile))
  335. // return;
  336. switch ($level) {
  337. case self::LEVEL_DEBUG:
  338. $levelName = "DEBUG";
  339. break;
  340. case self::LEVEL_INFO:
  341. $levelName = "INFO";
  342. break;
  343. case self::LEVEL_WARN:
  344. $levelName = "WARN";
  345. break;
  346. case self::LEVEL_ERROR:
  347. $levelName = "ERROR";
  348. break;
  349. case self::LEVEL_FATAL:
  350. $levelName = "FATAL";
  351. break;
  352. }
  353. $logFile = str_replace('{level}', strtolower($levelName), $logFile);
  354. $text = $this->format;
  355. $text = str_replace('{time}', date($this->dateFormat), $text);
  356. $text = str_replace('{level}', strtolower($levelName), $text);
  357. $text = str_replace('{message}', $message, $text);
  358. $message = $text . "\r\n";
  359. // Check filesize
  360. $roll = false;
  361. if (file_exists($logFile)) {
  362. $size = filesize($logFile);
  363. if ($size + strlen($message) > $this->maxSizeBytes) {
  364. $roll = true;
  365. }
  366. }
  367. // Roll if the size is right
  368. if ($roll) {
  369. for ($i = $this->maxFiles; $i >= 1; $i--) {
  370. $rfile = $logFile . "." . $i;
  371. $nfile = $logFile . "." . ($i + 1);
  372. if (file_exists($rfile)) {
  373. rename($rfile, $nfile);
  374. }
  375. }
  376. rename($logFile, $logFile . ".1");
  377. // Delete last logfile
  378. $delfile = $logFile . "." . ($this->maxFiles + 1);
  379. if (file_exists($delfile)) {
  380. unlink($delfile);
  381. }
  382. }
  383. // Append log line
  384. if (($fp = fopen($logFile, "a")) != null) {
  385. fputs($fp, $message);
  386. fflush($fp);
  387. fclose($fp);
  388. }
  389. }
  390. }
  391. ?>