source: branches/comu-ver2/data/module/log4php/php5/log4php/appenders/LoggerAppenderSocket.php @ 18220

Revision 18220, 6.6 KB checked in by yokkuns, 11 years ago (diff)

#149 ロガークラス作成

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