diff --git a/src/Monolog/LogtailHandler.php b/src/Monolog/LogtailHandler.php index 7edf287..f51ccf6 100644 --- a/src/Monolog/LogtailHandler.php +++ b/src/Monolog/LogtailHandler.php @@ -43,7 +43,8 @@ class LogtailHandler extends BufferHandler * @param bool $flushOnOverflow If true, the buffer is flushed when the max size has been reached, by default oldest entries are discarded * @param int $connectionTimeoutMs The maximum time in milliseconds that you allow the connection phase to the server to take * @param int $timeoutMs The maximum time in milliseconds that you allow a transfer operation to take - * @param int|null alwaysFlushAfterMs The time in milliseconds after which next log record will trigger flushing all logs. Null to disable. + * @param int|null $alwaysFlushAfterMs The time in milliseconds after which next log record will trigger flushing all logs. Null to disable. + * @param bool $throwExceptions Whether to throw exceptions when sending logs fails. */ public function __construct( $sourceToken, @@ -54,9 +55,10 @@ public function __construct( $flushOnOverflow = self::DEFAULT_FLUSH_ON_OVERFLOW, $connectionTimeoutMs = LogtailClient::DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS, $timeoutMs = LogtailClient::DEFAULT_TIMEOUT_MILLISECONDS, - $alwaysFlushAfterMs = self::DEFAULT_ALWAYS_FLUSH_AFTER_MILLISECONDS + $alwaysFlushAfterMs = self::DEFAULT_ALWAYS_FLUSH_AFTER_MILLISECONDS, + $throwExceptions = SynchronousLogtailHandler::DEFAULT_THROW_EXCEPTION ) { - parent::__construct(new SynchronousLogtailHandler($sourceToken, $level, $bubble, $endpoint, $connectionTimeoutMs, $timeoutMs), $bufferLimit, $level, $bubble, $flushOnOverflow); + parent::__construct(new SynchronousLogtailHandler($sourceToken, $level, $bubble, $endpoint, $connectionTimeoutMs, $timeoutMs, $throwExceptions), $bufferLimit, $level, $bubble, $flushOnOverflow); $this->alwaysFlushAfterMs = $alwaysFlushAfterMs; $this->setHighResolutionTimeOfLastFlush(); } diff --git a/src/Monolog/SynchronousLogtailHandler.php b/src/Monolog/SynchronousLogtailHandler.php index 2a43922..8a1a13b 100644 --- a/src/Monolog/SynchronousLogtailHandler.php +++ b/src/Monolog/SynchronousLogtailHandler.php @@ -16,12 +16,20 @@ /** * Sends log to Logtail. */ -class SynchronousLogtailHandler extends \Monolog\Handler\AbstractProcessingHandler { +class SynchronousLogtailHandler extends \Monolog\Handler\AbstractProcessingHandler +{ + const DEFAULT_THROW_EXCEPTION = false; + /** * @var LogtailClient $client */ private $client; + /** + * @var bool $throwExceptions + */ + private $throwExceptions; + /** * @param string $sourceToken * @param int $level @@ -29,6 +37,7 @@ class SynchronousLogtailHandler extends \Monolog\Handler\AbstractProcessingHandl * @param string $endpoint * @param int $connectionTimeoutMs * @param int $timeoutMs + * @param bool throwExceptions */ public function __construct( $sourceToken, @@ -36,11 +45,13 @@ public function __construct( $bubble = LogtailHandler::DEFAULT_BUBBLE, $endpoint = LogtailClient::URL, $connectionTimeoutMs = LogtailClient::DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS, - $timeoutMs = LogtailClient::DEFAULT_TIMEOUT_MILLISECONDS + $timeoutMs = LogtailClient::DEFAULT_TIMEOUT_MILLISECONDS, + $throwExceptions = self::DEFAULT_THROW_EXCEPTION ) { parent::__construct($level, $bubble); $this->client = new LogtailClient($sourceToken, $endpoint, $connectionTimeoutMs, $timeoutMs); + $this->throwExceptions = $throwExceptions; $this->pushProcessor(new \Monolog\Processor\IntrospectionProcessor($level, ['Logtail\\'])); $this->pushProcessor(new \Monolog\Processor\WebProcessor); @@ -52,7 +63,15 @@ public function __construct( * @param array $record */ protected function write(array $record): void { - $this->client->send($record["formatted"]); + try { + $this->client->send($record["formatted"]); + } catch (Throwable $throwable) { + if ($this->throwExceptions) { + throw $throwable; + } else { + trigger_error("Failed to send a single log record to Better Stack because of " . $throwable, E_USER_WARNING); + } + } } /** @@ -62,7 +81,15 @@ protected function write(array $record): void { public function handleBatch(array $records): void { $formattedRecords = $this->getFormatter()->formatBatch($records); - $this->client->send($formattedRecords); + try { + $this->client->send($formattedRecords); + } catch (\Throwable $throwable) { + if ($this->throwExceptions) { + throw $throwable; + } else { + trigger_error("Failed to send " . count($records) . " log records to Better Stack because of " . $throwable, E_USER_WARNING); + } + } } /**