-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstraction for logstash introduced, better error handling
- Loading branch information
1 parent
6eed73a
commit f575966
Showing
10 changed files
with
200 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Clearcode\SimpleBusElkBundle\Logstash; | ||
|
||
class CannotWriteToLogstash extends \Exception | ||
{ | ||
|
||
} | ||
This comment has been minimized.
Sorry, something went wrong. |
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,47 @@ | ||
<?php | ||
|
||
namespace Clearcode\SimpleBusElkBundle\Logstash; | ||
|
||
use Clearcode\SimpleBusElkBundle\Converter\DataToConvertIsNotAnObject; | ||
use Clearcode\SimpleBusElkBundle\Converter\ObjectToArrayConverterInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class Logstash | ||
{ | ||
/** @var LoggerInterface */ | ||
private $logger; | ||
/** @var ObjectToArrayConverterInterface */ | ||
private $converter; | ||
|
||
/** | ||
* @param LoggerInterface $logger | ||
* @param ObjectToArrayConverterInterface $converter | ||
*/ | ||
public function __construct(LoggerInterface $logger, ObjectToArrayConverterInterface $converter) | ||
{ | ||
$this->logger = $logger; | ||
$this->converter = $converter; | ||
} | ||
|
||
/** | ||
* @param $object | ||
* | ||
* @throws CannotWriteToLogstash | ||
*/ | ||
public function write($object) | ||
{ | ||
try { | ||
$this->logger->info('Event recorded', $this->converter->toArray($object)); | ||
|
||
} catch (DataToConvertIsNotAnObject $e) { | ||
$this->handleError(sprintf('Data conversion problem during writing to logstash: %s', $e->getMessage())); | ||
} catch (\Exception $e) { | ||
$this->handleError(sprintf('Connection with logstash error: %s', $e->getMessage())); | ||
} | ||
} | ||
|
||
private function handleError($message) | ||
{ | ||
throw new CannotWriteToLogstash($message); | ||
} | ||
} |
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,8 @@ | ||
services: | ||
simple_bus_elk.logstash: | ||
class: Clearcode\SimpleBusElkBundle\Logstash\Logstash | ||
arguments: | ||
- @logger | ||
- @simple_bus_elk.converter.to_array_converter | ||
tags: | ||
- { name: monolog.logger, channel: '%simple_bus_elk.monolog_channel%' } |
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,72 @@ | ||
<?php | ||
|
||
namespace tests\Clearcode\SimpleBusElkBundle\Logstash; | ||
|
||
use Clearcode\SimpleBusElkBundle\Converter\DataToConvertIsNotAnObject; | ||
use Clearcode\SimpleBusElkBundle\Converter\ObjectToArrayConverterInterface; | ||
use Clearcode\SimpleBusElkBundle\Logstash\Logstash; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class LogstashTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @var LoggerInterface|ObjectProphecy */ | ||
private $logger; | ||
/** @var ObjectToArrayConverterInterface|ObjectProphecy */ | ||
private $converter; | ||
/** @var Logstash */ | ||
private $logstash; | ||
|
||
/** @test */ | ||
public function it_can_write_log() | ||
{ | ||
$object = new \stdClass(); | ||
$this->converter->toArray($object)->willReturn(['message' => 'hello world!']); | ||
|
||
$this->logstash->write($object); | ||
|
||
$this->logger->info('Event recorded', ['message' => 'hello world!'])->shouldBeCalled(); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Clearcode\SimpleBusElkBundle\Logstash\CannotWriteToLogstash | ||
*/ | ||
public function it_fails_when_connection_failed() | ||
{ | ||
$object = new \stdClass(); | ||
$this->converter->toArray($object)->willReturn(['message' => 'hello world!']); | ||
$this->logger->info('Event recorded', ['message' => 'hello world!'])->willThrow(\UnexpectedValueException::class); | ||
|
||
$this->logstash->write($object); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Clearcode\SimpleBusElkBundle\Logstash\CannotWriteToLogstash | ||
*/ | ||
public function it_fails_when_data_conversion_failed() | ||
{ | ||
$object = new \stdClass(); | ||
$this->converter->toArray($object)->willThrow(DataToConvertIsNotAnObject::class); | ||
|
||
$this->logstash->write($object); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
protected function setUp() | ||
{ | ||
$this->logger = $this->prophesize(LoggerInterface::class); | ||
$this->converter = $this->prophesize(ObjectToArrayConverterInterface::class); | ||
|
||
$this->logstash = new Logstash($this->logger->reveal(), $this->converter->reveal()); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
protected function tearDown() | ||
{ | ||
$this->logger = null; | ||
$this->converter = null; | ||
$this->logstash = null; | ||
} | ||
} |
no empty line at the end