LoggerAppenderSocket.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. * @subpackage appenders
  18. */
  19. /**
  20. * @ignore
  21. */
  22. if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
  23. define('LOG4PHP_LOGGER_APPENDER_SOCKET_DEFAULT_PORT', 4446);
  24. define('LOG4PHP_LOGGER_APPENDER_SOCKET_DEFAULT_TIMEOUT', 30);
  25. require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php');
  26. require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
  27. require_once(LOG4PHP_DIR . '/LoggerLayout.php');
  28. require_once(LOG4PHP_DIR . '/LoggerLog.php');
  29. /**
  30. * Serialize events and send them to a network socket.
  31. *
  32. * Parameters are {@link $remoteHost}, {@link $port}, {@link $timeout},
  33. * {@link $locationInfo}, {@link $useXml} and {@link $log4jNamespace}.
  34. *
  35. * @author VxR <vxr@vxr.it>
  36. * @version $Revision: 1.1 $
  37. * @package log4php
  38. * @subpackage appenders
  39. */
  40. class LoggerAppenderSocket extends LoggerAppenderSkeleton {
  41. /**
  42. * @var mixed socket connection resource
  43. * @access private
  44. */
  45. var $sp = false;
  46. /**
  47. * Target host. On how to define remote hostaname see
  48. * {@link PHP_MANUAL#fsockopen}
  49. * @var string
  50. */
  51. var $remoteHost = '';
  52. /**
  53. * @var integer the network port.
  54. */
  55. var $port = LOG4PHP_LOGGER_APPENDER_SOCKET_DEFAULT_PORT;
  56. /**
  57. * @var boolean get event's location info.
  58. */
  59. var $locationInfo = false;
  60. /**
  61. * @var integer connection timeout
  62. */
  63. var $timeout = LOG4PHP_LOGGER_APPENDER_SOCKET_DEFAULT_TIMEOUT;
  64. /**
  65. * @var boolean output events via {@link LoggerXmlLayout}
  66. */
  67. var $useXml = false;
  68. /**
  69. * @var boolean forward this option to {@link LoggerXmlLayout}.
  70. * Ignored if {@link $useXml} is <i>false</i>.
  71. */
  72. var $log4jNamespace = false;
  73. /**
  74. * @var LoggerXmlLayout
  75. * @access private
  76. */
  77. var $xmlLayout = null;
  78. /**
  79. * @var boolean
  80. * @access private
  81. */
  82. var $requiresLayout = false;
  83. /**
  84. * Constructor
  85. *
  86. * @param string $name appender name
  87. */
  88. function LoggerAppenderSocket($name)
  89. {
  90. $this->LoggerAppenderSkeleton($name);
  91. }
  92. /**
  93. * Create a socket connection using defined parameters
  94. */
  95. function activateOptions()
  96. {
  97. LoggerLog::debug("LoggerAppenderSocket::activateOptions() creating a socket...");
  98. $errno = 0;
  99. $errstr = '';
  100. $this->sp = @fsockopen($this->getRemoteHost(), $this->getPort(), $errno, $errstr, $this->getTimeout());
  101. if ($errno) {
  102. LoggerLog::debug("LoggerAppenderSocket::activateOptions() socket error [$errno] $errstr");
  103. $this->closed = true;
  104. } else {
  105. LoggerLog::debug("LoggerAppenderSocket::activateOptions() socket created [".$this->sp."]");
  106. if ($this->getUseXml()) {
  107. $this->xmlLayout = LoggerLayout::factory('LoggerXmlLayout');
  108. if ($this->xmlLayout === null) {
  109. LoggerLog::debug("LoggerAppenderSocket::activateOptions() useXml is true but layout is null");
  110. $this->setUseXml(false);
  111. } else {
  112. $this->xmlLayout->setLocationInfo($this->getLocationInfo());
  113. $this->xmlLayout->setLog4jNamespace($this->getLog4jNamespace());
  114. $this->xmlLayout->activateOptions();
  115. }
  116. }
  117. $this->closed = false;
  118. }
  119. }
  120. function close()
  121. {
  122. @fclose($this->sp);
  123. $this->closed = true;
  124. }
  125. /**
  126. * @return string
  127. */
  128. function getHostname()
  129. {
  130. return $this->getRemoteHost();
  131. }
  132. /**
  133. * @return boolean
  134. */
  135. function getLocationInfo()
  136. {
  137. return $this->locationInfo;
  138. }
  139. /**
  140. * @return boolean
  141. */
  142. function getLog4jNamespace()
  143. {
  144. return $this->log4jNamespace;
  145. }
  146. /**
  147. * @return integer
  148. */
  149. function getPort()
  150. {
  151. return $this->port;
  152. }
  153. function getRemoteHost()
  154. {
  155. return $this->remoteHost;
  156. }
  157. /**
  158. * @return integer
  159. */
  160. function getTimeout()
  161. {
  162. return $this->timeout;
  163. }
  164. /**
  165. * @var boolean
  166. */
  167. function getUseXml()
  168. {
  169. return $this->useXml;
  170. }
  171. function reset()
  172. {
  173. $this->close();
  174. parent::reset();
  175. }
  176. /**
  177. * @param string
  178. * @deprecated Please, use {@link setRemoteHost}
  179. */
  180. function setHostname($hostname)
  181. {
  182. $this->setRemoteHost($hostname);
  183. }
  184. /**
  185. * @param mixed
  186. */
  187. function setLocationInfo($flag)
  188. {
  189. $this->locationInfo = LoggerOptionConverter::toBoolean($flag, $this->getLocationInfo());
  190. }
  191. /**
  192. * @param mixed
  193. */
  194. function setLog4jNamespace($flag)
  195. {
  196. $this->log4jNamespace = LoggerOptionConverter::toBoolean($flag, $this->getLog4jNamespace());
  197. }
  198. /**
  199. * @param integer
  200. */
  201. function setPort($port)
  202. {
  203. $port = LoggerOptionConverter::toInt($port, 0);
  204. if ($port > 0 and $port < 65535)
  205. $this->port = $port;
  206. }
  207. /**
  208. * @param string
  209. */
  210. function setRemoteHost($hostname)
  211. {
  212. $this->remoteHost = $hostname;
  213. }
  214. /**
  215. * @param integer
  216. */
  217. function setTimeout($timeout)
  218. {
  219. $this->timeout = LoggerOptionConverter::toInt($timeout, $this->getTimeout());
  220. }
  221. /**
  222. * @param mixed
  223. */
  224. function setUseXml($flag)
  225. {
  226. $this->useXml = LoggerOptionConverter::toBoolean($flag, $this->getUseXml());
  227. }
  228. /**
  229. * @param LoggerLoggingEvent
  230. */
  231. function append($event)
  232. {
  233. if ($this->sp) {
  234. LoggerLog::debug("LoggerAppenderSocket::append()");
  235. if ($this->getLocationInfo())
  236. $event->getLocationInfo();
  237. if (!$this->getUseXml()) {
  238. $sEvent = serialize($event);
  239. @fwrite($this->sp, $sEvent, strlen($sEvent));
  240. } else {
  241. @fwrite($this->sp, $this->xmlLayout->format($event));
  242. }
  243. // not sure about it...
  244. @fflush ($this->sp);
  245. }
  246. }
  247. }
  248. ?>