GelfMessageFormatter.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\Formatter;
  11. use Monolog\Logger;
  12. use Gelf\Message;
  13. use Monolog\Utils;
  14. /**
  15. * Serializes a log message to GELF
  16. * @see http://docs.graylog.org/en/latest/pages/gelf.html
  17. *
  18. * @author Matt Lehner <mlehner@gmail.com>
  19. */
  20. class GelfMessageFormatter extends NormalizerFormatter
  21. {
  22. protected const DEFAULT_MAX_LENGTH = 32766;
  23. /**
  24. * @var string the name of the system for the Gelf log message
  25. */
  26. protected $systemName;
  27. /**
  28. * @var string a prefix for 'extra' fields from the Monolog record (optional)
  29. */
  30. protected $extraPrefix;
  31. /**
  32. * @var string a prefix for 'context' fields from the Monolog record (optional)
  33. */
  34. protected $contextPrefix;
  35. /**
  36. * @var int max length per field
  37. */
  38. protected $maxLength;
  39. /**
  40. * Translates Monolog log levels to Graylog2 log priorities.
  41. */
  42. private $logLevels = [
  43. Logger::DEBUG => 7,
  44. Logger::INFO => 6,
  45. Logger::NOTICE => 5,
  46. Logger::WARNING => 4,
  47. Logger::ERROR => 3,
  48. Logger::CRITICAL => 2,
  49. Logger::ALERT => 1,
  50. Logger::EMERGENCY => 0,
  51. ];
  52. public function __construct(?string $systemName = null, ?string $extraPrefix = null, string $contextPrefix = 'ctxt_', ?int $maxLength = null)
  53. {
  54. parent::__construct('U.u');
  55. $this->systemName = (is_null($systemName) || $systemName === '') ? gethostname() : $systemName;
  56. $this->extraPrefix = is_null($extraPrefix) ? '' : $extraPrefix;
  57. $this->contextPrefix = $contextPrefix;
  58. $this->maxLength = is_null($maxLength) ? self::DEFAULT_MAX_LENGTH : $maxLength;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function format(array $record): Message
  64. {
  65. if (isset($record['context'])) {
  66. $record['context'] = parent::format($record['context']);
  67. }
  68. if (isset($record['extra'])) {
  69. $record['extra'] = parent::format($record['extra']);
  70. }
  71. if (!isset($record['datetime'], $record['message'], $record['level'])) {
  72. throw new \InvalidArgumentException('The record should at least contain datetime, message and level keys, '.var_export($record, true).' given');
  73. }
  74. $message = new Message();
  75. $message
  76. ->setTimestamp($record['datetime'])
  77. ->setShortMessage((string) $record['message'])
  78. ->setHost($this->systemName)
  79. ->setLevel($this->logLevels[$record['level']]);
  80. // message length + system name length + 200 for padding / metadata
  81. $len = 200 + strlen((string) $record['message']) + strlen($this->systemName);
  82. if ($len > $this->maxLength) {
  83. $message->setShortMessage(Utils::substr($record['message'], 0, $this->maxLength));
  84. }
  85. if (isset($record['channel'])) {
  86. $message->setFacility($record['channel']);
  87. }
  88. if (isset($record['extra']['line'])) {
  89. $message->setLine($record['extra']['line']);
  90. unset($record['extra']['line']);
  91. }
  92. if (isset($record['extra']['file'])) {
  93. $message->setFile($record['extra']['file']);
  94. unset($record['extra']['file']);
  95. }
  96. foreach ($record['extra'] as $key => $val) {
  97. $val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
  98. $len = strlen($this->extraPrefix . $key . $val);
  99. if ($len > $this->maxLength) {
  100. $message->setAdditional($this->extraPrefix . $key, Utils::substr($val, 0, $this->maxLength));
  101. continue;
  102. }
  103. $message->setAdditional($this->extraPrefix . $key, $val);
  104. }
  105. foreach ($record['context'] as $key => $val) {
  106. $val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
  107. $len = strlen($this->contextPrefix . $key . $val);
  108. if ($len > $this->maxLength) {
  109. $message->setAdditional($this->contextPrefix . $key, Utils::substr($val, 0, $this->maxLength));
  110. continue;
  111. }
  112. $message->setAdditional($this->contextPrefix . $key, $val);
  113. }
  114. if (null === $message->getFile() && isset($record['context']['exception']['file'])) {
  115. if (preg_match("/^(.+):([0-9]+)$/", $record['context']['exception']['file'], $matches)) {
  116. $message->setFile($matches[1]);
  117. $message->setLine($matches[2]);
  118. }
  119. }
  120. return $message;
  121. }
  122. }