From 2b6dffa23ffe27812564f39164b8aa246b57f7bf Mon Sep 17 00:00:00 2001 From: Robert Kaufmann III Date: Fri, 28 Mar 2014 22:55:15 -0400 Subject: [PATCH 1/4] Add handler and tests for logentries.com --- README.mdown | 1 + src/Monolog/Handler/LogEntriesHandler.php | 89 +++++++++++++++++++ .../Monolog/Handler/LogEntriesHandlerTest.php | 84 +++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 src/Monolog/Handler/LogEntriesHandler.php create mode 100644 tests/Monolog/Handler/LogEntriesHandlerTest.php diff --git a/README.mdown b/README.mdown index 1179d57e..2a4c83ec 100644 --- a/README.mdown +++ b/README.mdown @@ -136,6 +136,7 @@ Handlers - _LogglyHandler_: Logs records to a [Loggly](http://www.loggly.com/) account. - _RollbarHandler_: Logs records to a [Rollbar](https://rollbar.com/) account. - _SyslogUdpHandler_: Logs records to a remote [Syslogd](http://www.rsyslog.com/) server. +- _LogEntriesHandler_: Logs records to a [LogEntries](http://logentries.com/) account. ### Logging in development diff --git a/src/Monolog/Handler/LogEntriesHandler.php b/src/Monolog/Handler/LogEntriesHandler.php new file mode 100644 index 00000000..c1b59800 --- /dev/null +++ b/src/Monolog/Handler/LogEntriesHandler.php @@ -0,0 +1,89 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler; + +use Monolog\Logger; +use Monolog\Formatter\LineFormatter; + +/** + * @author Robert Kaufmann III + */ +class LogEntriesHandler extends SocketHandler +{ + + /** + * @var string + */ + protected $logToken; + + /** + * @param string $token Log token supplied by LogEntries + * @param int $level The minimum logging level to trigger this handler + * @param boolean $bubble Whether or not messages that are handled should bubble up the stack. + * @param boolean $useSSL Whether or not SSL encryption should be used. + * @param string $format Message format. See Monolog usage documents. + * + * @throws MissingExtensionExcpetion If SSL encryption is set to true and OpenSSL is missing + */ + public function __construct($token, $level = Logger::DEBUG, $bubble = true, $useSSL = true) + { + if ($useSSL && !extension_loaded('openssl')) { + throw new MissingExtensionException('The OpenSSL PHP plugin is required to use SSL encrypted connection for LogEntriesHandler'); + } + + $endpoint = $useSSL ? 'ssl://api.logentries.com:20000' : 'data.logentries.com:80'; + parent::__construct($endpoint, $level, $bubble); + $this->logToken = $token; + } + + /** + * {@inheritdoc} + * + * @param array $record + */ + public function write(array $record) + { + parent::write($record); + $this->closeSocket(); + } + + /** + * {@inheritdoc} + */ + public function handleBatch(array $records) + { + foreach ($records as $record) { + $record['formatted'] = $this->getFormatter()->format($record); + parent::write($record); + } + $this->closeSocket(); + } + + /** + * {@inheritdoc} + * + * @param array $record + * @return string + */ + protected function generateDataStream($record) + { + return $this->logToken . ' ' . $record['formatted']; + } + + /** + * {@inheritDoc} + */ + protected function getDefaultFormatter() + { + return new LineFormatter(); + } +} \ No newline at end of file diff --git a/tests/Monolog/Handler/LogEntriesHandlerTest.php b/tests/Monolog/Handler/LogEntriesHandlerTest.php new file mode 100644 index 00000000..c6ec8602 --- /dev/null +++ b/tests/Monolog/Handler/LogEntriesHandlerTest.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler; + +use Monolog\TestCase; +use Monolog\Logger; +use Monolog\Formatter\LineFormatter; + +/** + * @author Robert Kaufmann III + */ +class LogEntriesHandlerTest extends TestCase +{ + /** + * @var resource + */ + private $res; + + /** + * @var LogEntriesHandler + */ + private $handler; + + public function testWriteContent() + { + $this->createHandler(); + $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Critical write test')); + + fseek($this->res, 0); + $content = fread($this->res, 1024); + + $this->assertRegexp('/testToken \[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] test.CRITICAL: Critical write test/', $content); + } + + public function testWriteBatchContent() + { + $records = array( + $this->getRecord(), + $this->getRecord(), + $this->getRecord() + ); + $this->createHandler(); + $this->handler->handleBatch($records); + + fseek($this->res, 0); + $content = fread($this->res, 1024); + + $this->assertRegexp('/(testToken \[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] .* \[\] \[\]\n){3}/', $content); + } + + private function createHandler($logToken = 'testToken', $useSSL = true) + { + $args = array($logToken, Logger::DEBUG, true, $useSSL = true); + $this->res = fopen('php://memory', 'a'); + $this->handler = $this->getMock( + '\Monolog\Handler\LogEntriesHandler', + array('fsockopen', 'streamSetTimeout', 'closeSocket'), + $args + ); + + $reflectionProperty = new \ReflectionProperty('\Monolog\Handler\SocketHandler', 'connectionString'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($this->handler, 'localhost:1234'); + + $this->handler->expects($this->any()) + ->method('fsockopen') + ->will($this->returnValue($this->res)); + $this->handler->expects($this->any()) + ->method('streamSetTimeout') + ->will($this->returnValue(true)); + $this->handler->expects($this->any()) + ->method('closeSocket') + ->will($this->returnValue(true)); + } +} From 8812850aeebe626a476853fc8b97ec3028fdb653 Mon Sep 17 00:00:00 2001 From: Robert Kaufmann III Date: Sun, 30 Mar 2014 22:33:24 -0400 Subject: [PATCH 2/4] Reorder parameters for construct method. --- src/Monolog/Handler/LogEntriesHandler.php | 5 ++--- tests/Monolog/Handler/LogEntriesHandlerTest.php | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Monolog/Handler/LogEntriesHandler.php b/src/Monolog/Handler/LogEntriesHandler.php index c1b59800..ce69edee 100644 --- a/src/Monolog/Handler/LogEntriesHandler.php +++ b/src/Monolog/Handler/LogEntriesHandler.php @@ -27,14 +27,13 @@ class LogEntriesHandler extends SocketHandler /** * @param string $token Log token supplied by LogEntries + * @param boolean $useSSL Whether or not SSL encryption should be used. * @param int $level The minimum logging level to trigger this handler * @param boolean $bubble Whether or not messages that are handled should bubble up the stack. - * @param boolean $useSSL Whether or not SSL encryption should be used. - * @param string $format Message format. See Monolog usage documents. * * @throws MissingExtensionExcpetion If SSL encryption is set to true and OpenSSL is missing */ - public function __construct($token, $level = Logger::DEBUG, $bubble = true, $useSSL = true) + public function __construct($token, $useSSL = true, $level = Logger::DEBUG, $bubble = true) { if ($useSSL && !extension_loaded('openssl')) { throw new MissingExtensionException('The OpenSSL PHP plugin is required to use SSL encrypted connection for LogEntriesHandler'); diff --git a/tests/Monolog/Handler/LogEntriesHandlerTest.php b/tests/Monolog/Handler/LogEntriesHandlerTest.php index c6ec8602..5a3f9755 100644 --- a/tests/Monolog/Handler/LogEntriesHandlerTest.php +++ b/tests/Monolog/Handler/LogEntriesHandlerTest.php @@ -57,9 +57,10 @@ class LogEntriesHandlerTest extends TestCase $this->assertRegexp('/(testToken \[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] .* \[\] \[\]\n){3}/', $content); } - private function createHandler($logToken = 'testToken', $useSSL = true) + private function createHandler() { - $args = array($logToken, Logger::DEBUG, true, $useSSL = true); + $useSSL = extension_loaded('openssl'); + $args = array('testToken', $useSSL, Logger::DEBUG, true); $this->res = fopen('php://memory', 'a'); $this->handler = $this->getMock( '\Monolog\Handler\LogEntriesHandler', From 7f4229a2f41a23111f97e125c776c0032b55e571 Mon Sep 17 00:00:00 2001 From: Robert Kaufmann III Date: Sun, 30 Mar 2014 22:36:47 -0400 Subject: [PATCH 3/4] Remove unneeded calls to close method. --- src/Monolog/Handler/LogEntriesHandler.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Monolog/Handler/LogEntriesHandler.php b/src/Monolog/Handler/LogEntriesHandler.php index ce69edee..ada2ce70 100644 --- a/src/Monolog/Handler/LogEntriesHandler.php +++ b/src/Monolog/Handler/LogEntriesHandler.php @@ -44,17 +44,6 @@ class LogEntriesHandler extends SocketHandler $this->logToken = $token; } - /** - * {@inheritdoc} - * - * @param array $record - */ - public function write(array $record) - { - parent::write($record); - $this->closeSocket(); - } - /** * {@inheritdoc} */ @@ -64,7 +53,6 @@ class LogEntriesHandler extends SocketHandler $record['formatted'] = $this->getFormatter()->format($record); parent::write($record); } - $this->closeSocket(); } /** From fcd0d092b3c60c259652df31a8e70ef5e027c100 Mon Sep 17 00:00:00 2001 From: Rob Kaufmann Date: Mon, 31 Mar 2014 08:56:07 -0400 Subject: [PATCH 4/4] Remove unnecessary getDefaultFormatter and handleBatch methods. --- src/Monolog/Handler/LogEntriesHandler.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/Monolog/Handler/LogEntriesHandler.php b/src/Monolog/Handler/LogEntriesHandler.php index ada2ce70..f49eb032 100644 --- a/src/Monolog/Handler/LogEntriesHandler.php +++ b/src/Monolog/Handler/LogEntriesHandler.php @@ -44,17 +44,6 @@ class LogEntriesHandler extends SocketHandler $this->logToken = $token; } - /** - * {@inheritdoc} - */ - public function handleBatch(array $records) - { - foreach ($records as $record) { - $record['formatted'] = $this->getFormatter()->format($record); - parent::write($record); - } - } - /** * {@inheritdoc} * @@ -66,11 +55,4 @@ class LogEntriesHandler extends SocketHandler return $this->logToken . ' ' . $record['formatted']; } - /** - * {@inheritDoc} - */ - protected function getDefaultFormatter() - { - return new LineFormatter(); - } } \ No newline at end of file