Skip to content

Commit

Permalink
Add limit to BacktraceProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Nov 8, 2023
1 parent 605697e commit d7a56db
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/BacktraceProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ final class BacktraceProcessor implements ProcessorInterface
{
private const MONOLOG_VENDOR_DIRNAME = 'vendor/monolog/monolog';

public function __construct(private readonly int|false $limit = false)
{
}

public function __invoke(LogRecord $record): LogRecord
{
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
Expand All @@ -35,6 +39,10 @@ public function __invoke(LogRecord $record): LogRecord
}

$stack[] = \sprintf('%s(%d)', $file, $call['line']);

if ($this->limit && \count($stack) >= $this->limit) {
break;
}
}

$record['extra']['backtrace'] = $stack;
Expand Down
15 changes: 15 additions & 0 deletions tests/BacktraceProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,19 @@ public function testItWorks(): void
$this->assertStringContainsString(__FILE__, $record->extra['backtrace'][0]);
$this->assertStringMatchesFormat('%s/vendor/bin/phpunit(%d)', $record->extra['backtrace'][\count($record->extra['backtrace']) - 1]);
}

public function testLimit(): void
{
$processor = new BacktraceProcessor(2);

$handler = new TestHandler();
$handler->pushProcessor($processor);
$handler->handle($this->createRecord(Level::Notice));

$this->assertTrue($handler->hasNoticeRecords());
$record = $handler->getRecords()[0];

$this->assertArrayHasKey('backtrace', $record->extra);
$this->assertCount(2, $record->extra['backtrace']);
}
}

0 comments on commit d7a56db

Please sign in to comment.