SyslogUdpHandler.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. private $dateFormats = array(
  26. self::RFC3164 => 'M d H:i:s',
  27. self::RFC5424 => \DateTime::RFC3339,
  28. self::RFC5424e => \DateTime::RFC3339_EXTENDED,
  29. );
  30. protected $socket;
  31. protected $ident;
  32. protected $rfc;
  33. /**
  34. * @param string $host Either IP/hostname or a path to a unix socket (port must be 0 then)
  35. * @param int $port Port number, or 0 if $host is a unix socket
  36. * @param string|int $facility Either one of the names of the keys in $this->facilities, or a LOG_* facility constant
  37. * @param string|int $level The minimum logging level at which this handler will be triggered
  38. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  39. * @param string $ident Program name or tag for each log message.
  40. * @param int $rfc RFC to format the message for.
  41. */
  42. public function __construct(string $host, int $port = 514, $facility = LOG_USER, $level = Logger::DEBUG, bool $bubble = true, string $ident = 'php', int $rfc = self::RFC5424)
  43. {
  44. parent::__construct($facility, $level, $bubble);
  45. $this->ident = $ident;
  46. $this->rfc = $rfc;
  47. $this->socket = new UdpSocket($host, $port);
  48. }
  49. protected function write(array $record): void
  50. {
  51. $lines = $this->splitMessageIntoLines($record['formatted']);
  52. $header = $this->makeCommonSyslogHeader($this->logLevels[$record['level']], $record['datetime']);
  53. foreach ($lines as $line) {
  54. $this->socket->write($line, $header);
  55. }
  56. }
  57. public function close(): void
  58. {
  59. $this->socket->close();
  60. }
  61. private function splitMessageIntoLines($message): array
  62. {
  63. if (is_array($message)) {
  64. $message = implode("\n", $message);
  65. }
  66. return preg_split('/$\R?^/m', (string) $message, -1, PREG_SPLIT_NO_EMPTY);
  67. }
  68. /**
  69. * Make common syslog header (see rfc5424 or rfc3164)
  70. */
  71. protected function makeCommonSyslogHeader(int $severity, DateTimeInterface $datetime): string
  72. {
  73. $priority = $severity + $this->facility;
  74. if (!$pid = getmypid()) {
  75. $pid = '-';
  76. }
  77. if (!$hostname = gethostname()) {
  78. $hostname = '-';
  79. }
  80. if ($this->rfc === self::RFC3164 && ($datetime instanceof \DateTimeImmutable || $datetime instanceof \DateTime)) {
  81. $dateNew = $datetime->setTimezone(new \DateTimeZone('UTC'));
  82. $date = $dateNew->format($this->dateFormats[$this->rfc]);
  83. }
  84. else {
  85. $date = $datetime->format($this->dateFormats[$this->rfc]);
  86. }
  87. if ($this->rfc === self::RFC3164) {
  88. return "<$priority>" .
  89. $date . " " .
  90. $hostname . " " .
  91. $this->ident . "[" . $pid . "]: ";
  92. } else {
  93. return "<$priority>1 " .
  94. $date . " " .
  95. $hostname . " " .
  96. $this->ident . " " .
  97. $pid . " - - ";
  98. }
  99. }
  100. /**
  101. * Inject your own socket, mainly used for testing
  102. */
  103. public function setSocket(UdpSocket $socket): self
  104. {
  105. $this->socket = $socket;
  106. return $this;
  107. }
  108. }