Skip to content

Commit

Permalink
use BufferHandler, adapt filebeat, no need for uid
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Zogg committed Feb 8, 2020
1 parent 2a17736 commit 324e4a1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
16 changes: 6 additions & 10 deletions app/ServiceFactory/MonologServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace App\ServiceFactory;

use Monolog\Formatter\LogstashFormatter;
use Monolog\Handler\BufferHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

Expand All @@ -22,18 +22,14 @@ public function __invoke(): array
LoggerInterface::class => static function (ContainerInterface $container) {
$monolog = $container->get('monolog');

return new Logger(
$monolog['name'],
[
return new Logger($monolog['name'], [
new BufferHandler(
(new StreamHandler(
$monolog['path'],
$monolog['level']
))->setFormatter(new LogstashFormatter('app')),
],
[
new UidProcessor(),
]
);
))->setFormatter(new LogstashFormatter('app'))
),
]);
},
'logger' => static function (ContainerInterface $container) {
return $container->get(LoggerInterface::class);
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ services:
- ./docker/prod/filebeat/filebeat.yml:/usr/share/filebeat/filebeat.yml:ro
- /var/lib/docker/containers:/var/lib/docker/containers:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./:/var/www/html:ro
- ./var/log/dev:/var/log/application:ro
depends_on:
- elasticsearch
- kibana
2 changes: 1 addition & 1 deletion docker/prod/filebeat/filebeat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ filebeat.autodiscover:
config:
- type: log
paths:
- /var/www/html/var/log/*/*.log
- /var/log/application/*.log
json.keys_under_root: true

processors:
Expand Down
22 changes: 10 additions & 12 deletions tests/Unit/ServiceFactory/MonologServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
use Chubbyphp\Mock\Call;
use Chubbyphp\Mock\MockByCallsTrait;
use Monolog\Formatter\LogstashFormatter;
use Monolog\Handler\BufferHandler;
use Monolog\Handler\Handler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -58,8 +58,16 @@ public function testLoggerInterface(): void

self::assertCount(1, $handlers);

/** @var BufferHandler $bufferedHandler */
$bufferedHandler = array_shift($handlers);

self::assertInstanceOf(BufferHandler::class, $bufferedHandler);

$handlerReflectionProperty = new \ReflectionProperty($bufferedHandler, 'handler');
$handlerReflectionProperty->setAccessible(true);

/** @var StreamHandler $streamHandler */
$streamHandler = array_shift($handlers);
$streamHandler = $handlerReflectionProperty->getValue($bufferedHandler);

self::assertInstanceOf(StreamHandler::class, $streamHandler);

Expand All @@ -70,16 +78,6 @@ public function testLoggerInterface(): void
$formatter = $streamHandler->getFormatter();

self::assertInstanceOf(LogstashFormatter::class, $formatter);

/** @var array<int, callable> $processors */
$processors = $logger->getProcessors();

self::assertCount(1, $processors);

/** @var UidProcessor $uidProcessor */
$uidProcessor = array_shift($processors);

self::assertInstanceOf(UidProcessor::class, $uidProcessor);
}

public function testLogger(): void
Expand Down

0 comments on commit 324e4a1

Please sign in to comment.