OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / include / lib / log4php / appenders / LoggerAppenderSocket.php
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  */
23 if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
24
25 define('LOG4PHP_LOGGER_APPENDER_SOCKET_DEFAULT_PORT',       4446);
26 define('LOG4PHP_LOGGER_APPENDER_SOCKET_DEFAULT_TIMEOUT',    30);
27
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');
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: 2 $
41  * @package log4php
42  * @subpackage appenders
43  */ 
44 class 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                 }            
131             }
132             $this->closed = false;
133         }
134     }
135     
136     function close()
137     {
138         @fclose($this->sp);
139         $this->closed = true;
140     }
141
142     /**
143      * @return string
144      */
145     function getHostname()
146     {
147         return $this->getRemoteHost();
148     }
149     
150     /**
151      * @return boolean
152      */
153     function getLocationInfo()
154     {
155         return $this->locationInfo;
156     } 
157      
158     /**
159      * @return boolean
160      */
161     function getLog4jNamespace()
162     {
163         return $this->log4jNamespace;
164     }
165
166     /**
167      * @return integer
168      */
169     function getPort()
170     {
171         return $this->port;
172     }
173     
174     function getRemoteHost()
175     {
176         return $this->remoteHost;
177     }
178     
179     /**
180      * @return integer
181      */
182     function getTimeout()
183     {
184         return $this->timeout;
185     }
186     
187     /**
188      * @var boolean
189      */
190     function getUseXml()
191     {
192         return $this->useXml;
193     } 
194      
195     function reset()
196     {
197         $this->close();
198         parent::reset();
199     }
200
201     /**
202      * @param string
203      * @deprecated Please, use {@link setRemoteHost}
204      */
205     function setHostname($hostname)
206     {
207         $this->setRemoteHost($hostname);
208     }
209     
210     /**
211      * @param mixed
212      */
213     function setLocationInfo($flag)
214     {
215         $this->locationInfo = LoggerOptionConverter::toBoolean($flag, $this->getLocationInfo());
216     } 
217
218     /**
219      * @param mixed
220      */
221     function setLog4jNamespace($flag)
222     {
223         $this->log4jNamespace = LoggerOptionConverter::toBoolean($flag, $this->getLog4jNamespace());
224     } 
225             
226     /**
227      * @param integer
228      */
229     function setPort($port)
230     {
231         $port = LoggerOptionConverter::toInt($port, 0);
232         if ($port > 0 and $port < 65535)
233             $this->port = $port;    
234     }
235     
236     /**
237      * @param string
238      */
239     function setRemoteHost($hostname)
240     {
241         $this->remoteHost = $hostname;
242     }
243     
244     /**
245      * @param integer
246      */
247     function setTimeout($timeout)
248     {
249         $this->timeout = LoggerOptionConverter::toInt($timeout, $this->getTimeout());
250     }
251     
252     /**
253      * @param mixed
254      */
255     function setUseXml($flag)
256     {
257         $this->useXml = LoggerOptionConverter::toBoolean($flag, $this->getUseXml());
258     } 
259  
260     /**
261      * @param LoggerLoggingEvent
262      */
263     function append($event)
264     {
265         if ($this->sp) {
266         
267             LoggerLog::debug("LoggerAppenderSocket::append()");
268             
269             if ($this->getLocationInfo())
270                 $event->getLocationInfo();
271         
272             if (!$this->getUseXml()) {
273                 $sEvent = serialize($event);
274                 @fwrite($this->sp, $sEvent, strlen($sEvent));
275             } else {
276                 @fwrite($this->sp, $this->xmlLayout->format($event));
277             }            
278
279             // not sure about it...
280             @fflush ($this->sp);
281         } 
282     }
283 }
284
285 ?>