LoggerAppenderSkeleton.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 . '/LoggerAppender.php');
  25. require_once(LOG4PHP_DIR . '/LoggerLog.php');
  26. require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
  27. /**
  28. * Abstract superclass of the other appenders in the package.
  29. *
  30. * This class provides the code for common functionality, such as
  31. * support for threshold filtering and support for general filters.
  32. *
  33. * @author VxR <vxr@vxr.it>
  34. * @author Sergio Strampelli <sergio@ascia.net>
  35. * @version $Revision: 1.1 $
  36. * @package log4php
  37. * @abstract
  38. */
  39. class LoggerAppenderSkeleton extends LoggerAppender {
  40. /**
  41. * @var boolean closed appender flag
  42. */
  43. var $closed;
  44. /**
  45. * @var object unused
  46. */
  47. var $errorHandler;
  48. /**
  49. * The first filter in the filter chain
  50. * @var LoggerFilter
  51. */
  52. var $headFilter = null;
  53. /**
  54. * LoggerLayout for this appender. It can be null if appender has its own layout
  55. * @var LoggerLayout
  56. */
  57. var $layout = null;
  58. /**
  59. * @var string Appender name
  60. */
  61. var $name;
  62. /**
  63. * The last filter in the filter chain
  64. * @var LoggerFilter
  65. */
  66. var $tailFilter = null;
  67. /**
  68. * @var LoggerLevel There is no level threshold filtering by default.
  69. */
  70. var $threshold = null;
  71. /**
  72. * @var boolean needs a layout formatting ?
  73. */
  74. var $requiresLayout = false;
  75. /* --------------------------------------------------------------------------*/
  76. /* --------------------------------------------------------------------------*/
  77. /* --------------------------------------------------------------------------*/
  78. /**
  79. * Constructor
  80. *
  81. * @param string $name appender name
  82. */
  83. function LoggerAppenderSkeleton($name)
  84. {
  85. $this->name = $name;
  86. $this->clearFilters();
  87. }
  88. /**
  89. * @param LoggerFilter $newFilter add a new LoggerFilter
  90. * @see LoggerAppender::addFilter()
  91. */
  92. function addFilter($newFilter)
  93. {
  94. if($this->headFilter === null) {
  95. $this->headFilter = $newFilter;
  96. $this->tailFilter =& $this->headFilter;
  97. } else {
  98. $this->tailFilter->next = $newFilter;
  99. $this->tailFilter =& $this->tailFilter->next;
  100. }
  101. }
  102. /**
  103. * Derived appenders should override this method if option structure
  104. * requires it.
  105. */
  106. function activateOptions()
  107. {
  108. }
  109. /**
  110. * Subclasses of {@link LoggerAppenderSkeleton} should implement
  111. * this method to perform actual logging.
  112. *
  113. * @param LoggerLoggingEvent $event
  114. * @see doAppend()
  115. * @abstract
  116. */
  117. function append($event)
  118. {
  119. // override me
  120. }
  121. /**
  122. * @see LoggerAppender::clearFilters()
  123. */
  124. function clearFilters()
  125. {
  126. unset($this->headFilter);
  127. unset($this->tailFilter);
  128. $this->headFilter = null;
  129. $this->tailFilter = null;
  130. }
  131. /**
  132. * @see LoggerAppender::close()
  133. */
  134. function close()
  135. {
  136. //override me
  137. }
  138. /**
  139. * Finalize this appender by calling the derived class' <i>close()</i> method.
  140. */
  141. function finalize()
  142. {
  143. // An appender might be closed then garbage collected. There is no
  144. // point in closing twice.
  145. if ($this->closed) return;
  146. LoggerLog::debug("LoggerAppenderSkeleton::finalize():name=[{$this->name}].");
  147. $this->close();
  148. }
  149. /**
  150. * Do not use this method.
  151. * @see LoggerAppender::getErrorHandler()
  152. * @return object
  153. */
  154. function &getErrorHandler()
  155. {
  156. return $this->errorHandler;
  157. }
  158. /**
  159. * @see LoggerAppender::getFilter()
  160. * @return Filter
  161. */
  162. function &getFilter()
  163. {
  164. return $this->headFilter;
  165. }
  166. /**
  167. * Return the first filter in the filter chain for this Appender.
  168. * The return value may be <i>null</i> if no is filter is set.
  169. * @return Filter
  170. */
  171. function &getFirstFilter()
  172. {
  173. return $this->headFilter;
  174. }
  175. /**
  176. * @see LoggerAppender::getLayout()
  177. * @return LoggerLayout
  178. */
  179. function &getLayout()
  180. {
  181. return $this->layout;
  182. }
  183. /**
  184. * @see LoggerAppender::getName()
  185. * @return string
  186. */
  187. function getName()
  188. {
  189. return $this->name;
  190. }
  191. /**
  192. * Returns this appenders threshold level.
  193. * See the {@link setThreshold()} method for the meaning of this option.
  194. * @return LoggerLevel
  195. */
  196. function &getThreshold()
  197. {
  198. return $this->threshold;
  199. }
  200. /**
  201. * Check whether the message level is below the appender's threshold.
  202. *
  203. *
  204. * If there is no threshold set, then the return value is always <i>true</i>.
  205. * @param LoggerLevel $priority
  206. * @return boolean true if priority is greater or equal than threshold
  207. */
  208. function isAsSevereAsThreshold($priority)
  209. {
  210. if ($this->threshold === null)
  211. return true;
  212. return $priority->isGreaterOrEqual($this->getThreshold());
  213. }
  214. /**
  215. * @see LoggerAppender::doAppend()
  216. * @param LoggerLoggingEvent $event
  217. */
  218. function doAppend($event)
  219. {
  220. LoggerLog::debug("LoggerAppenderSkeleton::doAppend()");
  221. if ($this->closed) {
  222. LoggerLog::debug("LoggerAppenderSkeleton::doAppend() Attempted to append to closed appender named [{$this->name}].");
  223. return;
  224. }
  225. if(!$this->isAsSevereAsThreshold($event->getLevel())) {
  226. LoggerLog::debug("LoggerAppenderSkeleton::doAppend() event level is less severe than threshold.");
  227. return;
  228. }
  229. $f = $this->getFirstFilter();
  230. while($f !== null) {
  231. switch ($f->decide($event)) {
  232. case LOG4PHP_LOGGER_FILTER_DENY: return;
  233. case LOG4PHP_LOGGER_FILTER_ACCEPT: return $this->append($event);
  234. case LOG4PHP_LOGGER_FILTER_NEUTRAL: $f = $f->next;
  235. }
  236. }
  237. $this->append($event);
  238. }
  239. /**
  240. * @see LoggerAppender::requiresLayout()
  241. * @return boolean
  242. */
  243. function requiresLayout()
  244. {
  245. return $this->requiresLayout;
  246. }
  247. /**
  248. * @see LoggerAppender::setErrorHandler()
  249. * @param object
  250. */
  251. function setErrorHandler($errorHandler)
  252. {
  253. if($errorHandler == null) {
  254. // We do not throw exception here since the cause is probably a
  255. // bad config file.
  256. LoggerLog::warn("You have tried to set a null error-handler.");
  257. } else {
  258. $this->errorHandler = $errorHandler;
  259. }
  260. }
  261. /**
  262. * @see LoggerAppender::setLayout()
  263. * @param LoggerLayout $layout
  264. */
  265. function setLayout($layout)
  266. {
  267. if ($this->requiresLayout())
  268. $this->layout = $layout;
  269. }
  270. /**
  271. * @see LoggerAppender::setName()
  272. * @param string $name
  273. */
  274. function setName($name)
  275. {
  276. $this->name = $name;
  277. }
  278. /**
  279. * Set the threshold level of this appender.
  280. *
  281. * @param mixed $threshold can be a {@link LoggerLevel} object or a string.
  282. * @see LoggerOptionConverter::toLevel()
  283. */
  284. function setThreshold($threshold)
  285. {
  286. if (is_string($threshold)) {
  287. $this->threshold = LoggerOptionConverter::toLevel($threshold, null);
  288. }elseif (is_a($threshold, 'loggerlevel')) {
  289. $this->threshold = $threshold;
  290. }
  291. }
  292. /**
  293. * Perform actions before object serialization.
  294. *
  295. * Call {@link finalize()} to properly close the appender.
  296. */
  297. function __sleep()
  298. {
  299. $this->finalize();
  300. return array_keys(get_object_vars($this));
  301. }
  302. /**
  303. * Perform actions after object deserialization.
  304. *
  305. * Call {@link activateOptions()} to properly setup the appender.
  306. */
  307. function __wakeup()
  308. {
  309. $this->activateOptions();
  310. }
  311. }
  312. ?>