Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query backtrace logger #3513

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/Doctrine/DBAL/Logging/BacktraceLogger.php
@@ -0,0 +1,25 @@
<?php

namespace Doctrine\DBAL\Logging;

use const DEBUG_BACKTRACE_IGNORE_ARGS;
use function array_shift;
use function debug_backtrace;

class BacktraceLogger extends DebugStack
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd avoid coupling the BacktraceLogger to the existing DebugStack one, if possible

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, the whole point here is to add the backtrace into the data stored for each query. Not coupling it would defeat the point.

{
/**
* {@inheritdoc}
*/
public function startQuery($sql, ?array $params = null, ?array $types = null)
{
parent::startQuery($sql, $params, $types);

$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);

// skip first since it's always the current method
array_shift($backtrace);

$this->queries[$this->currentQuery]['backtrace'] = $backtrace;
}
}
24 changes: 24 additions & 0 deletions tests/Doctrine/Tests/DBAL/Logging/BacktraceLoggerTest.php
@@ -0,0 +1,24 @@
<?php

namespace Doctrine\Tests\DBAL\Logging;

use Doctrine\DBAL\Logging\BacktraceLogger;
use Doctrine\Tests\DbalTestCase;
use function current;

class BacktraceLoggerTest extends DbalTestCase
{
public function testBacktraceLogged()
{
$logger = new BacktraceLogger();

$logger->startQuery('SELECT column FROM table');

$currentQuery = current($logger->queries);

self::assertSame('SELECT column FROM table', $currentQuery['sql']);
self::assertNull($currentQuery['params']);
self::assertNull($currentQuery['types']);
self::assertIsArray($currentQuery['backtrace']);
}
}