LogEntriesHandler.php 1.7 KB

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