PHP code example of tourze / doctrine-logger-bundle

1. Go to this page and download the library: Download tourze/doctrine-logger-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / doctrine-logger-bundle example snippets




return [
    // ...
    Tourze\DoctrineLoggerBundle\DoctrineLoggerBundle::class => ['all' => true],
];



use Tourze\DoctrineLoggerBundle\Service\QueryExecutionTimeLogger;

class MyService
{
    public function __construct(
        private QueryExecutionTimeLogger $queryLogger
    ) {}
    
    public function executeCustomQuery(string $sql, array $params): mixed
    {
        return $this->queryLogger->watch(
            'my_custom_query',
            $sql,
            $params,
            function() use ($sql, $params) {
                // Execute your query here
                // For example:
                // return $this->connection->executeQuery($sql, $params)
                //     ->fetchAllAssociative();
            }
        );
    }
}



use Tourze\DoctrineLoggerBundle\Service\QueryExecutionTimeLogger;

class CustomQueryLogger extends QueryExecutionTimeLogger
{
    public function __construct()
    {
        parent::__construct();
        // Set custom threshold to 500ms
        $this->executionTimeThreshold = 500;
    }
}



use Tourze\DoctrineLoggerBundle\Service\LogMiddleware;

// In your Doctrine configuration
$config = new Configuration();
$config->setMiddlewares([
    new LogMiddleware($queryLogger, $stopwatch),
    // Other middlewares...
]);