-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add query logger utility to the page
- Loading branch information
1 parent
5f5ff2c
commit 6228191
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |