PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/log4php/LoggerAppender.php

http://sabre-zarafa.googlecode.com/
PHP | 287 lines | 93 code | 31 blank | 163 comment | 8 complexity | f409887d4447d7c6804ce90413b36328 MD5 | raw file
  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * 7 http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * @package log4php
  19. */
  20. /**
  21. * Abstract class that defines output logs strategies.
  22. *
  23. * @version $Revision: 31 $
  24. * @package log4php
  25. */
  26. abstract class LoggerAppender extends LoggerConfigurable {
  27. /**
  28. * Set to true when the appender is closed. A closed appender will not
  29. * accept any logging requests.
  30. * @var boolean
  31. */
  32. protected $closed = false;
  33. /**
  34. * The first filter in the filter chain.
  35. * @var LoggerFilter
  36. */
  37. protected $filter;
  38. /**
  39. * The appender's layout. Can be null if the appender does not use
  40. * a layout.
  41. * @var LoggerLayout
  42. */
  43. protected $layout;
  44. /**
  45. * Appender name. Used by other components to identify this appender.
  46. * @var string
  47. */
  48. protected $name;
  49. /**
  50. * Appender threshold level. Events whose level is below the threshold
  51. * will not be logged.
  52. * @var LoggerLevel
  53. */
  54. protected $threshold;
  55. /**
  56. * Set to true if the appender requires a layout.
  57. *
  58. * True by default, appenders which do not use a layout should override
  59. * this property to false.
  60. *
  61. * @var boolean
  62. */
  63. protected $requiresLayout = true;
  64. /**
  65. * Default constructor.
  66. * @param string $name Appender name
  67. */
  68. public function __construct($name = '') {
  69. $this->name = $name;
  70. // Closes the appender on shutdown. Better than a destructor because
  71. // it will be called even if a fatal error occurs (destructor won't).
  72. register_shutdown_function(array($this, 'close'));
  73. if ($this->requiresLayout) {
  74. $this->layout = $this->getDefaultLayout();
  75. }
  76. }
  77. /**
  78. * Returns the default layout for this appender. Can be overriden by
  79. * derived appenders.
  80. *
  81. * @return LoggerLayout
  82. */
  83. public function getDefaultLayout()
  84. {
  85. return new LoggerLayoutSimple();
  86. }
  87. /**
  88. * Adds a filter to the end of the filter chain.
  89. * @param LoggerFilter $filter add a new LoggerFilter
  90. */
  91. public function addFilter($filter) {
  92. if($this->filter === null) {
  93. $this->filter = $filter;
  94. } else {
  95. $this->filter->addNext($filter);
  96. }
  97. }
  98. /**
  99. * Clears the filter chain by removing all the filters in it.
  100. */
  101. public function clearFilters() {
  102. $this->filter = null;
  103. }
  104. /**
  105. * Returns the first filter in the filter chain.
  106. * The return value may be <i>null</i> if no is filter is set.
  107. * @return LoggerFilter
  108. */
  109. public function getFilter() {
  110. return $this->filter;
  111. }
  112. /**
  113. * Returns the first filter in the filter chain.
  114. * The return value may be <i>null</i> if no is filter is set.
  115. * @return LoggerFilter
  116. */
  117. public function getFirstFilter() {
  118. return $this->filter;
  119. }
  120. /**
  121. * Performs threshold checks and invokes filters before delegating logging
  122. * to the subclass' specific <i>append()</i> method.
  123. * @see LoggerAppender::append()
  124. * @param LoggerLoggingEvent $event
  125. */
  126. public function doAppend(LoggerLoggingEvent $event) {
  127. if($this->closed) {
  128. return;
  129. }
  130. if(!$this->isAsSevereAsThreshold($event->getLevel())) {
  131. return;
  132. }
  133. $f = $this->getFirstFilter();
  134. while($f !== null) {
  135. switch ($f->decide($event)) {
  136. case LoggerFilter::DENY: return;
  137. case LoggerFilter::ACCEPT: return $this->append($event);
  138. case LoggerFilter::NEUTRAL: $f = $f->getNext();
  139. }
  140. }
  141. $this->append($event);
  142. }
  143. /**
  144. * Sets the appender layout.
  145. * @param LoggerLayout $layout
  146. */
  147. public function setLayout($layout) {
  148. if($this->requiresLayout()) {
  149. $this->layout = $layout;
  150. }
  151. }
  152. /**
  153. * Returns the appender layout.
  154. * @return LoggerLayout
  155. */
  156. public function getLayout() {
  157. return $this->layout;
  158. }
  159. /**
  160. * Configurators call this method to determine if the appender
  161. * requires a layout.
  162. *
  163. * <p>If this method returns <i>true</i>, meaning that layout is required,
  164. * then the configurator will configure a layout using the configuration
  165. * information at its disposal. If this method returns <i>false</i>,
  166. * meaning that a layout is not required, then layout configuration will be
  167. * skipped even if there is available layout configuration
  168. * information at the disposal of the configurator.</p>
  169. *
  170. * <p>In the rather exceptional case, where the appender
  171. * implementation admits a layout but can also work without it, then
  172. * the appender should return <i>true</i>.</p>
  173. *
  174. * @return boolean
  175. */
  176. public function requiresLayout() {
  177. return $this->requiresLayout;
  178. }
  179. /**
  180. * Retruns the appender name.
  181. * @return string
  182. */
  183. public function getName() {
  184. return $this->name;
  185. }
  186. /**
  187. * Sets the appender name.
  188. * @param string $name
  189. */
  190. public function setName($name) {
  191. $this->name = $name;
  192. }
  193. /**
  194. * Returns the appender's threshold level.
  195. * @return LoggerLevel
  196. */
  197. public function getThreshold() {
  198. return $this->threshold;
  199. }
  200. /**
  201. * Sets the appender threshold.
  202. *
  203. * @param LoggerLevel|string $threshold Either a {@link LoggerLevel}
  204. * object or a string equivalent.
  205. * @see LoggerOptionConverter::toLevel()
  206. */
  207. public function setThreshold($threshold) {
  208. $this->setLevel('threshold', $threshold);
  209. }
  210. /**
  211. * Checks whether the message level is below the appender's threshold.
  212. *
  213. * If there is no threshold set, then the return value is always <i>true</i>.
  214. *
  215. * @param LoggerLevel $level
  216. * @return boolean Returns true if level is greater or equal than
  217. * threshold, or if the threshold is not set. Otherwise returns false.
  218. */
  219. public function isAsSevereAsThreshold($level) {
  220. if($this->threshold === null) {
  221. return true;
  222. }
  223. return $level->isGreaterOrEqual($this->getThreshold());
  224. }
  225. /**
  226. * Prepares the appender for logging.
  227. *
  228. * Derived appenders should override this method if option structure
  229. * requires it.
  230. */
  231. public function activateOptions() {
  232. $this->closed = false;
  233. }
  234. /**
  235. * Forwards the logging event to the destination.
  236. *
  237. * Derived appenders should implement this method to perform actual logging.
  238. *
  239. * @param LoggerLoggingEvent $event
  240. */
  241. abstract protected function append(LoggerLoggingEvent $event);
  242. /**
  243. * Releases any resources allocated by the appender.
  244. *
  245. * Derived appenders should override this method to perform proper closing
  246. * procedures.
  247. */
  248. public function close() {
  249. $this->closed = true;
  250. }
  251. /** Triggers a warning for this logger with the given message. */
  252. protected function warn($message) {
  253. $id = get_class($this) . (empty($this->name) ? '' : ":{$this->name}");
  254. trigger_error("log4php: [$id]: $message", E_USER_WARNING);
  255. }
  256. }