From f1a59c5e0f0e29364caa57fa8734c5f98f4e0eb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Sat, 23 Jan 2016 21:31:52 +0100 Subject: [PATCH] add release number to every log This adds an internal release number to the raven handler. The release number is added to what is sent to sentry unless something already is present because a release number was sent via "context" or "extra". --- src/Monolog/Handler/RavenHandler.php | 20 ++++++++++++++++++++ tests/Monolog/Handler/RavenHandlerTest.php | 16 ++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/Monolog/Handler/RavenHandler.php b/src/Monolog/Handler/RavenHandler.php index cba15f74..90758274 100644 --- a/src/Monolog/Handler/RavenHandler.php +++ b/src/Monolog/Handler/RavenHandler.php @@ -38,6 +38,12 @@ class RavenHandler extends AbstractProcessingHandler Logger::EMERGENCY => Raven_Client::FATAL, ); + /** + * @var string should represent the current version of the calling + * software. Can be any string (git commit, version number) + */ + private $release; + /** * @var Raven_Client the client object that sends the message to the server */ @@ -169,6 +175,10 @@ class RavenHandler extends AbstractProcessingHandler $options['extra']['extra'] = $record['extra']; } + if (!empty($this->release) && !isset($options['release'])) { + $options['release'] = $this->release; + } + if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) { $options['extra']['message'] = $record['formatted']; $this->ravenClient->captureException($record['context']['exception'], $options); @@ -208,4 +218,14 @@ class RavenHandler extends AbstractProcessingHandler { return array('checksum', 'release'); } + + /** + * @param string $value + */ + public function setRelease($value) + { + $this->release = $value; + + return $this; + } } diff --git a/tests/Monolog/Handler/RavenHandlerTest.php b/tests/Monolog/Handler/RavenHandlerTest.php index 9f55af64..a7c4845f 100644 --- a/tests/Monolog/Handler/RavenHandlerTest.php +++ b/tests/Monolog/Handler/RavenHandlerTest.php @@ -204,6 +204,22 @@ class RavenHandlerTest extends TestCase $this->assertSame($formatter, $handler->getBatchFormatter()); } + public function testRelease() + { + $ravenClient = $this->getRavenClient(); + $handler = $this->getHandler($ravenClient); + $release = 'v42.42.42'; + $handler->setRelease($release); + $record = $this->getRecord(Logger::INFO, 'test'); + $handler->handle($record); + $this->assertEquals($release, $ravenClient->lastData['release']); + + $localRelease = 'v41.41.41'; + $record = $this->getRecord(Logger::INFO, 'test', array('release' => $localRelease)); + $handler->handle($record); + $this->assertEquals($localRelease, $ravenClient->lastData['release']); + } + private function methodThatThrowsAnException() { throw new \Exception('This is an exception');