Skip to content

Commit

Permalink
update logger channel logic
Browse files Browse the repository at this point in the history
  • Loading branch information
flotzilla committed Apr 14, 2020
1 parent 51a7b2b commit e14430d
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
vendor
phpunit.xml
phpunit.xml
phpstan.neon
6 changes: 6 additions & 0 deletions src/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use flotzilla\Logger\Exception\FormatterException;
use flotzilla\Logger\Exception\HandlerException;
use flotzilla\Logger\Exception\InvalidChannelNameException;
use flotzilla\Logger\Exception\InvalidLogLevelException;
use flotzilla\Logger\LogLevel\LogLevel;
use flotzilla\Logger\LogLevel\LoglevelInterface;
Expand Down Expand Up @@ -39,6 +40,7 @@ class Channel implements ChannelInterface, LoglevelInterface
* @param string|null $minLogLevel
*
* @throws InvalidLogLevelException
* @throws InvalidChannelNameException
*/
public function __construct(
string $channelName,
Expand All @@ -47,6 +49,10 @@ public function __construct(
string $minLogLevel = null
)
{
if (!$channelName){
throw new InvalidChannelNameException();
}

$this->channelName = $channelName;
$this->handlers = $handlers;

Expand Down
50 changes: 50 additions & 0 deletions src/Channel/NullChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace flotzilla\Logger\Channel;

use flotzilla\Logger\Handler\HandlerInterface;
use flotzilla\Logger\LogLevel\LogLevel;

class NullChannel implements ChannelInterface
{
/**
* @inheritDoc
*/
public function setHandlers(array $handlers): void
{

}

/**
* @inheritDoc
*/
public function getHandlers(): array
{
return [];
}

/**
* @inheritDoc
*/
public function addHandler(HandlerInterface $handler, string $handlerName = null)
{
}

/**
* @inheritDoc
*/
public function getChannelName(): string
{
return 'NullChannel';
}

/**
* @inheritDoc
*/
public function handle(string $message = '', array $context = [], string $level = LogLevel::DEBUG, string $date = '')
{
return true;
}
}
25 changes: 25 additions & 0 deletions src/Exception/InvalidChannelNameException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace flotzilla\Logger\Exception;

use Throwable;

class InvalidChannelNameException extends \Exception
{
/** @var string $message */
protected $message = 'Channel name is invalid';

/**
* InvalidLogLevelException constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct($message = '', $code = 0, Throwable $previous = null)
{
$message = $message ?: $this->message;
parent::__construct($message, $code, $previous);
}
}
42 changes: 38 additions & 4 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DateTimeZone;
use Exception;
use flotzilla\Logger\Channel\ChannelInterface;
use flotzilla\Logger\Channel\NullChannel;
use flotzilla\Logger\Exception\InvalidConfigurationException;
use flotzilla\Logger\Exception\InvalidLogLevelException;
use flotzilla\Logger\Exception\LoggerErrorStackException;
Expand All @@ -21,7 +22,7 @@ class Logger implements LoggerInterface
use LoggerTrait;

/** @var ChannelInterface[] */
protected $channels;
protected $channels = [];

/** @var string $dateTimeFormat */
protected $dateTimeFormat;
Expand All @@ -46,7 +47,7 @@ public function __construct(
throw new InvalidConfigurationException('Invalid date time format');
}

$this->channels = $channels;
$this->setChannels($channels);
$this->dateTimeFormat = $dateTimeFormat;
$this->timeZone = $tz ?: new DateTimeZone(date_default_timezone_get());
}
Expand Down Expand Up @@ -85,7 +86,7 @@ public function log($level, $message, array $context = [])
}
}

if (count($loggerErrors) > 0){
if (count($loggerErrors) > 0) {
throw $loggerErrors;
}
}
Expand All @@ -100,10 +101,43 @@ public function getChannels(): array

