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

/MyVenture-BlackHawk/Library/Logging/Include/appenders/LoggerAppenderSocket.php

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