1.5.4. Profiling and Logging

It is often useful to see what queries have been executed, where they were issued from in the codebase, and how long they took to complete. As such, ExtendedPdo comes with a profiler that logs to any PSR-3 implementation. The profiler defaults to a naive memory-based logger for debugging purposes. Using The Profiler

You can activate and deactivate the profiler using the Profiler::setActive() method. (Messages are not logged when the profiler is not active.)

You can then examine the log messages using the underlying log system; in the below example, we use the default MemoryLogger implementation.

// activate the profiler

// ...
// query(), fetch(), beginTransaction()/commit()/rollback() etc.
// ...

// now retrieve the array messages from the default memory logger:
$messages = $pdo->getProfiler()->getLogger()->getMessages();
print_r($messages); Other Logger Implementations

You can set your own profiler and PSR-3 logger implementation using the ExtendedPdo::setProfiler() method.

use Aura\Sql\Profiler\Profiler;

$myLogger = new Psr3LoggerImplementation();
$pdo->setProfiler(new Profiler($myLogger)); Profiler Log Messages

Profiler log messages, by default, will match this format:

{function} ({duration} seconds): {statement} {backtrace}

You can customize the message format using the Profiler::setLogFormat() method, like so:

$pdo->getProfiler()->setLogFormat("{duration}: {function} {statement}")

The context keys are: Profiler Log Level

By default, all messages are logged at the DEBUG level. You can change the logging level with the Profiler::setLogLevel() method:

use Psr\Log\LogLevel;


Likewise, you can get the current log level with Profiler::getLogLevel().