library/log4php/appenders/LoggerAppenderSocket.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     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 /**
       
    20  * LoggerAppenderSocket appends to a network socket.
       
    21  *
       
    22  * ## Configurable parameters: ##
       
    23  *
       
    24  * - **remoteHost** - Target remote host.
       
    25  * - **port** - Target port (optional, defaults to 4446).
       
    26  * - **timeout** - Connection timeout in seconds (optional, defaults to
       
    27  *     'default_socket_timeout' from php.ini)
       
    28  *
       
    29  * The socket will by default be opened in blocking mode.
       
    30  *
       
    31  * @version $Revision: 1337820 $
       
    32  * @package log4php
       
    33  * @subpackage appenders
       
    34  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
       
    35  * @link http://logging.apache.org/log4php/docs/appenders/socket.html Appender documentation
       
    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         return new LoggerLayoutSerialized();
       
    58     }
       
    59 
       
    60     public function activateOptions() {
       
    61         if (empty($this->remoteHost)) {
       
    62             $this->warn("Required parameter [remoteHost] not set. Closing appender.");
       
    63             $this->closed = true;
       
    64             return;
       
    65         }
       
    66 
       
    67         if (empty($this->timeout)) {
       
    68             $this->timeout = ini_get("default_socket_timeout");
       
    69         }
       
    70 
       
    71         $this->closed = false;
       
    72     }
       
    73 
       
    74     public function append(LoggerLoggingEvent $event) {
       
    75         $socket = fsockopen($this->remoteHost, $this->port, $errno, $errstr, $this->timeout);
       
    76         if ($socket === false) {
       
    77             $this->warn("Could not open socket to {$this->remoteHost}:{$this->port}. Closing appender.");
       
    78             $this->closed = true;
       
    79             return;
       
    80         }
       
    81 
       
    82         if (false === fwrite($socket, $this->layout->format($event))) {
       
    83             $this->warn("Error writing to socket. Closing appender.");
       
    84             $this->closed = true;
       
    85         }
       
    86         fclose($socket);
       
    87     }
       
    88 
       
    89     // ******************************************
       
    90     // *** Accessor methods                   ***
       
    91     // ******************************************
       
    92 
       
    93     /** Sets the target host. */
       
    94     public function setRemoteHost($hostname) {
       
    95         $this->setString('remoteHost', $hostname);
       
    96     }
       
    97 
       
    98     /** Sets the target port */
       
    99     public function setPort($port) {
       
   100         $this->setPositiveInteger('port', $port);
       
   101     }
       
   102 
       
   103     /** Sets the timeout. */
       
   104     public function setTimeout($timeout) {
       
   105         $this->setPositiveInteger('timeout', $timeout);
       
   106     }
       
   107 
       
   108     /** Returns the target host. */
       
   109     public function getRemoteHost() {
       
   110         return $this->getRemoteHost();
       
   111     }
       
   112 
       
   113     /** Returns the target port. */
       
   114     public function getPort() {
       
   115         return $this->port;
       
   116     }
       
   117 
       
   118     /** Returns the timeout */
       
   119     public function getTimeout() {
       
   120         return $this->timeout;
       
   121     }
       
   122 }