From 9525131475a453916635eea9b885d486a7451d88 Mon Sep 17 00:00:00 2001 From: Tyler Arbon Date: Fri, 16 Feb 2018 16:13:09 -0700 Subject: [PATCH] Added setLogLevel and getLogLevel functions (#62) * - Added setLogLevel and getLogLevel functions * fixed test file --- CHANGELOG.md | 3 +++ README.md | 10 ++++++++++ src/Components/Notifier.php | 20 ++++++++++++++++++++ src/LERN.php | 20 ++++++++++++++++++++ tests/LERNTest.php | 20 ++++++++++++++++++++ 5 files changed, 73 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c85d38c..3d81a7a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to `LERN` will be documented in this file. +### 4.3.0 +- Added setLogLevel and getLogLevel functions + ### 4.2.2 - Backwards compatibility fix for custom Recorder/Notifier classes diff --git a/README.md b/README.md index 6c28ad0..a664dd4 100755 --- a/README.md +++ b/README.md @@ -120,6 +120,16 @@ To change what is recorded in to the database take a look at `config/lern.php` ### Notifications LERN uses the Monolog library to send notifications. If you need more than the supported notification channels, then you can add your own custom Monolog handlers. To start using any of the supported handlers just edit the provided config file `config/lern.php`. + +#### Changing the log level programmatically +Some notification services support different log levels. If changing the config value `lern.notify.log_level` is not enough then try it this way: +```php +// Change the log level. +// Default is: critical +// Options are: debug, info, notice, warning, error, critical, alert, emergency +LERN::setLogLevel("emergency"); +``` + #### Changing the subject line Some notification services support a subject line, this is how you change it. ```php diff --git a/src/Components/Notifier.php b/src/Components/Notifier.php index cd0236a..ebb4204 100755 --- a/src/Components/Notifier.php +++ b/src/Components/Notifier.php @@ -182,6 +182,26 @@ public function getContext(Exception $e, $context = []) } } + /** + * Get the log level + * @return string + */ + public function getLogLevel() + { + return $this->config['log_level']; + } + + /** + * Set the log level + * @param string $level The log level + * @return \Tylercd100\LERN\LERN + */ + public function setLogLevel($level) + { + $this->config['log_level'] = $level; + return $this; + } + /** * Pushes on another Monolog Handler * @param HandlerInterface $handler The handler instance to add on diff --git a/src/LERN.php b/src/LERN.php index f08e993..143abcf 100755 --- a/src/LERN.php +++ b/src/LERN.php @@ -123,6 +123,26 @@ public function setRecorder(Recorder $recorder) return $this; } + /** + * Get the log level + * @return string + */ + public function getLogLevel() + { + return $this->notifier->getLogLevel(); + } + + /** + * Set the log level + * @param string $level The log level + * @return \Tylercd100\LERN\LERN + */ + public function setLogLevel($level) + { + $this->notifier->setLogLevel($level); + return $this; + } + /** * Set a string or a closure to be called that will generate the message body for the notification * @param function|string $cb This closure function will be passed an Exception and must return a string diff --git a/tests/LERNTest.php b/tests/LERNTest.php index a57179a..30c079e 100755 --- a/tests/LERNTest.php +++ b/tests/LERNTest.php @@ -94,6 +94,17 @@ public function testItCallsNotifierSetMessageMethod() $lern->setMessage("Test Message"); } + public function testItCallsNotifierSetLogLevelMethod() + { + $mock = $this->getMockBuilder('Tylercd100\LERN\Components\Notifier')->setMethods(array('setLogLevel'))->getMock(); + + $mock->expects($this->once()) + ->method('setLogLevel'); + + $lern = new LERN($mock); + $lern->setLogLevel("debug"); + } + public function testSettingAndGettingACustomNotifierInstance() { $lern = new LERN; @@ -111,4 +122,13 @@ public function testSettingAndGettingACustomRecorderInstance() $new_recorder = $lern->getRecorder(); $this->assertEquals($new_recorder, $orig_recorder); } + + public function testSettingAndGettingLogLevels() + { + $lern = new LERN; + $level = "debug"; + $lern->setLogLevel($level); + $result = $lern->getLogLevel(); + $this->assertEquals($result, $level); + } }