Skip to content

Commit

Permalink
Merge pull request #15 from holtkamp/patch-configurable-log-level
Browse files Browse the repository at this point in the history
Prevent using -1 as index
  • Loading branch information
abacaphiliac authored Jan 26, 2020
2 parents f5a7055 + 05aa32d commit 0bf991b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/LogLevelConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getApplicableLogLevel(float $durationInSeconds): ?string
return count($this->logLevelMapping) > 0 ? $this->determineApplicableLogLevel($durationInSeconds) : null;
}

private function determineApplicableLogLevel(float $durationInSeconds) : string
private function determineApplicableLogLevel(float $durationInSeconds) : ?string
{
$durationInMilliseconds = $durationInSeconds * 1000;

Expand All @@ -79,6 +79,6 @@ private function determineApplicableLogLevel(float $durationInSeconds) : string

$logLevels = array_keys($this->logLevelMapping);

return $logLevels[$key - 1]; //Now take the "previous" key
return $logLevels[$key - 1] ?? null;
}
}
10 changes: 7 additions & 3 deletions src/PsrSqlLoggerConfigurableLogLevels.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ final class PsrSqlLoggerConfigurableLogLevels implements SQLLogger
/** @var LoggerInterface */
private $logger;

/** @var string */
private $sql;

/** @var float */
private $start;

Expand All @@ -34,11 +37,11 @@ final class PsrSqlLoggerConfigurableLogLevels implements SQLLogger

public function __construct(
LoggerInterface $logger,
LogLevelConfiguration $logLevelMapping,
LogLevelConfiguration $logLevelConfiguration,
string $defaultLogLevel = LogLevel::INFO
) {
$this->logger = $logger;
$this->logLevelConfiguration = $logLevelMapping;
$this->logLevelConfiguration = $logLevelConfiguration;
$this->defaultLogLevel = $defaultLogLevel;
$this->startQueryCallable = $this->getStartQueryCallable($defaultLogLevel);
}
Expand All @@ -65,8 +68,8 @@ private function getLoggerCallable(string $level) : array

public function startQuery($sql, array $params = null, array $types = null) : void
{
$this->sql = $sql;
$this->queryId = uniqid('', true);

$this->start = microtime(true);

call_user_func($this->startQueryCallable, 'Query started', array_merge(
Expand Down Expand Up @@ -95,6 +98,7 @@ public function stopQuery() : void
'start' => $this->start,
'stop' => $stop,
'duration_s' => $durationInSeconds,
'sql' => $this->sql,
]);
}

Expand Down
69 changes: 44 additions & 25 deletions test/PsrSqlLoggerConfigurableLogLevelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,40 @@
*/
class PsrSqlLoggerConfigurableLogLevelsTest extends TestCase
{
/** @var PsrSqlLoggerConfigurableLogLevels */
private $sut;

/** @var TestLogger */
/**
* @var TestLogger
*/
private $logger;

/** @var string */
private $sql = 'SELECT * FROM users WHERE id = :id';

public function testLogLevel() : void
{
$this->sut->startQuery($this->sql);
$this->sut->stopQuery();
$defaultLogLevel = LogLevel::DEBUG;
$logLevelAfterReachingThreshold = LogLevel::INFO;
$thresholdInMilliseconds = 25;
$psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels(
$this->logger,
new LogLevelConfiguration([
$logLevelAfterReachingThreshold => $thresholdInMilliseconds,
]),
$defaultLogLevel
);

self::assertSame(LogLevel::DEBUG, (string) $this->getRecordByIndex(0)->level);
self::assertSame(LogLevel::INFO, (string) $this->getRecordByIndex(1)->level);
$psrSqlLoggerConfigurableLogLevels->startQuery($this->sql);
$psrSqlLoggerConfigurableLogLevels->stopQuery();

self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level);
//No threshold is reached yet: default log level should be used
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level);

$this->sut->startQuery($this->sql);
usleep(50 * 1000); //Sleep 50 milliseconds to simulate query execution
$this->sut->stopQuery();
$psrSqlLoggerConfigurableLogLevels->startQuery($this->sql);
usleep($thresholdInMilliseconds * 1000); //Sleep to simulate query execution and reach the threshold
$psrSqlLoggerConfigurableLogLevels->stopQuery();

self::assertSame(LogLevel::DEBUG, (string) $this->getRecordByIndex(2)->level);
self::assertSame(LogLevel::NOTICE, (string) $this->getRecordByIndex(3)->level);
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(2)->level);
self::assertSame($logLevelAfterReachingThreshold, (string) $this->getRecordByIndex(3)->level);
}

private function getRecordByIndex(int $index): stdClass
Expand Down Expand Up @@ -67,6 +78,24 @@ public function testFallbackToDefaultLogLevel() : void
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level);
}

public function testFallbackToDefaultLogLevelWhenNoThresholdIsReached() : void
{
$defaultLogLevel = LogLevel::DEBUG;
$psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels(
$this->logger,
new LogLevelConfiguration([
LogLevel::CRITICAL => 1000 * 60, //Use a huge threshold of one minute which should never be reached
]),
$defaultLogLevel
);

$psrSqlLoggerConfigurableLogLevels->startQuery($this->sql);
$psrSqlLoggerConfigurableLogLevels->stopQuery();

self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level);
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level);
}

public function testInvalidConfiguration() : void
{
$this->expectException(TypeError::class);
Expand All @@ -78,7 +107,7 @@ public function testInvalidConfiguration() : void
);
}

public function testInvalidLogLevelUsedInConfiration() : void
public function testInvalidLogLevelUsedInConfiguration() : void
{
$this->expectException(InvalidArgumentException::class);
$loggerWhichWillFailToInitialize = new PsrSqlLoggerConfigurableLogLevels(
Expand All @@ -92,15 +121,5 @@ public function testInvalidLogLevelUsedInConfiration() : void
protected function setUp()
{
$this->logger = new TestLogger();
$this->sut = new PsrSqlLoggerConfigurableLogLevels(
$this->logger,
new LogLevelConfiguration([
LogLevel::INFO => 0,
LogLevel::NOTICE => 50,
LogLevel::WARNING => 100,
LogLevel::CRITICAL => 500
]),
LogLevel::DEBUG
);
}
}

0 comments on commit 0bf991b

Please sign in to comment.