Skip to content

Commit

Permalink
Custom Log Level (#35)
Browse files Browse the repository at this point in the history
* First commit for 3.6.0

* Removed undefined index error

* Added tests

* corrected config and tests

* updated travis.yml

* updated travis.yml

* updated travis.yml
  • Loading branch information
tylercd100 authored Jul 25, 2016
1 parent fc71b92 commit aab1503
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 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.

### 3.6.0
- Added a log_level option in the config to set the desired log level

### 3.5.0
- Added Mailgun support

Expand Down
6 changes: 6 additions & 0 deletions config/lern.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

return [


'record'=>[
'table'=>'vendor_tylercd100_lern_exceptions',
'collect'=>[
Expand All @@ -26,6 +27,11 @@
*/
'channel'=>'Tylercd100\LERN',

/**
* The log level to use when notifying
*/
'log_level' => 'critical', //Options are: debug, info, notice, warning, error, critical, alert, emergency.

/**
* When using the default message body this will also include the stack trace
*/
Expand Down
8 changes: 6 additions & 2 deletions src/Components/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function setContext($cb)
*/
public function getContext(Exception $e, $context = []) {

//This needs a better solution. How do I specific context needs for different drivers?
//This needs a better solution. How do I set specific context needs for different drivers?
if (in_array('pushover', $this->config['drivers'])) {
$context['sound'] = $this->config['pushover']['sound'];
}
Expand Down Expand Up @@ -154,7 +154,11 @@ public function send(Exception $e, array $context = []) {
try {
$notify = new Notify($this->config, $this->log, $subject);

$notify->critical($message, $context);
$level = (array_key_exists('log_level', $this->config) && !empty($this->config['log_level']))
? $this->config['log_level']
: 'critical';

$notify->{$level}($message, $context);

return true;
} catch (Exception $e) {
Expand Down
18 changes: 18 additions & 0 deletions tests/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ public function testAllSupportedDrivers()
$subject->send(new Exception);
}

public function testSendsDifferentLogLevels()
{
$logLevels = ['debug', 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency'];

$this->app['config']->set('lern.notify.drivers', ['slack']);

foreach($logLevels as $logLevel){
$this->app['config']->set('lern.notify.log_level', $logLevel);

$observer = $this->getMock('Monolog\Logger',[$logLevel],['channelName']);
$observer->expects($this->once())
->method($logLevel);

$subject = new Notifier($observer);
$subject->send(new Exception);
}
}

public function testLoggerCallsAddsError()
{
$this->app['config']->set('lern.notify.drivers', ['slack','pushover']);
Expand Down

0 comments on commit aab1503

Please sign in to comment.