3 * log4php is a PHP port of the log4j java logging package.
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>
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>
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>
17 * @subpackage appenders
23 if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
25 define('LOG4PHP_LOGGER_APPENDER_SOCKET_DEFAULT_PORT', 4446);
26 define('LOG4PHP_LOGGER_APPENDER_SOCKET_DEFAULT_TIMEOUT', 30);
28 require_once(LOG4PHP_DIR . '/LoggerAppenderSkeleton.php');
29 require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
30 require_once(LOG4PHP_DIR . '/LoggerLayout.php');
31 require_once(LOG4PHP_DIR . '/LoggerLog.php');
34 * Serialize events and send them to a network socket.
36 * Parameters are {@link $remoteHost}, {@link $port}, {@link $timeout},
37 * {@link $locationInfo}, {@link $useXml} and {@link $log4jNamespace}.
39 * @author VxR <vxr@vxr.it>
40 * @version $Revision: 2 $
42 * @subpackage appenders
44 class LoggerAppenderSocket extends LoggerAppenderSkeleton {
47 * @var mixed socket connection resource
53 * Target host. On how to define remote hostaname see
54 * {@link PHP_MANUAL#fsockopen}
60 * @var integer the network port.
62 var $port = LOG4PHP_LOGGER_APPENDER_SOCKET_DEFAULT_PORT;
65 * @var boolean get event's location info.
67 var $locationInfo = false;
70 * @var integer connection timeout
72 var $timeout = LOG4PHP_LOGGER_APPENDER_SOCKET_DEFAULT_TIMEOUT;
75 * @var boolean output events via {@link LoggerXmlLayout}
80 * @var boolean forward this option to {@link LoggerXmlLayout}.
81 * Ignored if {@link $useXml} is <i>false</i>.
83 var $log4jNamespace = false;
86 * @var LoggerXmlLayout
89 var $xmlLayout = null;
95 var $requiresLayout = false;
100 * @param string $name appender name
102 function LoggerAppenderSocket($name)
104 $this->LoggerAppenderSkeleton($name);
108 * Create a socket connection using defined parameters
110 function activateOptions()
112 LoggerLog::debug("LoggerAppenderSocket::activateOptions() creating a socket...");
115 $this->sp = @fsockopen($this->getRemoteHost(), $this->getPort(), $errno, $errstr, $this->getTimeout());
117 LoggerLog::debug("LoggerAppenderSocket::activateOptions() socket error [$errno] $errstr");
118 $this->closed = true;
120 LoggerLog::debug("LoggerAppenderSocket::activateOptions() socket created [".$this->sp."]");
121 if ($this->getUseXml()) {
122 $this->xmlLayout = LoggerLayout::factory('LoggerXmlLayout');
123 if ($this->xmlLayout === null) {
124 LoggerLog::debug("LoggerAppenderSocket::activateOptions() useXml is true but layout is null");
125 $this->setUseXml(false);
127 $this->xmlLayout->setLocationInfo($this->getLocationInfo());
128 $this->xmlLayout->setLog4jNamespace($this->getLog4jNamespace());
129 $this->xmlLayout->activateOptions();
132 $this->closed = false;
139 $this->closed = true;
145 function getHostname()
147 return $this->getRemoteHost();
153 function getLocationInfo()
155 return $this->locationInfo;
161 function getLog4jNamespace()
163 return $this->log4jNamespace;
174 function getRemoteHost()
176 return $this->remoteHost;
182 function getTimeout()
184 return $this->timeout;
192 return $this->useXml;
203 * @deprecated Please, use {@link setRemoteHost}
205 function setHostname($hostname)
207 $this->setRemoteHost($hostname);
213 function setLocationInfo($flag)
215 $this->locationInfo = LoggerOptionConverter::toBoolean($flag, $this->getLocationInfo());
221 function setLog4jNamespace($flag)
223 $this->log4jNamespace = LoggerOptionConverter::toBoolean($flag, $this->getLog4jNamespace());
229 function setPort($port)
231 $port = LoggerOptionConverter::toInt($port, 0);
232 if ($port > 0 and $port < 65535)
239 function setRemoteHost($hostname)
241 $this->remoteHost = $hostname;
247 function setTimeout($timeout)
249 $this->timeout = LoggerOptionConverter::toInt($timeout, $this->getTimeout());
255 function setUseXml($flag)
257 $this->useXml = LoggerOptionConverter::toBoolean($flag, $this->getUseXml());
261 * @param LoggerLoggingEvent
263 function append($event)
267 LoggerLog::debug("LoggerAppenderSocket::append()");
269 if ($this->getLocationInfo())
270 $event->getLocationInfo();
272 if (!$this->getUseXml()) {
273 $sEvent = serialize($event);
274 @fwrite($this->sp, $sEvent, strlen($sEvent));
276 @fwrite($this->sp, $this->xmlLayout->format($event));
279 // not sure about it...