4 * This file is part of the Monolog package.
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Monolog\Handler\SyslogUdp;
16 const DATAGRAM_MAX_LENGTH = 65023;
22 public function __construct($ip, $port = 514)
26 $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
29 public function write($line, $header = "")
31 $this->send($this->assembleMessage($line, $header));
34 public function close()
36 if (is_resource($this->socket)) {
37 socket_close($this->socket);
42 protected function send($chunk)
44 if (!is_resource($this->socket)) {
45 throw new \LogicException('The UdpSocket to '.$this->ip.':'.$this->port.' has been closed and can not be written to anymore');
47 socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port);
50 protected function assembleMessage($line, $header)
52 $chunkSize = self::DATAGRAM_MAX_LENGTH - strlen($header);
54 return $header . substr($line, 0, $chunkSize);