InsightOpsHandler.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. /**
  13. * Inspired on LogEntriesHandler.
  14. *
  15. * @author Robert Kaufmann III <rok3@rok3.me>
  16. * @author Gabriel Machado <gabriel.ms1@hotmail.com>
  17. */
  18. class InsightOpsHandler extends SocketHandler
  19. {
  20. /**
  21. * @var string
  22. */
  23. protected $logToken;
  24. /**
  25. * @param string $token Log token supplied by InsightOps
  26. * @param string $region Region where InsightOps account is hosted. Could be 'us' or 'eu'.
  27. * @param bool $useSSL Whether or not SSL encryption should be used
  28. * @param string|int $level The minimum logging level to trigger this handler
  29. * @param bool $bubble Whether or not messages that are handled should bubble up the stack.
  30. *
  31. * @throws MissingExtensionException If SSL encryption is set to true and OpenSSL is missing
  32. */
  33. public function __construct(string $token, string $region = 'us', bool $useSSL = true, $level = Logger::DEBUG, bool $bubble = true)
  34. {
  35. if ($useSSL && !extension_loaded('openssl')) {
  36. throw new MissingExtensionException('The OpenSSL PHP plugin is required to use SSL encrypted connection for LogEntriesHandler');
  37. }
  38. $endpoint = $useSSL
  39. ? 'ssl://' . $region . '.data.logs.insight.rapid7.com:443'
  40. : $region . '.data.logs.insight.rapid7.com:80';
  41. parent::__construct($endpoint, $level, $bubble);
  42. $this->logToken = $token;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function generateDataStream(array $record): string
  48. {
  49. return $this->logToken . ' ' . $record['formatted'];
  50. }
  51. }