Skip to content

Commit

Permalink
Add query logger utility to the page
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijastuden committed May 12, 2024
1 parent 5f5ff2c commit 6228191
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/QueryLogger/QueryLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the Feud project.
*
* (c) PhpCloud.org Core Team <[email protected]>. All rights reserved.
*/

declare(strict_types=1);

namespace ActiveCollab\DatabaseConnection\QueryLogger;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class QueryLogger implements QueryLoggerInterface
{
private array $queries = [];
private float $executionTime = 0.0;

public function __construct(
private LoggerInterface $logger,
private string $logLevel = LogLevel::DEBUG,
)
{
}

public function __invoke(string $querySql, float $queryExecutionTime): void
{
$this->logger->log(
$this->logLevel,
'Query {query} ran in {time}s.',
[
'query' => $querySql,
'time' => round($queryExecutionTime, 5),
],
);

$this->queries[] = $querySql;
$this->executionTime += $queryExecutionTime;
}

public function getNumberOfQueries(): int
{
return count($this->queries);
}

public function getExecutionTime(): float
{
return round($this->executionTime, 5);
}
}
17 changes: 17 additions & 0 deletions src/QueryLogger/QueryLoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Active Collab Bootstrap project.
*
* (c) A51 doo <[email protected]>. All rights reserved.
*/

declare(strict_types=1);

namespace ActiveCollab\DatabaseConnection\QueryLogger;

interface QueryLoggerInterface
{
public function getNumberOfQueries(): int;
public function getExecutionTime(): float;
}

0 comments on commit 6228191

Please sign in to comment.