-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Introduced a PSR-3 logger adapter #3156
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Logging; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Logs every query as a debug message | ||
*/ | ||
final class PsrAdapter implements SQLLogger | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the "adapter" here is essential since unlike other implementations, this class just converts calls from one API to another. While the SQL part is implied since the DBAL doesn't log anything else than SQL. |
||
{ | ||
/** @var LoggerInterface */ | ||
private $logger; | ||
|
||
public function __construct(LoggerInterface $logger) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This requires an addition to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’s not a hard requirement. Do you want to add a suggestion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would need to be a hard requirement if we rely on its signature in any way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We rely on this signature in the same way as on signatures of PDO, Looks like we have different expectations from PSR-3 in DBAL. As I see it, it will just enable dumping debug information into an abstract channel as requested in #2911. We're not deprecating |
||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function startQuery($sql, array $params = null, array $types = null) | ||
{ | ||
$this->logger->debug($sql, [ | ||
'params' => $params, | ||
'types' => $types, | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function stopQuery() | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably want to log There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way we’ll have two almost identical consecutive messages per query. Without precise timestamps it will be just a noise. Want to elaborate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but the start/end are vital to profile queries. Filtering them out would be trivial here, while adding them wouldn't be as easy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, using a logging interface for profiling is an abuse. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Logging; | ||
|
||
use Doctrine\DBAL\ParameterType; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LoggerInterface; | ||
use function interface_exists; | ||
|
||
class PsrAdapterTest extends TestCase | ||
{ | ||
public function testLogging() | ||
{ | ||
if (! interface_exists(LoggerInterface::class)) { | ||
$this->markTestSkipped('PSR-3 LoggerInterface is unavailable'); | ||
} | ||
|
||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger->expects($this->once()) | ||
->method('debug') | ||
->with('SELECT name FROM users WHERE id = ?', [ | ||
'params' => [1], | ||
'types' => [ParameterType::INTEGER], | ||
]); | ||
|
||
$adapter = new PsrAdapter($logger); | ||
$adapter->startQuery('SELECT name FROM users WHERE id = ?', [1], [ParameterType::INTEGER]); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably no longer true
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?