Skip to content

Commit

Permalink
Ensure exceptions during log sending does not result in RuntimeExcept…
Browse files Browse the repository at this point in the history
…ion by default
  • Loading branch information
PetrHeinz committed Jun 26, 2024
1 parent 38e4016 commit 16e8541
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
8 changes: 5 additions & 3 deletions src/Monolog/LogtailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
}
Expand Down
35 changes: 31 additions & 4 deletions src/Monolog/SynchronousLogtailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,42 @@
/**
* 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
* @param bool $bubble
* @param string $endpoint
* @param int $connectionTimeoutMs
* @param int $timeoutMs
* @param bool throwExceptions
*/
public function __construct(
$sourceToken,
$level = \Monolog\Logger::DEBUG,
$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);
Expand All @@ -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);
}
}
}

/**
Expand All @@ -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);
}
}
}

/**
Expand Down

0 comments on commit 16e8541

Please sign in to comment.