source: branches/comu-ver2/data/module/log4php/php4/log4php/appenders/LoggerAppenderSocket.php @ 18701

Revision 18701, 7.5 KB checked in by nanasess, 14 years ago (diff)

Copyright の更新(#601)

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