Skip to content

Commit

Permalink
Fix method asLoggerInterface().
Browse files Browse the repository at this point in the history
  • Loading branch information
ksucherek committed Dec 5, 2024
1 parent b32657a commit 9c802f1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
38 changes: 21 additions & 17 deletions src/Ouzo/Core/Logger/LoggerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,50 +93,54 @@ public function log(string $level, $message, array $context = []): void

public function asLoggerInterface(): LoggerInterface
{
return new class() implements LoggerInterface {
return new class($this) implements LoggerInterface {
public function __construct(private readonly LoggerAdapter $loggerAdapter)
{
}

public function log($level, $message, array $context = []): void
{
$this->log($level, $message, $context);
$this->loggerAdapter->log($level, $message, $context);
}

public function emergency($message, array $context = array())
public function emergency($message, array $context = [])
{
$this->emergency($message, $context);
$this->loggerAdapter->emergency($message, $context);
}

public function alert($message, array $context = array())
public function alert($message, array $context = [])
{
$this->alert($message, $context);
$this->loggerAdapter->alert($message, $context);
}

public function critical($message, array $context = array())
public function critical($message, array $context = [])
{
$this->critical($message, $context);
$this->loggerAdapter->critical($message, $context);
}

public function error($message, array $context = array())
public function error($message, array $context = [])
{
$this->error($message, $context);
$this->loggerAdapter->error($message, $context);
}

public function warning($message, array $context = array())
public function warning($message, array $context = [])
{
$this->warning($message, $context);
$this->loggerAdapter->warning($message, $context);
}

public function notice($message, array $context = array())
public function notice($message, array $context = [])
{
$this->notice($message, $context);
$this->loggerAdapter->notice($message, $context);
}

public function info($message, array $context = array())
public function info($message, array $context = [])
{
$this->info($message, $context);
$this->loggerAdapter->info($message, $context);
}

public function debug($message, array $context = array())
{
$this->debug($message, $context);
$this->loggerAdapter->debug($message, $context);
}
};
}
Expand Down
10 changes: 9 additions & 1 deletion test/src/Ouzo/Core/Logger/LoggerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Ouzo\Logger\StdOutputLogger;
use Ouzo\Tests\Assert;
use Ouzo\Tests\Mock\Mock;
use Ouzo\Tests\Mock\MockInterface;
use Ouzo\Tests\StreamStub;
use Ouzo\Utilities\Clock;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -166,9 +167,16 @@ public function shouldIgnoreDebugMessageIfDebugIsOff()
public function shouldGetAsInstanceOfLoggerInterface(): void
{
// given
Config::overrideProperty('debug')->with(true);
$loggerInterface = $this->logger->asLoggerInterface();
// when
$loggerInterface->debug('My debug log line without params.');

// then
Assert::that($this->logger->asLoggerInterface())->isInstanceOf(LoggerInterface::class);
Assert::that($loggerInterface)->isInstanceOf(LoggerInterface::class);
$logContent = $this->readStreamContent('test://stdout');
Assert::thatString($logContent)->contains('2014-01-01 11:11:11: TEST debug: [ID: ] My debug log line without params.');

}

private function readStreamContent(string $streamFile): string
Expand Down

0 comments on commit 9c802f1

Please sign in to comment.