Verified Commit 9c9510fd authored by Bastien Durel's avatar Bastien Durel
Browse files

init

parents
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+1 −0
Original line number Diff line number Diff line
/vendor/

composer.json

0 → 100644
+21 −0
Original line number Diff line number Diff line
{
    "name": "data/dbal_log_time",
    "description": "Replacement for symfony's dbal logger, logging execution time",
    "type": "library",
    "require": {
        "symfony/doctrine-bridge": "^4.4.22|^5.2.7|^6.0"
    },
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "Data\\DbalLogTime\\": "src/"
        }
    },
    "authors": [
        {
            "name": "Bastien Durel",
            "email": "bastien.durel@data.fr"
        }
    ],
    "minimum-stability": "dev"
}

composer.lock

0 → 100644
+0 −0

File added.

Preview size limit exceeded, changes collapsed.

+49 −0
Original line number Diff line number Diff line
<?php

namespace Data\DbalLogTime;

use Psr\Log\LoggerInterface;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Bridge\Doctrine\Logger;

class DbalLogger extends Logger\DbalLogger {
    protected $start;
    
    function __construct(LoggerInterface $logger = null, Stopwatch $stopwatch = null) {
        parent::__construct($logger, $stopwatch);
        error_log("construct");
    }

    /**
     * {@inheritdoc}
     */
    public function startQuery($sql, array $params = null, array $types = null)
    {
        if (null !== $this->logger) {
            if (null === $this->stopwatch)
                $this->start = hrtime(false);
        }
        parent::startQuery($sql, $params, $types);
    }

    /**
     * {@inheritdoc}
     */
    public function stopQuery()
    {
        if (null !== $this->logger) {
            if (null === $this->stopwatch) {
                list ($sec, $nsec) = hrtime(false);
                $sd = $sec - $this->start[0];
                $nd = $nsec - $this->start[1];
                $duration = $sd + ($nd / 1000000000);
            }
            else {
                $ev = $this->stopwatch->getEvent('doctrine');
                $duration = $ev->getDuration() / 1000;
            }
            $this->logger->debug("Query duration: ${duration}s");
        }
        parent::stopQuery();
    }
}