PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/library/spoon/log/log.php

http://github.com/forkcms/forkcms
PHP | 208 lines | 61 code | 36 blank | 111 comment | 4 complexity | 9258fecf83da557c4b627d3bea50bce8 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, MIT, AGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Spoon Library
  4. *
  5. * This source file is part of the Spoon Library. More information,
  6. * documentation and tutorials can be found @ http://www.spoon-library.com
  7. *
  8. * @package spoon
  9. * @subpackage log
  10. *
  11. *
  12. * @author Davy Hellemans <davy@spoon-library.com>
  13. * @since 1.0.0
  14. */
  15. /**
  16. * This base class provides methods used to log data.
  17. *
  18. * @package spoon
  19. * @subpackage log
  20. *
  21. *
  22. * @author Davy Hellemans <davy@spoon-library.com>
  23. * @since 1.0.0
  24. */
  25. class SpoonLog
  26. {
  27. /**
  28. * Maximum log size expressed in megabytes.
  29. *
  30. * @var int
  31. */
  32. private $maxLogSize = 10;
  33. /**
  34. * Location for the log file.
  35. *
  36. * @var string
  37. */
  38. private $path;
  39. /**
  40. * Log type, can be determined freely.
  41. *
  42. * @var string
  43. */
  44. private $type;
  45. /**
  46. * Class constructor.
  47. *
  48. * @return SpoonLog
  49. * @param string[optional] $type This variable will be used as the name for the logfile.
  50. * @param string[optional] $path The location of the logfile(s).
  51. */
  52. public function __construct($type = 'custom', $path = null)
  53. {
  54. $this->setPath($path);
  55. $this->setType($type);
  56. }
  57. /**
  58. * Fetch the maximum log size, expressed in megabyte.
  59. *
  60. * @return int
  61. */
  62. public function getMaxLogSize()
  63. {
  64. return $this->maxLogSize;
  65. }
  66. /**
  67. * Fetch the logfile location.
  68. *
  69. * @return string
  70. */
  71. public function getPath()
  72. {
  73. return $this->path;
  74. }
  75. /**
  76. * Fetch the log type.
  77. *
  78. * @return string
  79. */
  80. public function getType()
  81. {
  82. return $this->type;
  83. }
  84. /**
  85. * Archive the current log file, but only if it exists.
  86. *
  87. * @return SpoonLog
  88. */
  89. public function rotate()
  90. {
  91. // file
  92. $file = $this->getPath() . '/' . $this->type . '.log';
  93. // rename file
  94. if(SpoonFile::exists($file)) SpoonDirectory::move($file, $file . '.' . date('Ymdhis'));
  95. // self
  96. return $this;
  97. }
  98. /**
  99. * Set the log maximum filesize before rotation occurs.
  100. *
  101. * @return SpoonLog
  102. * @param int $value The maximum log size expressed in megabytes, which needs to be 1 or more.
  103. */
  104. public function setMaxLogSize($value)
  105. {
  106. $this->maxLogSize = ((int) $value >= 1) ? (int) $value : $this->maxLogSize;
  107. return $this;
  108. }
  109. /**
  110. * Set the location where to save the logfile.
  111. *
  112. * @return SpoonLog
  113. * @param string[optional] $path The path where you want to store the logfile. If null it will be saved in 'spoon/log/*'.
  114. */
  115. public function setPath($path = null)
  116. {
  117. $this->path = ($path !== null) ? (string) $path : realpath(dirname(__FILE__));
  118. return $this;
  119. }
  120. /**
  121. * Sets the type, which will be used as the name for the logfile.
  122. *
  123. * @return SpoonLog
  124. * @param string $value The value for this type. Only a-z, 0-9, underscores and hyphens are allowed.
  125. */
  126. public function setType($value)
  127. {
  128. // redefine
  129. $value = (string) $value;
  130. // check for invalid characters
  131. if(!SpoonFilter::isValidAgainstRegexp('/(^[a-z0-9\-_]+$)/i', $value)) throw new SpoonLogException('The log type should only contain a-z, 0-9, underscores and hyphens. Your value "' . $value . '" is invalid.');
  132. // set type & return object
  133. $this->type = $value;
  134. return $this;
  135. }
  136. /**
  137. * Write a message to the log.
  138. *
  139. * @return SpoonLog
  140. * @param string $message the message that should be logged.
  141. */
  142. public function write($message)
  143. {
  144. // milliseconds
  145. list($milliseconds) = explode(' ', microtime());
  146. $milliseconds = round($milliseconds * 1000, 0);
  147. // redefine var
  148. $message = date('Y-m-d H:i:s') . ' ' . $milliseconds . 'ms | ' . $message . PHP_EOL;
  149. // file
  150. $file = $this->getPath() . '/' . $this->type . '.log';
  151. // rename if needed
  152. if(SpoonFile::exists($file) && (int) @filesize($file) >= ($this->maxLogSize * 1024 * 1024))
  153. {
  154. // start new log file
  155. $this->rotate();
  156. }
  157. // write content
  158. SpoonFile::setContent($file, $message, true, true);
  159. // self
  160. return $this;
  161. }
  162. }
  163. /**
  164. * This exception is used to handle log related exceptions.
  165. *
  166. * @package spoon
  167. * @subpackage log
  168. *
  169. * @author Davy Hellemans <davy@spoon-library.com>
  170. * @since 1.0.0
  171. */
  172. class SpoonLogException extends SpoonException {}