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

/lib/log4php/appenders/LoggerAppenderSyslog.php

http://sabre-zarafa.googlecode.com/
PHP | 322 lines | 114 code | 33 blank | 175 comment | 14 complexity | 6f712febe9cfa5e4fbe0ac075e992636 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. * 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. * Log events to a system log using the {@link PHP_MANUAL#syslog} function.
  22. *
  23. * This appenders requires a layout.
  24. *
  25. * Configurable parameters:
  26. *
  27. * - ident - The ident of the syslog message.
  28. * - priority - The priority for the syslog message (used when overriding priority).
  29. * - facility - The facility for the syslog message
  30. * - overridePriority - If set to true, the message priority will always use
  31. * the value defined in {@link $priority}, otherwise the
  32. * priority will be determined by the message's log level.
  33. * - option - The option value for the syslog message.
  34. *
  35. * Recognised syslog options are:
  36. * - CONS - if there is an error while sending data to the system logger, write directly to the system console
  37. * - NDELAY - open the connection to the logger immediately
  38. * - ODELAY - delay opening the connection until the first message is logged (default)
  39. * - PERROR - print log message also to standard error
  40. * - PID - include PID with each message
  41. *
  42. * Multiple options can be set by delimiting them with a pipe character,
  43. * e.g.: "CONS|PID|PERROR".
  44. *
  45. * Recognised syslog priorities are:
  46. * - EMERG
  47. * - ALERT
  48. * - CRIT
  49. * - ERR
  50. * - WARNING
  51. * - NOTICE
  52. * - INFO
  53. * - DEBUG
  54. *
  55. * Levels are mapped as follows:
  56. * - <b>FATAL</b> to LOG_ALERT
  57. * - <b>ERROR</b> to LOG_ERR
  58. * - <b>WARN</b> to LOG_WARNING
  59. * - <b>INFO</b> to LOG_INFO
  60. * - <b>DEBUG</b> to LOG_DEBUG
  61. * - <b>TRACE</b> to LOG_DEBUG
  62. *
  63. * An example:
  64. *
  65. * {@example ../../examples/php/appender_syslog.php 19}
  66. *
  67. * {@example ../../examples/resources/appender_syslog.properties 18}
  68. *
  69. * @version $Revision: 31 $
  70. * @package log4php
  71. * @subpackage appenders
  72. */
  73. class LoggerAppenderSyslog extends LoggerAppender {
  74. /**
  75. * The ident string is added to each message. Typically the name of your application.
  76. *
  77. * @var string
  78. */
  79. protected $ident = "Apache log4php";
  80. /**
  81. * The syslog priority to use when overriding priority. This setting is
  82. * required if {@link overridePriority} is set to true.
  83. *
  84. * @var string
  85. */
  86. protected $priority;
  87. /**
  88. * The option used when opening the syslog connection.
  89. *
  90. * @var string
  91. */
  92. protected $option = 'PID|CONS';
  93. /**
  94. * The facility value indicates the source of the message.
  95. *
  96. * @var string
  97. */
  98. protected $facility = 'USER';
  99. /**
  100. * If set to true, the message priority will always use the value defined
  101. * in {@link $priority}, otherwise the priority will be determined by the
  102. * message's log level.
  103. *
  104. * @var string
  105. */
  106. protected $overridePriority = false;
  107. /**
  108. * Holds the int value of the {@link $priority}.
  109. * @var int
  110. */
  111. private $intPriority;
  112. /**
  113. * Holds the int value of the {@link $facility}.
  114. * @var int
  115. */
  116. private $intFacility;
  117. /**
  118. * Holds the int value of the {@link $option}.
  119. * @var int
  120. */
  121. private $intOption;
  122. /** Maps log4php levels to equivalent syslog priorities. */
  123. private $levelMap = array(
  124. LoggerLevel::TRACE => LOG_DEBUG,
  125. LoggerLevel::DEBUG => LOG_DEBUG,
  126. LoggerLevel::INFO => LOG_INFO,
  127. LoggerLevel::WARN => LOG_WARNING,
  128. LoggerLevel::ERROR => LOG_ERR,
  129. LoggerLevel::FATAL => LOG_ALERT,
  130. );
  131. /**
  132. * Sets the {@link $ident}.
  133. *
  134. * @param string $ident
  135. */
  136. public function setIdent($ident) {
  137. $this->ident = $ident;
  138. }
  139. /**
  140. * Sets the {@link $priority}.
  141. *
  142. * @param string $priority
  143. */
  144. public function setPriority($priority) {
  145. $this->priority = $priority;
  146. }
  147. /**
  148. * Sets the {@link $facility}.
  149. *
  150. * @param string $facility
  151. */
  152. public function setFacility($facility) {
  153. $this->facility = $facility;
  154. }
  155. /**
  156. * Sets the {@link $overridePriority}.
  157. *
  158. * @param string $overridePriority
  159. */
  160. public function setOverridePriority($overridePriority) {
  161. $this->overridePriority = $overridePriority;
  162. }
  163. /**
  164. * Sets the {@link $option}.
  165. *
  166. * @param string $option
  167. */
  168. public function setOption($option) {
  169. $this->option = $option;
  170. }
  171. /**
  172. * Returns the {@link $ident}.
  173. *
  174. * @return string $ident
  175. */
  176. public function getIdent() {
  177. return $this->ident;
  178. }
  179. /**
  180. * Returns the {@link $priority}.
  181. *
  182. * @return string
  183. */
  184. public function getPriority() {
  185. return $this->priority;
  186. }
  187. /**
  188. * Returns the {@link $facility}.
  189. *
  190. * @return string
  191. */
  192. public function getFacility() {
  193. return $this->facility;
  194. }
  195. /**
  196. * Returns the {@link $overridePriority}.
  197. *
  198. * @return string
  199. */
  200. public function getOverridePriority() {
  201. return $this->overridePriority;
  202. }
  203. /**
  204. * Returns the {@link $option}.
  205. *
  206. * @return string
  207. */
  208. public function getOption() {
  209. return $this->option;
  210. }
  211. public function activateOptions() {
  212. $this->intPriority = $this->parsePriority();
  213. $this->intOption = $this->parseOption();
  214. $this->intFacility = $this->parseFacility();
  215. $this->closed = false;
  216. }
  217. public function close() {
  218. if($this->closed != true) {
  219. closelog();
  220. $this->closed = true;
  221. }
  222. }
  223. /**
  224. * Appends the event to syslog.
  225. *
  226. * Log is opened and closed each time because if it is not closed, it
  227. * can cause the Apache httpd server to log to whatever ident/facility
  228. * was used in openlog().
  229. *
  230. * @see http://www.php.net/manual/en/function.syslog.php#97843
  231. */
  232. public function append(LoggerLoggingEvent $event) {
  233. $priority = $this->getSyslogPriority($event->getLevel());
  234. $message = $this->layout->format($event);
  235. openlog($this->ident, $this->intOption, $this->intFacility);
  236. syslog($priority, $message);
  237. closelog();
  238. }
  239. /** Determines which syslog priority to use based on the given level. */
  240. private function getSyslogPriority(LoggerLevel $level) {
  241. if($this->overridePriority) {
  242. return $this->intPriority;
  243. }
  244. $int = $level->toInt();
  245. if (isset($this->levelMap[$int])) {
  246. return $this->levelMap[$int];
  247. } else {
  248. return LOG_DEBUG;
  249. }
  250. }
  251. /** Parses a syslog option string and returns the correspodning int value. */
  252. private function parseOption() {
  253. $value = 0;
  254. $options = explode('|', $this->option);
  255. foreach($options as $option) {
  256. if (!empty($option)) {
  257. $constant = "LOG_" . trim($option);
  258. if (defined($constant)) {
  259. $value |= constant($constant);
  260. } else {
  261. trigger_error("log4php: Invalid syslog option provided: $option. Whole option string: {$this->option}.", E_USER_WARNING);
  262. }
  263. }
  264. }
  265. return $value;
  266. }
  267. /** Parses the facility string and returns the corresponding int value. */
  268. private function parseFacility() {
  269. if (!empty($this->facility)) {
  270. $constant = "LOG_" . trim($this->facility);
  271. if (defined($constant)) {
  272. return constant($constant);
  273. } else {
  274. trigger_error("log4php: Invalid syslog facility provided: {$this->facility}.", E_USER_WARNING);
  275. }
  276. }
  277. }
  278. /** Parses the priority string and returns the corresponding int value. */
  279. private function parsePriority() {
  280. if (!empty($this->priority)) {
  281. $constant = "LOG_" . trim($this->priority);
  282. if (defined($constant)) {
  283. return constant($constant);
  284. } else {
  285. trigger_error("log4php: Invalid syslog priority provided: {$this->priority}.", E_USER_WARNING);
  286. }
  287. }
  288. }
  289. }