SyslogUdpHandler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use DateTimeInterface;
  12. use Monolog\Logger;
  13. use Monolog\Handler\SyslogUdp\UdpSocket;
  14. /**
  15. * A Handler for logging to a remote syslogd server.
  16. *
  17. * @author Jesper Skovgaard Nielsen <nulpunkt@gmail.com>
  18. * @author Dominik Kukacka <dominik.kukacka@gmail.com>
  19. */
  20. class SyslogUdpHandler extends AbstractSyslogHandler
  21. {
  22. const RFC3164 = 0;
  23. const RFC5424 = 1;
  24. const RFC5424e = 2;
  25. /** @var array<self::RFC*, string> */
  26. private $dateFormats = array(
  27. self::RFC3164 => 'M d H:i:s',
  28. self::RFC5424 => \DateTime::RFC3339,
  29. self::RFC5424e => \DateTime::RFC3339_EXTENDED,
  30. );
  31. /** @var UdpSocket */
  32. protected $socket;
  33. /** @var string */
  34. protected $ident;
  35. /** @var self::RFC* */
  36. protected $rfc;
  37. /**
  38. * @param string $host Either IP/hostname or a path to a unix socket (port must be 0 then)
  39. * @param int $port Port number, or 0 if $host is a unix socket
  40. * @param string|int $facility Either one of the names of the keys in $this->facilities, or a LOG_* facility constant
  41. * @param string|int $level The minimum logging level at which this handler will be triggered
  42. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  43. * @param string $ident Program name or tag for each log message.
  44. * @param int $rfc RFC to format the message for.
  45. *
  46. * @phpstan-param self::RFC* $rfc
  47. */
  48. public function __construct(string $host, int $port = 514, $facility = LOG_USER, $level = Logger::DEBUG, bool $bubble = true, string $ident = 'php', int $rfc = self::RFC5424)
  49. {
  50. parent::__construct($facility, $level, $bubble);
  51. $this->ident = $ident;
  52. $this->rfc = $rfc;
  53. $this->socket = new UdpSocket($host, $port);
  54. }
  55. protected function write(array $record): void
  56. {
  57. $lines = $this->splitMessageIntoLines($record['formatted']);
  58. $header = $this->makeCommonSyslogHeader($this->logLevels[$record['level']], $record['datetime']);
  59. foreach ($lines as $line) {
  60. $this->socket->write($line, $header);
  61. }
  62. }
  63. public function close(): void
  64. {
  65. $this->socket->close();
  66. }
  67. /**
  68. * @param string|string[] $message
  69. * @return string[]
  70. */
  71. private function splitMessageIntoLines($message): array
  72. {
  73. if (is_array($message)) {
  74. $message = implode("\n", $message);
  75. }
  76. return preg_split('/$\R?^/m', (string) $message, -1, PREG_SPLIT_NO_EMPTY);
  77. }
  78. /**
  79. * Make common syslog header (see rfc5424 or rfc3164)
  80. */
  81. protected function makeCommonSyslogHeader(int $severity, DateTimeInterface $datetime): string
  82. {
  83. $priority = $severity + $this->facility;
  84. if (!$pid = getmypid()) {
  85. $pid = '-';
  86. }
  87. if (!$hostname = gethostname()) {
  88. $hostname = '-';
  89. }
  90. if ($this->rfc === self::RFC3164 && ($datetime instanceof \DateTimeImmutable || $datetime instanceof \DateTime)) {
  91. $dateNew = $datetime->setTimezone(new \DateTimeZone('UTC'));
  92. $date = $dateNew->format($this->dateFormats[$this->rfc]);
  93. }
  94. else {
  95. $date = $datetime->format($this->dateFormats[$this->rfc]);
  96. }
  97. if ($this->rfc === self::RFC3164) {
  98. return "<$priority>" .
  99. $date . " " .
  100. $hostname . " " .
  101. $this->ident . "[" . $pid . "]: ";
  102. } else {
  103. return "<$priority>1 " .
  104. $date . " " .
  105. $hostname . " " .
  106. $this->ident . " " .
  107. $pid . " - - ";
  108. }
  109. }
  110. /**
  111. * Inject your own socket, mainly used for testing
  112. */
  113. public function setSocket(UdpSocket $socket): self
  114. {
  115. $this->socket = $socket;
  116. return $this;
  117. }
  118. }