/**
* @param ChannelInterface[] $channels
* @throws InvalidConfigurationException
*/
public function setChannels(array $channels): void
{
$this->channels = $channels;
foreach ($channels as $channel)
{
if (!$channel instanceof ChannelInterface){
throw new InvalidConfigurationException('Array arguments should be instance of ChannelInterface');
}

$this->addChannel($channel);;
}
}

/**
* @param ChannelInterface $channel
* @throws InvalidConfigurationException
*/
public function addChannel(ChannelInterface $channel): void
{
if (array_key_exists($channel->getChannelName(), $this->channels))
{
throw new InvalidConfigurationException(
"Channel with name {$channel->getChannelName()} already exist in runtime")
;
}

$this->channels[$channel->getChannelName()] = $channel;
}

/**
* @param string $name
* @return ChannelInterface|null
*/
public function getChannel(string $name): ?ChannelInterface
{
return array_key_exists($name, $this->channels)? $this->channels[$name]: new NullChannel;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/Channel/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace flotzilla\Logger\Test\Channel;

use flotzilla\Logger\Channel\Channel;
use flotzilla\Logger\Exception\InvalidChannelNameException;
use flotzilla\Logger\Exception\InvalidLogLevelException;
use flotzilla\Logger\Formatter\SimpleLineFormatter;
use flotzilla\Logger\Handler\FileHandler;
Expand Down Expand Up @@ -168,4 +169,10 @@ public function testSetMinLogLevelException()

$c->setMinLogLevel('some wrong level');
}

public function testName()
{
$this->expectException(InvalidChannelNameException::class);
$c = new Channel('');
}
}
59 changes: 59 additions & 0 deletions tests/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use flotzilla\Logger\Channel\Channel;
use flotzilla\Logger\Exception\FormatterException;
use flotzilla\Logger\Exception\InvalidChannelNameException;
use flotzilla\Logger\Exception\InvalidConfigurationException;
use flotzilla\Logger\Exception\InvalidLogLevelException;
use flotzilla\Logger\Exception\LoggerErrorStackException;
Expand All @@ -13,6 +14,7 @@
use flotzilla\Logger\Handler\FileHandler;
use flotzilla\Logger\Logger;
use flotzilla\Logger\LogLevel\LogLevel;
use flotzilla\Logger\Test\Formatter\TestFormatter;
use PHPUnit\Framework\TestCase;

class LoggerTest extends TestCase
Expand Down Expand Up @@ -281,4 +283,61 @@ public function testThrowOneError()
$this->assertTrue(file_exists('tmp/test-additional-' . date('j.n.Y') . '.log'));
}
}

public function testEmptyNameChannel()
{
$this->expectException(InvalidChannelNameException::class);
$logger = new Logger();
$logger->addChannel(new Channel('', [ new FileHandler(new TestFormatter) ]));
}

public function testSetEmptyChannels()
{
$logger = new Logger();
$logger->setChannels([]);
$this->assertCount(0, $logger->getChannels());
}

public function testSetInvalidChannels()
{
$this->expectException(InvalidConfigurationException::class);
$logger = new Logger();
$logger->setChannels([
new \stdClass(),
""
]);
}

public function testAddExistingChannel()
{
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('Channel with name c already exist in runtime');
$logger = new Logger();
$logger->addChannel(new Channel('c'));
$logger->addChannel(new Channel('c'));
}

public function testGetChannelByName()
{
$logger = new Logger();
$logger->addChannel(new Channel('c'));

$this->assertInstanceOf(Channel::class, $logger->getChannel('c'));
}

public function testMultipleChannelsGet()
{
$logger = new Logger();
$c1 = new Channel('c1');
$c2 = new Channel('c2');

$logger->addChannel($c1);
$logger->addChannel($c2);

$this->assertInstanceOf(Channel::class, $logger->getChannel('c1'));
$this->assertInstanceOf(Channel::class, $logger->getChannel('c2'));
$this->assertEquals($c1, $logger->getChannel('c1'));
$this->assertEquals($c2, $logger->getChannel('c2'));
$this->assertCount(2, $logger->getChannels());
}
}

0 comments on commit e14430d

Please sign in to comment.