Source of file NetworkLogWriter.php
Size: 1,830 Bytes - Last Modified: 2021-12-23T10:07:34+00:00
/var/www/docs.ssmods.com/process/src/code/NetworkLogWriter.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 | <?php require_once THIRDPARTY_PATH . '/Zend/Log/Writer/Abstract.php'; /** * Class NetworkLogWriter * @author Christian Blank <c.blank@notthatbad.net> */ class NetworkLogWriter extends Zend_Log_Writer_Abstract { const UDP = "udp"; const TCP = "tcp"; /** * @var ILogAdapter */ protected $adapter = null; /** * @var string */ protected $protocol; /** * @param ILogAdapter $adapter * @param string $protocol */ public function __construct($adapter, $protocol) { $this->adapter = $adapter; $this->protocol = $protocol; } /** * Write a message to the log. * * @param array $event log data event * @return void */ protected function _write($event) { // format data if(!$this->_formatter) { $formatter = new LogstashFormatter(); $this->setFormatter($formatter); } $formattedData = $this->_formatter->format($event); $socket = $this->createSocket(); if($socket !== false) { fwrite($socket, $formattedData); fflush($socket); fclose($socket); } else { // report into local error logs, that the socket is not reachable from this host. error_log("{$this->protocol} socket for logging is not reachable"); } } /** * @return resource */ protected function createSocket() { $fp = @fsockopen("{$this->protocol}://{$this->adapter->host()}", $this->adapter->port()); return $fp; } /** * Construct a Zend_Log driver * * @param ILogAdapter $adapter * @return Zend_Log_FactoryInterface */ static public function factory($adapter) { return new NetworkLogWriter($adapter, self::UDP); } } |