Skip to content

Commit

Permalink
refactor & fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ytake committed Jul 17, 2018
1 parent 9eed211 commit d991d4a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 28 deletions.
1 change: 0 additions & 1 deletion phpunit.xml
Expand Up @@ -8,7 +8,6 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
>
<testsuites>
<testsuite name="Application Test Suite">
Expand Down
6 changes: 6 additions & 0 deletions src/FluentHandler.php
Expand Up @@ -23,6 +23,12 @@
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;

use function str_replace;
use function preg_match_all;
use function sprintf;
use function is_array;
use function array_key_exists;

/**
* Class FluentHandler
*/
Expand Down
74 changes: 53 additions & 21 deletions src/FluentLogManager.php
Expand Up @@ -20,19 +20,22 @@
namespace Ytake\LaravelFluent;

use Fluent\Logger\FluentLogger;
use Fluent\Logger\PackerInterface;
use Illuminate\Log\LogManager;
use Monolog\Handler\HandlerInterface;
use Monolog\Logger as Monolog;
use Psr\Log\LoggerInterface;

use function is_null;
use function class_exists;
use function strval;

/**
* Class FluentLogManager
*/
final class FluentLogManager extends LogManager
{
/**
* @var \Illuminate\Contracts\Container\Container
*/
/** @var \Illuminate\Contracts\Container\Container */
protected $app;

/**
Expand All @@ -49,31 +52,21 @@ protected function createFluentDriver(array $config): LoggerInterface
]);
}

private function createFluentHandler(array $config) : HandlerInterface
/**
* @param array $config
*
* @return HandlerInterface
*/
private function createFluentHandler(array $config): HandlerInterface
{
$configure = $this->app['config']['fluent'];

$packer = null;
if (!is_null($configure['packer'])) {
if (class_exists($configure['packer'])) {
$packer = $this->app->make($configure['packer']);
}
}

$fluentHandler = FluentHandler::class;
if (!is_null($configure['handler'])) {
if (class_exists($configure['handler'])) {
$fluentHandler = $configure['handler'];
}
}


$fluentHandler = $this->detectHandler($configure);
return new $fluentHandler(
new FluentLogger(
$configure['host'] ?? FluentLogger::DEFAULT_ADDRESS,
(int)$configure['port'] ?? FluentLogger::DEFAULT_LISTEN_PORT,
$configure['options'] ?? [],
$packer
$this->detectPacker($configure)
),
$configure['tagFormat'] ?? null,
$this->level($config)
Expand All @@ -89,4 +82,43 @@ public function __invoke(array $config): LoggerInterface
{
return $this->createFluentDriver($config);
}

/**
* @return string
*/
protected function defaultHandler(): string
{
return FluentHandler::class;
}

/**
* @param array $configure
*
* @return PackerInterface|null
*/
protected function detectPacker(array $configure): ?PackerInterface
{
if (!is_null($configure['packer'])) {
if (class_exists($configure['packer'])) {
return $this->app->make($configure['packer']);
}
}
return null;
}

/**
* @param array $configure
*
* @return string
*/
protected function detectHandler(array $configure): string
{
$handler = $configure['handler'] ?? null;
if (!is_null($handler)) {
if (class_exists($handler)) {
return strval($handler);
}
}
return $this->defaultHandler();
}
}
2 changes: 1 addition & 1 deletion src/config/fluent.php
Expand Up @@ -28,7 +28,7 @@
// specified class name
'packer' => null,

// optionally override FluentHandler-class to customize behaviour
// optionally override Ytake\LaravelFluent\FluentHandler class to customize behaviour
'handler' => null,

'tagFormat' => '{{channel}}.{{level_name}}',
Expand Down
25 changes: 20 additions & 5 deletions tests/TestCase.php
@@ -1,4 +1,9 @@
<?php
declare(strict_types=1);

use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Config\Repository;

class TestCase extends \PHPUnit\Framework\TestCase
{
Expand All @@ -11,14 +16,14 @@ protected function setUp()
}

/**
* @return \Illuminate\Container\Container
* @return Container
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function createApplicationContainer()
protected function createApplicationContainer(): Container
{
$container = new \Illuminate\Container\Container;
$filesystem = new \Illuminate\Filesystem\Filesystem;
$container->instance('config', new \Illuminate\Config\Repository);
$container = $this->getExtendedContainer();
$filesystem = new Filesystem();
$container->instance('config', new Repository());
$container['config']
->set("fluent", $filesystem->getRequire(__DIR__ . '/config/fluent.php'));
$container['config']
Expand All @@ -28,4 +33,14 @@ protected function createApplicationContainer()
$eventProvider->register();
return $container;
}

protected function getExtendedContainer(): Container
{
return new class() extends Container {
public function storagePath(): string
{
return __DIR__ . '/storages';
}
};
}
}
2 changes: 2 additions & 0 deletions tests/storages/logs/.gitignore
@@ -0,0 +1,2 @@
*
!.gitignore

0 comments on commit d991d4a

Please sign in to comment.