2
0

SyslogUdpHandler.php 3.5 KB

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