-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added support for PSR-3 Co-authored-by: Greg Holmes <[email protected]>
- Loading branch information
1 parent
0994517
commit 799e66d
Showing
8 changed files
with
200 additions
and
6 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
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
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
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
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,21 @@ | ||
<?php | ||
|
||
namespace Vonage\Logger; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
interface LoggerAwareInterface | ||
{ | ||
public function getLogger(): ?LoggerInterface; | ||
|
||
/** | ||
* @param string|int $level Level of message that we are logging | ||
* @param array<mixed> $context Additional information for context | ||
*/ | ||
public function log($level, string $message, array $context = []): void; | ||
|
||
/** | ||
* @return self | ||
*/ | ||
public function setLogger(LoggerInterface $logger); | ||
} |
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,35 @@ | ||
<?php | ||
|
||
namespace Vonage\Logger; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
trait LoggerTrait | ||
{ | ||
/** | ||
* @var LoggerInterface | ||
*/ | ||
protected $logger; | ||
|
||
public function getLogger(): ?LoggerInterface | ||
{ | ||
return $this->logger; | ||
} | ||
|
||
/** | ||
* @param string|int $level Level of message that we are logging | ||
* @param array<mixed> $context Additional information for context | ||
*/ | ||
public function log($level, string $message, array $context = []): void | ||
{ | ||
$logger = $this->getLogger(); | ||
if ($logger) { | ||
$logger->log($level, $message, $context); | ||
} | ||
} | ||
|
||
public function setLogger(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
} |
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
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,46 @@ | ||
<?php | ||
|
||
namespace VonageTest\Logger; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Vonage\Logger\LoggerTrait; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class LoggerTraitTest extends TestCase | ||
{ | ||
public function testCanSetAndGetLogger() | ||
{ | ||
/** @var LoggerTrait $trait */ | ||
$trait = $this->getMockForTrait(LoggerTrait::class); | ||
$logger = $this->prophesize(LoggerInterface::class)->reveal(); | ||
$trait->setLogger($logger); | ||
|
||
$this->assertSame($logger, $trait->getLogger()); | ||
} | ||
|
||
public function testNoLoggerReturnsNull() | ||
{ | ||
/** @var LoggerTrait $trait */ | ||
$trait = $this->getMockForTrait(LoggerTrait::class); | ||
|
||
$this->assertNull($trait->getLogger()); | ||
} | ||
|
||
public function testCanLogMessageWithLogger() | ||
{ | ||
/** @var LoggerTrait $trait */ | ||
$trait = $this->getMockForTrait(LoggerTrait::class); | ||
$logger = $this->prophesize(LoggerInterface::class)->reveal(); | ||
$trait->setLogger($logger); | ||
|
||
$this->assertNull($trait->log('debug', 'This is a message')); | ||
} | ||
|
||
public function testLoggingAcceptsMessageWithLogger() | ||
{ | ||
/** @var LoggerTrait $trait */ | ||
$trait = $this->getMockForTrait(LoggerTrait::class); | ||
|
||
$this->assertNull($trait->log('debug', 'This is a message')); | ||
} | ||
} |