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 : /**
22 : * Appends events to a network socket.
23 : *
24 : * This appender can be configured by changing the following attributes:
25 : *
26 : * - remoteHost - Target remote host.
27 : * - port - Target port (optional, defaults to 4446).
28 : * - timeout - Connection timeout in seconds (optional, defaults to
29 : * 'default_socket_timeout' from php.ini)
30 : *
31 : * The socket will by default be opened in blocking mode.
32 : *
33 : * @version $Revision: 1213283 $
34 : * @package log4php
35 : * @subpackage appenders
36 : */
37 : class LoggerAppenderSocket extends LoggerAppender {
38 :
39 : /**
40 : * Target host.
41 : * @see http://php.net/manual/en/function.fsockopen.php
42 : */
43 : protected $remoteHost;
44 :
45 : /** Target port */
46 : protected $port = 4446;
47 :
48 : /** Connection timeout in ms. */
49 : protected $timeout;
50 :
51 : // ******************************************
52 : // *** Appender methods ***
53 : // ******************************************
54 :
55 : /** Override the default layout to use serialized. */
56 : public function getDefaultLayout() {
57 2 : return new LoggerLayoutSerialized();
58 : }
59 :
60 : public function activateOptions() {
61 1 : if (empty($this->remoteHost)) {
62 0 : $this->warn("Required parameter [remoteHost] not set. Closing appender.");
63 0 : $this->closed = true;
64 0 : return;
65 : }
66 :
67 1 : if (empty($this->timeout)) {
68 1 : $this->timeout = ini_get("default_socket_timeout");
69 1 : }
70 :
71 1 : $this->closed = false;
72 1 : }
73 :
74 : public function append(LoggerLoggingEvent $event) {
75 1 : $socket = fsockopen($this->remoteHost, $this->port, $errno, $errstr, $this->timeout);
76 1 : if ($socket === false) {
77 0 : $this->warn("Could not open socket to {$this->remoteHost}:{$this->port}. Closing appender.");
78 0 : $this->closed = true;
79 0 : return;
80 : }
81 :
82 1 : if (false === fwrite($socket, $this->layout->format($event))) {
83 0 : $this->warn("Error writing to socket. Closing appender.");
84 0 : $this->closed = true;
85 0 : }
86 1 : fclose($socket);
87 1 : }
88 :
89 : // ******************************************
90 : // *** Accessor methods ***
91 : // ******************************************
92 :
93 : /** Sets the target host. */
94 : public function setRemoteHost($hostname) {
95 1 : $this->setString('remoteHost', $hostname);
96 1 : }
97 :
98 : /** Sets the target port */
99 : public function setPort($port) {
100 1 : $this->setPositiveInteger('port', $port);
101 1 : }
102 :
103 : /** Sets the timeout. */
104 : public function setTimeout($timeout) {
105 0 : $this->setPositiveInteger('timeout', $timeout);
106 0 : }
107 :
108 : /** Returns the target host. */
109 : public function getRemoteHost() {
110 0 : return $this->getRemoteHost();
111 : }
112 :
113 : /** Returns the target port. */
114 : public function getPort() {
115 0 : return $this->port;
116 : }
117 :
118 : /** Returns the timeout */
119 : public function getTimeout() {
120 0 : return $this->timeout;
121 : }
122 : }
|