LoggerAppenderSocket.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. * Serialize events and send them to a network socket.
  22. *
  23. * This appender can be configured by changing the following attributes:
  24. *
  25. * - locationInfo - Sets the location info for the xml layout (true or false)
  26. * - log4jNamespace - Sets the namespace for log4j (true or false)
  27. * - port - Sets the port of the socket.
  28. * - remoteHost - Sets the remote host
  29. * - timeout - Sets the timeout in ms
  30. * - useXml - true, if xml should be transmitted.
  31. * false, if a serialized php object should be transmitted
  32. *
  33. * Parameters are {@link $remoteHost}, {@link $port}, {@link $timeout},
  34. * {@link $locationInfo}, {@link $useXml} and {@link $log4jNamespace}.
  35. *
  36. * An example:
  37. *
  38. * {@example ../../examples/php/appender_socket.php 19}
  39. *
  40. * {@example ../../examples/resources/appender_socket.properties 18}
  41. *
  42. * @version $Revision: 883108 $
  43. * @package log4php
  44. * @subpackage appenders
  45. */
  46. class LoggerAppenderSocket extends LoggerAppender {
  47. /**
  48. * @var mixed socket connection resource
  49. * @access private
  50. */
  51. private $sp = false;
  52. /**
  53. * Target host. On how to define remote hostaname see
  54. * {@link PHP_MANUAL#fsockopen}
  55. * @var string
  56. */
  57. private $remoteHost = '';
  58. /**
  59. * @var integer the network port.
  60. */
  61. private $port = 4446;
  62. /**
  63. * @var boolean get event's location info.
  64. */
  65. private $locationInfo = false;
  66. /**
  67. * @var integer connection timeout
  68. */
  69. private $timeout = 30;
  70. /**
  71. * @var boolean output events via {@link LoggerXmlLayout}
  72. */
  73. private $useXml = false;
  74. /**
  75. * @var boolean forward this option to {@link LoggerXmlLayout}.
  76. * Ignored if {@link $useXml} is <i>false</i>.
  77. */
  78. private $log4jNamespace = false;
  79. /**
  80. * @var LoggerXmlLayout
  81. * @access private
  82. */
  83. private $xmlLayout = null;
  84. /** @var indiciates if this appender should run in dry mode */
  85. private $dry = false;
  86. public function __destruct() {
  87. $this->close();
  88. }
  89. /**
  90. * Create a socket connection using defined parameters
  91. */
  92. public function activateOptions() {
  93. if(!$this->dry) {
  94. $this->sp = @fsockopen($this->getRemoteHost(), $this->getPort(), $errno, $errstr, $this->getTimeout());
  95. if ($this->sp === false) {
  96. throw new LoggerException("Could not open socket to ".$this->getRemoteHost().":".$this->getPort().": $errstr ($errno)");
  97. }
  98. }
  99. if($this->getUseXml()) {
  100. $this->xmlLayout = LoggerReflectionUtils::createObject('LoggerLayoutXml');
  101. if($this->xmlLayout === null) {
  102. $this->setUseXml(false);
  103. } else {
  104. $this->xmlLayout->setLocationInfo($this->getLocationInfo());
  105. $this->xmlLayout->setLog4jNamespace($this->getLog4jNamespace());
  106. $this->xmlLayout->activateOptions();
  107. }
  108. }
  109. $this->closed = false;
  110. }
  111. public function close() {
  112. if($this->closed != true) {
  113. if(!$this->dry and $this->sp !== false) {
  114. fclose($this->sp);
  115. }
  116. $this->closed = true;
  117. }
  118. }
  119. public function setDry($dry) {
  120. $this->dry = $dry;
  121. }
  122. /**
  123. * @return string
  124. */
  125. public function getHostname() {
  126. return $this->getRemoteHost();
  127. }
  128. /**
  129. * @return boolean
  130. */
  131. public function getLocationInfo() {
  132. return $this->locationInfo;
  133. }
  134. /**
  135. * @return boolean
  136. */
  137. public function getLog4jNamespace() {
  138. return $this->log4jNamespace;
  139. }
  140. /**
  141. * @return integer
  142. */
  143. public function getPort() {
  144. return $this->port;
  145. }
  146. public function getRemoteHost() {
  147. return $this->remoteHost;
  148. }
  149. /**
  150. * @return integer
  151. */
  152. public function getTimeout() {
  153. return $this->timeout;
  154. }
  155. /**
  156. * @var boolean
  157. */
  158. public function getUseXml() {
  159. return $this->useXml;
  160. }
  161. public function reset() {
  162. $this->close();
  163. parent::reset();
  164. }
  165. /**
  166. * @param mixed
  167. */
  168. public function setLocationInfo($flag) {
  169. $this->locationInfo = LoggerOptionConverter::toBoolean($flag, $this->getLocationInfo());
  170. }
  171. /**
  172. * @param mixed
  173. */
  174. public function setLog4jNamespace($flag) {
  175. $this->log4jNamespace = LoggerOptionConverter::toBoolean($flag, $this->getLog4jNamespace());
  176. }
  177. /**
  178. * @param integer
  179. */
  180. public function setPort($port) {
  181. $port = LoggerOptionConverter::toInt($port, 0);
  182. if($port > 0 and $port < 65535) {
  183. $this->port = $port;
  184. }
  185. }
  186. /**
  187. * @param string
  188. */
  189. public function setRemoteHost($hostname) {
  190. $this->remoteHost = $hostname;
  191. }
  192. /**
  193. * @param integer
  194. */
  195. public function setTimeout($timeout) {
  196. $this->timeout = LoggerOptionConverter::toInt($timeout, $this->getTimeout());
  197. }
  198. /**
  199. * @param mixed
  200. */
  201. public function setUseXml($flag) {
  202. $this->useXml = LoggerOptionConverter::toBoolean($flag, $this->getUseXml());
  203. }
  204. public function append(LoggerLoggingEvent $event) {
  205. if($this->sp || $this->dry) {
  206. if($this->getLocationInfo()) {
  207. $event->getLocationInformation();
  208. }
  209. if(!$this->getUseXml()) {
  210. $sEvent = serialize($event);
  211. if(!$this->dry) {
  212. fwrite($this->sp, $sEvent, strlen($sEvent));
  213. } else {
  214. echo "DRY MODE OF SOCKET APPENDER: ".$sEvent;
  215. }
  216. } else {
  217. if(!$this->dry) {
  218. fwrite($this->sp, $this->xmlLayout->format($event));
  219. } else {
  220. echo "DRY MODE OF SOCKET APPENDER: ".$this->xmlLayout->format($event);
  221. }
  222. }
  223. // not sure about it...
  224. if(!$this->dry) {
  225. fflush($this->sp);
  226. }
  227. }
  228. }
  229. }