From 396c110e89fa1ea4c9bb5adad6f134c12857cb3f Mon Sep 17 00:00:00 2001 From: Artur Moczulski Date: Mon, 8 Jul 2013 00:33:45 -0700 Subject: [PATCH 1/4] added ctags generated 'tags' files to .gitingore for --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0a3fb552..ace14cef 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.phar phpunit.xml composer.lock .DS_Store +tags From fe13e807871aafb1d08c577f9cdead910657eb0e Mon Sep 17 00:00:00 2001 From: Artur Moczulski Date: Mon, 8 Jul 2013 00:35:22 -0700 Subject: [PATCH 2/4] added support for Pushover API priority brought up by SteveEdson in issue #187; the priority levels can be adjusted either through the handler's constructor or setters --- src/Monolog/Handler/PushoverHandler.php | 26 ++++++++++++++++++- tests/Monolog/Handler/PushoverHandlerTest.php | 25 +++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Monolog/Handler/PushoverHandler.php b/src/Monolog/Handler/PushoverHandler.php index 06818393..15695148 100644 --- a/src/Monolog/Handler/PushoverHandler.php +++ b/src/Monolog/Handler/PushoverHandler.php @@ -24,6 +24,9 @@ class PushoverHandler extends SocketHandler private $token; private $user; private $title; + + private $highPriorityLevel; + private $emergencyLevel; /** * @param string $token Pushover api token @@ -33,8 +36,12 @@ class PushoverHandler extends SocketHandler * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not * @param Boolean $useSSL Whether to connect via SSL. Required when pushing messages to users that are not * the pushover.net app owner. OpenSSL is required for this option. + * @param integer $highPriorityLevel The minimum logging level at which this handler will start + * sending "high priority" requestes to Pushover API + * @param integer $emergencyLevel The minimum logging level at which this handler will start + * sending "emergency" requests to Pushover API */ - public function __construct($token, $user, $title = null, $level = Logger::CRITICAL, $bubble = true, $useSSL = true) + public function __construct($token, $user, $title = null, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $highPriorityLevel = Logger::CRITICAL, $emergencyLevel = Logger::EMERGENCY) { $connectionString = $useSSL ? 'ssl://api.pushover.net:443' : 'api.pushover.net:80'; parent::__construct($connectionString, $level, $bubble); @@ -42,6 +49,8 @@ class PushoverHandler extends SocketHandler $this->token = $token; $this->user = $user; $this->title = $title ?: gethostname(); + $this->highPriorityLevel = $highPriorityLevel; + $this->emergencyLevel = $emergencyLevel; } protected function generateDataStream($record) @@ -66,6 +75,12 @@ class PushoverHandler extends SocketHandler 'timestamp' => $timestamp ); + if( $record['level'] >= $this->highPriorityLevel && $record['level'] < $this->emergencyLevel ) { + $dataArray['priority'] = 1; + } else if ( $record['level'] >= $this->emergencyLevel ) { + $dataArray['priority'] = 2; + } + return http_build_query($dataArray); } @@ -85,4 +100,13 @@ class PushoverHandler extends SocketHandler parent::write($record); $this->closeSocket(); } + + public function setHighPriorityLevel($value) { + $this->highPriorityLevel = $value; + } + + public function setEmergencyLevel($value) { + $this->emergencyLevel = $value; + } + } diff --git a/tests/Monolog/Handler/PushoverHandlerTest.php b/tests/Monolog/Handler/PushoverHandlerTest.php index 991d0e9b..a64a7feb 100644 --- a/tests/Monolog/Handler/PushoverHandlerTest.php +++ b/tests/Monolog/Handler/PushoverHandlerTest.php @@ -29,6 +29,7 @@ class PushoverHandlerTest extends TestCase public function testWriteHeader() { $this->createHandler(); + $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); fseek($this->res, 0); $content = fread($this->res, 1024); @@ -48,7 +49,7 @@ class PushoverHandlerTest extends TestCase public function testWriteWithComplexTitle() { - $this->createHandler('myToken', 'myUser', 'Backup finished - SQL1'); + $this->createHandler('myToken', 'myUser', 'Backup finished - SQL1', Logger::EMERGENCY); $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); fseek($this->res, 0); $content = fread($this->res, 1024); @@ -59,6 +60,7 @@ class PushoverHandlerTest extends TestCase public function testWriteWithComplexMessage() { $this->createHandler(); + $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications $this->handler->handle($this->getRecord(Logger::CRITICAL, 'Backup of database "example" finished in 16 minutes.')); fseek($this->res, 0); $content = fread($this->res, 1024); @@ -70,6 +72,7 @@ class PushoverHandlerTest extends TestCase { $message = str_pad('test', 520, 'a'); $this->createHandler(); + $this->handler->setHighPriorityLevel(Logger::EMERGENCY); // skip priority notifications $this->handler->handle($this->getRecord(Logger::CRITICAL, $message)); fseek($this->res, 0); $content = fread($this->res, 1024); @@ -79,6 +82,26 @@ class PushoverHandlerTest extends TestCase $this->assertRegexp('/message=' . $expectedMessage . '&title/', $content); } + public function testWriteWithHighPriority() + { + $this->createHandler(); + $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); + fseek($this->res, 0); + $content = fread($this->res, 1024); + + $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog×tamp=\d{10}&priority=1$/', $content); + } + + public function testWriteWithEmergencyPriority() + { + $this->createHandler(); + $this->handler->handle($this->getRecord(Logger::EMERGENCY, 'test1')); + fseek($this->res, 0); + $content = fread($this->res, 1024); + + $this->assertRegexp('/token=myToken&user=myUser&message=test1&title=Monolog×tamp=\d{10}&priority=2$/', $content); + } + private function createHandler($token = 'myToken', $user = 'myUser', $title = 'Monolog') { $constructorArgs = array($token, $user, $title); From 8dfd124bcd1299556716928d682403de9f43d7bd Mon Sep 17 00:00:00 2001 From: Artur Moczulski Date: Wed, 10 Jul 2013 08:42:39 -0700 Subject: [PATCH 3/4] Update .gitignore removing tags from .gitignore following stof suggestion --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index ace14cef..0a3fb552 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,3 @@ composer.phar phpunit.xml composer.lock .DS_Store -tags From aeb2dd578c28119924f0b1a198cad2c9d78ddc72 Mon Sep 17 00:00:00 2001 From: Artur Moczulski Date: Wed, 10 Jul 2013 08:44:39 -0700 Subject: [PATCH 4/4] Update PushoverHandler.php simplifying the priority level setting if condition following stof suggestion --- src/Monolog/Handler/PushoverHandler.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Monolog/Handler/PushoverHandler.php b/src/Monolog/Handler/PushoverHandler.php index 15695148..b2c0b4e6 100644 --- a/src/Monolog/Handler/PushoverHandler.php +++ b/src/Monolog/Handler/PushoverHandler.php @@ -75,10 +75,10 @@ class PushoverHandler extends SocketHandler 'timestamp' => $timestamp ); - if( $record['level'] >= $this->highPriorityLevel && $record['level'] < $this->emergencyLevel ) { - $dataArray['priority'] = 1; - } else if ( $record['level'] >= $this->emergencyLevel ) { + if( $record['level'] >= $this->emergencyLevel ) { $dataArray['priority'] = 2; + } else if ( $record['level'] >= $this->highPriorityLevel ) { + $dataArray['priority'] = 1; } return http_build_query($dataArray);