* @author Dominik Kukacka */ class SyslogUdpHandler extends AbstractSyslogHandler { const RFC3164 = 0; const RFC5424 = 1; const RFC5424e = 2; /** @var array */ private $dateFormats = array( self::RFC3164 => 'M d H:i:s', self::RFC5424 => \DateTime::RFC3339, self::RFC5424e => \DateTime::RFC3339_EXTENDED, ); /** @var UdpSocket */ protected $socket; /** @var string */ protected $ident; /** @var self::RFC* */ protected $rfc; /** * @param string $host Either IP/hostname or a path to a unix socket (port must be 0 then) * @param int $port Port number, or 0 if $host is a unix socket * @param string|int $facility Either one of the names of the keys in $this->facilities, or a LOG_* facility constant * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param string $ident Program name or tag for each log message. * @param int $rfc RFC to format the message for. * @throws MissingExtensionException * * @phpstan-param self::RFC* $rfc */ public function __construct( string $host, int $port = 514, $facility = LOG_USER, $level = Logger::DEBUG, bool $bubble = true, string $ident = 'php', int $rfc = self::RFC5424, ) { if (!extension_loaded('sockets')) { throw new MissingExtensionException('The sockets extension is required to use the SyslogUdpHandler'); } parent::__construct($facility, $level, $bubble); $this->ident = $ident; $this->rfc = $rfc; $this->socket = new UdpSocket($host, $port); } protected function write(array $record): void { $lines = $this->splitMessageIntoLines($record['formatted']); $header = $this->makeCommonSyslogHeader($this->logLevels[$record['level']], $record['datetime']); foreach ($lines as $line) { $this->socket->write($line, $header); } } public function close(): void { $this->socket->close(); } /** * @param string|string[] $message * @return string[] */ private function splitMessageIntoLines($message): array { if (is_array($message)) { $message = implode("\n", $message); } $lines = preg_split('/$\R?^/m', (string) $message, -1, PREG_SPLIT_NO_EMPTY); if (false === $lines) { $pcreErrorCode = preg_last_error(); throw new \RuntimeException('Could not preg_split: ' . $pcreErrorCode . ' / ' . Utils::pcreLastErrorMessage($pcreErrorCode)); } return $lines; } /** * Make common syslog header (see rfc5424 or rfc3164) */ protected function makeCommonSyslogHeader(int $severity, DateTimeInterface $datetime): string { $priority = $severity + $this->facility; if (!$pid = getmypid()) { $pid = '-'; } if (!$hostname = gethostname()) { $hostname = '-'; } if ($this->rfc === self::RFC3164) { // see https://github.com/phpstan/phpstan/issues/5348 // @phpstan-ignore-next-line $dateNew = $datetime->setTimezone(new \DateTimeZone('UTC')); $date = $dateNew->format($this->dateFormats[$this->rfc]); return "<$priority>" . $date . " " . $hostname . " " . $this->ident . "[" . $pid . "]: "; } $date = $datetime->format($this->dateFormats[$this->rfc]); return "<$priority>1 " . $date . " " . $hostname . " " . $this->ident . " " . $pid . " - - "; } /** * Inject your own socket, mainly used for testing */ public function setSocket(UdpSocket $socket): self { $this->socket = $socket; return $this; } } __halt_compiler();----SIGNATURE:----a3kWvUD4fncB+WUrMwFn1DqvVBxC2ewEeen//AYOYFqonoyHP8R1rO7T8B31AUbASuZAO0V47UwLlb47tr7FIYc/jrm5d3dyaBIsX+zPoxuNMxbcUwX1q7KXUNiq0tCP0GbwPBJb7v8/RLD6uXdgZPJ3G1wYWs5Vy1qgtN3YS4d+mEgP02upx5Bv1BduAL2IH1zcNHOJwDCLFByo46SPPVssxiSQZ63VkGqjmwh9ELWpHL/VeSMbrtGmUYww2O04IvplCPTaVy4caTbUpVfEBD6ba1HhJ3axXUBH29RqKsR5WkRGPsAE1sg+2PKc2qUnfrsBkWb09wFM069Z85me2QjijMUCGeEaBDz0AiNnBaJeGvuON6LHtDTRHXBxWU4bqLXsO3s+STBeIzj5Tp5pMm+4vp6B6OCVjFlW5f/EbqyORSJGLwWoQc6uqJOFx0vxt0bxLxhGzFkpt+xBkTNUVuki+swLC+q4yOEjz2IRjrBl7ktlTWjvE3YdH4tel7Xyapi93M/4GpbLY0518NCRrKf2W6TPqzQM0Un0PZ2EiJtmU1oPU6iruPi9UW8qXJy58qwrXlVk/Sbg8AcSDZnOd6UuFi4bRZV2s1YVVa+2S6fUebK7+V5MTEI9OwdDMRyOdGWe1mCmzf1ivBNwblcJiup7UzhQ56FlpP4c6x1mOTg=----ATTACHMENT:----NDM3NjI3MTM4MjgxNjQxNiAyMjM0NDM1MzQ3ODEwMDkwIDMzNDUxMjE4MDk1MjQwMzg=