Skip to content

Commit

Permalink
Change default logging to disabled, custom options
Browse files Browse the repository at this point in the history
Previously, the default logging state was all Errors, logged to file.
This has been changed so that logging is disabled by default (using
Analog's Null handler).

In addition, the Analog handler has been exposed through the settings, so
that a user may specify a custom handler (syslog, etc) instead of the
default file handler.

Closes #12
  • Loading branch information
polyfractal committed Mar 10, 2013
1 parent 12cb2f2 commit 474fa95
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Sherlock/Sherlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,15 @@ public static function registerAutoloader()
/**
* @return array Default settings
*/
public static function getDefaultSettings()
private static function getDefaultSettings()
{
return array(
// Application
'base' => __DIR__.'/',
'mode' => 'development',
'log.enabled' => false,
'log.level' => 'error',
'log.handler' => null,
'log.file' => '../sherlock.log',
'autodetect.cluster' => false
);
Expand Down Expand Up @@ -249,8 +251,13 @@ public function addNode($host, $port = 9200)
return $this;
}



/**
* @return array
*/
public function getSherlockSettings()
{
return $this->settings;
}



Expand Down Expand Up @@ -325,8 +332,19 @@ private function setupLogging()
$level = Analog::ALERT;
break;
}
Analog::handler(\Analog\Handler\Threshold::init (\Analog\Handler\File::init ($this->settings['base'] . $this->settings['log.file']),$level));
//Analog::handler(\Analog\Handler\LevelBuffer::init (\Analog\Handler\File::init ($this->settings['base'] . $this->settings['log.file']),$level));

//if logging is disabled, use the null Analog logger
if ($this->settings['log.enabled'] == false)
$this->settings['log.handler'] = \Analog\Handler\Null::init();
else
{
//if logging is enabled but no handler was specified by the user
//use the default file logger
if ($this->settings['log.handler'] === null)
$this->settings['log.handler'] = \Analog\Handler\Threshold::init (\Analog\Handler\File::init ($this->settings['base'] . $this->settings['log.file']),$level);
}
Analog::handler($this->settings['log.handler']);

Analog::log("--------------------------------------------------------", Analog::ALERT);
Analog::log("Logging setup at ".date("Y-m-d H:i:s.u"), Analog::INFO);
}
Expand Down
99 changes: 99 additions & 0 deletions tests/SherlockSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* User: Zachary Tong
* Date: 3/10/13
* Time: 10:36 AM
*/

namespace Sherlock\tests;
use Analog\Analog;
use Sherlock\Sherlock;

class SherlockSettingsTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Sherlock
*/
protected $object;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{

}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{

}

function assertThrowsException($exception_name, $code) {
$e = null;
try{
$code();
}catch (\Exception $e) {
// No more code, we only want to catch the exception in $e
}

$this->assertInstanceOf($exception_name, $e);
}


public function testSettings()
{
$sherlock = new Sherlock();
$data = $sherlock->getSherlockSettings();

//Check default Null handler
$settings = array();
$settings['log.enabled'] = false;
$sherlock = new Sherlock($settings);
$data = $sherlock->getSherlockSettings();
$this->assertEquals($settings['log.enabled'], $data['log.enabled']);

$nullHandler = \Analog\Handler\Null::init();
$this->assertEquals($nullHandler, $data['log.handler']);


//Check default File handler
$settings = array();
$settings['log.enabled'] = true;
$sherlock = new Sherlock($settings);
$data = $sherlock->getSherlockSettings();
$this->assertEquals($settings['log.enabled'], $data['log.enabled']);

$fileHandler = \Analog\Handler\Threshold::init (\Analog\Handler\File::init ($data['base'] .$data['log.file']), Analog::ERROR);
$this->assertEquals($fileHandler, $data['log.handler']);


//Check default File handler with custom path
$settings = array();
$settings['log.enabled'] = true;
$settings['log.file'] = '../sherlock.log';
$sherlock = new Sherlock($settings);
$data = $sherlock->getSherlockSettings();
$this->assertEquals($settings['log.file'], $data['log.file']);

$fileHandler = \Analog\Handler\Threshold::init (\Analog\Handler\File::init ($data['base'] .$data['log.file']), Analog::ERROR);
$this->assertEquals($fileHandler, $data['log.handler']);


//Check custom handler (syslog)
$syslogHandler = \Analog\Handler\Syslog::init ('analog', 'user');

$settings = array();
$settings['log.enabled'] = true;
$settings['log.handler'] = $syslogHandler;
$sherlock = new Sherlock($settings);
$data = $sherlock->getSherlockSettings();

$this->assertEquals($syslogHandler, $data['log.handler']);
}
}

0 comments on commit 474fa95

Please sign in to comment.