Skip to content

Commit

Permalink
Merge pull request #8 from polderknowledge/feature/daily-stream
Browse files Browse the repository at this point in the history
Feature/daily stream
  • Loading branch information
waltertamboer authored Nov 2, 2018
2 parents 41114a4 + 06003e2 commit 720d3e9
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/Monolog/Handler/Factory/DailyStream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Polder Knowledge / log-module (https://polderknowledge.com)
*
* @link https://github.com/polderknowledge/log-module for the canonical source repository
* @copyright Copyright (c) 2016-2017 Polder Knowledge (https://polderknowledge.com)
* @license https://github.com/polderknowledge/log-module/blob/master/LICENSE.md MIT
*/

namespace PolderKnowledge\LogModule\Monolog\Handler\Factory;

use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use WShafer\PSR11MonoLog\FactoryInterface;

final class DailyStream implements FactoryInterface
{
public function __invoke(array $options)
{
$stream = $this->getStream(
$options['stream'] ?? null,
$options['dateFormat'] ?? 'Ymd'
);

$level = (int)($options['level'] ?? Logger::DEBUG);
$bubble = (boolean)($options['bubble'] ?? true);
$filePermission = (int)($options['filePermission'] ?? 0644);
$useLocking = (boolean)($options['useLocking'] ?? true);

return new StreamHandler($stream, $level, $bubble, $filePermission, $useLocking);
}

protected function getStream($stream, $dateFormat)
{
if (is_resource($stream)) {
return $stream;
}

return sprintf((string)$stream, date($dateFormat));
}
}
30 changes: 30 additions & 0 deletions tests/Monolog/Handler/Factory/DailyStreamTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Polder Knowledge / log-module (https://polderknowledge.com)
*
* @link https://github.com/polderknowledge/log-module for the canonical source repository
* @copyright Copyright (c) 2016-2017 Polder Knowledge (https://polderknowledge.com)
* @license https://github.com/polderknowledge/log-module/blob/master/LICENSE.md MIT
*/

namespace PolderKnowledge\LogModule\Monolog\Handler\Factory;

use Monolog\Handler\StreamHandler;
use PHPUnit\Framework\TestCase;

final class DailyStreamTest extends TestCase
{
public function testInvoke()
{
// Arrange
$options = [];

$factory = new DailyStream();

// Act
$result = $factory->__invoke($options);

// Assert
static::assertInstanceOf(StreamHandler::class, $result);
}
}

0 comments on commit 720d3e9

Please sign in to comment.