Skip to content

Commit

Permalink
Added setLogLevel and getLogLevel functions (#62)
Browse files Browse the repository at this point in the history
* - Added setLogLevel and getLogLevel functions

* fixed test file
  • Loading branch information
tylercd100 authored Feb 16, 2018
1 parent e0784ca commit 9525131
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/Components/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/LERN.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/LERNTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

0 comments on commit 9525131

Please sign in to comment.