LoggerLevel.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * log4php is a PHP port of the log4j java logging package.
  4. *
  5. * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
  6. * <p>Design, strategies and part of the methods documentation are developed by log4j team
  7. * (Ceki Gülcü as log4j project founder and
  8. * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
  9. *
  10. * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
  11. * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
  12. *
  13. * <p>This software is published under the terms of the LGPL License
  14. * a copy of which has been included with this distribution in the LICENSE file.</p>
  15. *
  16. * @package log4php
  17. */
  18. /**
  19. * @ignore
  20. */
  21. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__));
  22. /**
  23. */
  24. require_once(LOG4PHP_DIR . '/LoggerLog.php');
  25. define('LOG4PHP_LEVEL_OFF_INT', 2147483647);
  26. define('LOG4PHP_LEVEL_FATAL_INT', 50000);
  27. define('LOG4PHP_LEVEL_ERROR_INT', 40000);
  28. define('LOG4PHP_LEVEL_WARN_INT', 30000);
  29. define('LOG4PHP_LEVEL_INFO_INT', 20000);
  30. define('LOG4PHP_LEVEL_DEBUG_INT', 10000);
  31. define('LOG4PHP_LEVEL_ALL_INT', -2147483648);
  32. /**
  33. * Defines the minimum set of levels recognized by the system, that is
  34. * <i>OFF</i>, <i>FATAL</i>, <i>ERROR</i>,
  35. * <i>WARN</i>, <i>INFO</i, <i>DEBUG</i> and
  36. * <i>ALL</i>.
  37. *
  38. * <p>The <i>LoggerLevel</i> class may be subclassed to define a larger
  39. * level set.</p>
  40. *
  41. * @author VxR <vxr@vxr.it>
  42. * @version $Revision: 1.1 $
  43. * @package log4php
  44. * @since 0.5
  45. */
  46. class LoggerLevel {
  47. /**
  48. * @var integer
  49. */
  50. var $level;
  51. /**
  52. * @var string
  53. */
  54. var $levelStr;
  55. /**
  56. * @var integer
  57. */
  58. var $syslogEquivalent;
  59. /**
  60. * Constructor
  61. *
  62. * @param integer $level
  63. * @param string $levelStr
  64. * @param integer $syslogEquivalent
  65. */
  66. function LoggerLevel($level, $levelStr, $syslogEquivalent)
  67. {
  68. $this->level = $level;
  69. $this->levelStr = $levelStr;
  70. $this->syslogEquivalent = $syslogEquivalent;
  71. }
  72. /**
  73. * Two priorities are equal if their level fields are equal.
  74. *
  75. * @param object $o
  76. * @return boolean
  77. */
  78. function equals($o)
  79. {
  80. if (is_a($o, 'loggerlevel')) {
  81. return ($this->level == $o->level);
  82. } else {
  83. return false;
  84. }
  85. }
  86. /**
  87. * Returns an Off Level
  88. * @static
  89. * @return LoggerLevel
  90. */
  91. function &getLevelOff()
  92. {
  93. static $level;
  94. if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_OFF_INT, 'OFF', 0);
  95. return $level;
  96. }
  97. /**
  98. * Returns a Fatal Level
  99. * @static
  100. * @return LoggerLevel
  101. */
  102. function &getLevelFatal()
  103. {
  104. static $level;
  105. if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_FATAL_INT, 'FATAL', 0);
  106. return $level;
  107. }
  108. /**
  109. * Returns an Error Level
  110. * @static
  111. * @return LoggerLevel
  112. */
  113. function &getLevelError()
  114. {
  115. static $level;
  116. if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_ERROR_INT, 'ERROR', 3);
  117. return $level;
  118. }
  119. /**
  120. * Returns a Warn Level
  121. * @static
  122. * @return LoggerLevel
  123. */
  124. function &getLevelWarn()
  125. {
  126. static $level;
  127. if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_WARN_INT, 'WARN', 4);
  128. return $level;
  129. }
  130. /**
  131. * Returns an Info Level
  132. * @static
  133. * @return LoggerLevel
  134. */
  135. function &getLevelInfo()
  136. {
  137. static $level;
  138. if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_INFO_INT, 'INFO', 6);
  139. return $level;
  140. }
  141. /**
  142. * Returns a Debug Level
  143. * @static
  144. * @return LoggerLevel
  145. */
  146. function &getLevelDebug()
  147. {
  148. static $level;
  149. if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_DEBUG_INT, 'DEBUG', 7);
  150. return $level;
  151. }
  152. /**
  153. * Returns an All Level
  154. * @static
  155. * @return LoggerLevel
  156. */
  157. function &getLevelAll()
  158. {
  159. static $level;
  160. if (!isset($level)) $level = new LoggerLevel(LOG4PHP_LEVEL_ALL_INT, 'ALL', 7);
  161. return $level;
  162. }
  163. /**
  164. * Return the syslog equivalent of this priority as an integer.
  165. * @final
  166. * @return integer
  167. */
  168. function getSyslogEquivalent()
  169. {
  170. return $this->syslogEquivalent;
  171. }
  172. /**
  173. * Returns <i>true</i> if this level has a higher or equal
  174. * level than the level passed as argument, <i>false</i>
  175. * otherwise.
  176. *
  177. * <p>You should think twice before overriding the default
  178. * implementation of <i>isGreaterOrEqual</i> method.
  179. *
  180. * @param LoggerLevel $r
  181. * @return boolean
  182. */
  183. function isGreaterOrEqual($r)
  184. {
  185. return $this->level >= $r->level;
  186. }
  187. /**
  188. * Returns the string representation of this priority.
  189. * @return string
  190. * @final
  191. */
  192. function toString()
  193. {
  194. return $this->levelStr;
  195. }
  196. /**
  197. * Returns the integer representation of this level.
  198. * @return integer
  199. */
  200. function toInt()
  201. {
  202. return $this->level;
  203. }
  204. /**
  205. * Convert the string passed as argument to a level. If the
  206. * conversion fails, then this method returns a DEBUG Level.
  207. *
  208. * @param mixed $arg
  209. * @param LoggerLevel $default
  210. * @static
  211. */
  212. function &toLevel($arg, $defaultLevel = null)
  213. {
  214. if ($defaultLevel === null) {
  215. return LoggerLevel::toLevel($arg, LoggerLevel::getLevelDebug());
  216. } else {
  217. if (is_int($arg)) {
  218. switch($arg) {
  219. case LOG4PHP_LEVEL_ALL_INT: return LoggerLevel::getLevelAll();
  220. case LOG4PHP_LEVEL_DEBUG_INT: return LoggerLevel::getLevelDebug();
  221. case LOG4PHP_LEVEL_INFO_INT: return LoggerLevel::getLevelInfo();
  222. case LOG4PHP_LEVEL_WARN_INT: return LoggerLevel::getLevelWarn();
  223. case LOG4PHP_LEVEL_ERROR_INT: return LoggerLevel::getLevelError();
  224. case LOG4PHP_LEVEL_FATAL_INT: return LoggerLevel::getLevelFatal();
  225. case LOG4PHP_LEVEL_OFF_INT: return LoggerLevel::getLevelOff();
  226. default: return $defaultLevel;
  227. }
  228. } else {
  229. switch(strtoupper($arg)) {
  230. case 'ALL': return LoggerLevel::getLevelAll();
  231. case 'DEBUG': return LoggerLevel::getLevelDebug();
  232. case 'INFO': return LoggerLevel::getLevelInfo();
  233. case 'WARN': return LoggerLevel::getLevelWarn();
  234. case 'ERROR': return LoggerLevel::getLevelError();
  235. case 'FATAL': return LoggerLevel::getLevelFatal();
  236. case 'OFF': return LoggerLevel::getLevelOff();
  237. default: return $defaultLevel;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. ?>