LogglyHandler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 Monolog\Logger;
  12. use Monolog\Formatter\FormatterInterface;
  13. use Monolog\Formatter\LogglyFormatter;
  14. use function array_key_exists;
  15. use CurlHandle;
  16. /**
  17. * Sends errors to Loggly.
  18. *
  19. * @author Przemek Sobstel <przemek@sobstel.org>
  20. * @author Adam Pancutt <adam@pancutt.com>
  21. * @author Gregory Barchard <gregory@barchard.net>
  22. */
  23. class LogglyHandler extends AbstractProcessingHandler
  24. {
  25. protected const HOST = 'logs-01.loggly.com';
  26. protected const ENDPOINT_SINGLE = 'inputs';
  27. protected const ENDPOINT_BATCH = 'bulk';
  28. /**
  29. * Caches the curl handlers for every given endpoint.
  30. *
  31. * @var resource[]|CurlHandle[]
  32. */
  33. protected $curlHandlers = [];
  34. protected $token;
  35. protected $tag = [];
  36. /**
  37. * @param string $token API token supplied by Loggly
  38. * @param string|int $level The minimum logging level to trigger this handler
  39. * @param bool $bubble Whether or not messages that are handled should bubble up the stack.
  40. *
  41. * @throws MissingExtensionException If the curl extension is missing
  42. */
  43. public function __construct(string $token, $level = Logger::DEBUG, bool $bubble = true)
  44. {
  45. if (!extension_loaded('curl')) {
  46. throw new MissingExtensionException('The curl extension is needed to use the LogglyHandler');
  47. }
  48. $this->token = $token;
  49. parent::__construct($level, $bubble);
  50. }
  51. /**
  52. * Loads and returns the shared curl handler for the given endpoint.
  53. *
  54. * @param string $endpoint
  55. *
  56. * @return resource|CurlHandle
  57. */
  58. protected function getCurlHandler(string $endpoint)
  59. {
  60. if (!array_key_exists($endpoint, $this->curlHandlers)) {
  61. $this->curlHandlers[$endpoint] = $this->loadCurlHandle($endpoint);
  62. }
  63. return $this->curlHandlers[$endpoint];
  64. }
  65. /**
  66. * Starts a fresh curl session for the given endpoint and returns its handler.
  67. *
  68. * @param string $endpoint
  69. *
  70. * @return resource|CurlHandle
  71. */
  72. private function loadCurlHandle(string $endpoint)
  73. {
  74. $url = sprintf("https://%s/%s/%s/", static::HOST, $endpoint, $this->token);
  75. $ch = curl_init();
  76. curl_setopt($ch, CURLOPT_URL, $url);
  77. curl_setopt($ch, CURLOPT_POST, true);
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  79. return $ch;
  80. }
  81. /**
  82. * @param string[]|string $tag
  83. */
  84. public function setTag($tag): self
  85. {
  86. $tag = !empty($tag) ? $tag : [];
  87. $this->tag = is_array($tag) ? $tag : [$tag];
  88. return $this;
  89. }
  90. /**
  91. * @param string[]|string $tag
  92. */
  93. public function addTag($tag): self
  94. {
  95. if (!empty($tag)) {
  96. $tag = is_array($tag) ? $tag : [$tag];
  97. $this->tag = array_unique(array_merge($this->tag, $tag));
  98. }
  99. return $this;
  100. }
  101. protected function write(array $record): void
  102. {
  103. $this->send($record["formatted"], static::ENDPOINT_SINGLE);
  104. }
  105. public function handleBatch(array $records): void
  106. {
  107. $level = $this->level;
  108. $records = array_filter($records, function ($record) use ($level) {
  109. return ($record['level'] >= $level);
  110. });
  111. if ($records) {
  112. $this->send($this->getFormatter()->formatBatch($records), static::ENDPOINT_BATCH);
  113. }
  114. }
  115. protected function send(string $data, string $endpoint): void
  116. {
  117. $ch = $this->getCurlHandler($endpoint);
  118. $headers = ['Content-Type: application/json'];
  119. if (!empty($this->tag)) {
  120. $headers[] = 'X-LOGGLY-TAG: '.implode(',', $this->tag);
  121. }
  122. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  123. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  124. Curl\Util::execute($ch, 5, false);
  125. }
  126. protected function getDefaultFormatter(): FormatterInterface
  127. {
  128. return new LogglyFormatter();
  129. }
  130. }