From 371b8ebb1bdfe5f254024fed671959f6c452aff7 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 13 Nov 2016 20:25:52 +0100 Subject: [PATCH] Add a RollbarHandler::flush() and make it close automatically on shutdown, fixes #864, closes #865 --- src/Monolog/Handler/RollbarHandler.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Monolog/Handler/RollbarHandler.php b/src/Monolog/Handler/RollbarHandler.php index 9a2b1325..0d9de1a2 100644 --- a/src/Monolog/Handler/RollbarHandler.php +++ b/src/Monolog/Handler/RollbarHandler.php @@ -58,6 +58,8 @@ class RollbarHandler extends AbstractProcessingHandler */ private $hasRecords = false; + protected $initialized = false; + /** * @param RollbarNotifier $rollbarNotifier RollbarNotifier object constructed with valid token * @param int $level The minimum logging level at which this handler will be triggered @@ -75,6 +77,12 @@ class RollbarHandler extends AbstractProcessingHandler */ protected function write(array $record) { + if (!$this->initialized) { + // __destructor() doesn't get called on Fatal errors + register_shutdown_function(array($this, 'close')); + $this->initialized = true; + } + $context = $record['context']; $payload = array(); if (isset($context['payload'])) { @@ -105,14 +113,19 @@ class RollbarHandler extends AbstractProcessingHandler $this->hasRecords = true; } - /** - * {@inheritdoc} - */ - public function close() + public function flush() { if ($this->hasRecords) { $this->rollbarNotifier->flush(); $this->hasRecords = false; } } + + /** + * {@inheritdoc} + */ + public function close() + { + $this->flush(); + } }