Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a default logger routed to the error log #35

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Expose/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Expose;

use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger;
use Psr\Log\NullLogger;

class Manager
{
/**
Expand Down Expand Up @@ -98,6 +102,10 @@ public function __construct(
if ($logger !== null) {
$this->setLogger($logger);
}
else {
$monolog = new Logger('expose', array(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Logger::WARNING)));
$this->setLogger($monolog);
}
if ($queue !== null) {
$this->setQueue($queue);
}
Expand Down
62 changes: 49 additions & 13 deletions tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace Expose;

use Expose\Exception\LoggerNotDefined;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger;

require_once 'MockLogger.php';
require_once 'MockQueue.php';

Expand All @@ -21,17 +25,17 @@ class ManagerTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$logger = new \Expose\Log\Mongo();
$filters = new \Expose\FilterCollection();
$this->manager = new \Expose\Manager($filters, $logger);
$filters = new FilterCollection();
$this->manager = new Manager($filters, $logger);
}

public function executeFilters($data, $queue = false, $notify = false)
{
$filterCollection = new \Expose\FilterCollection();
$filterCollection = new FilterCollection();
$filterCollection->setFilterData($this->sampleFilters);

$logger = new MockLogger();
$manager = new \Expose\Manager($filterCollection, $logger);
$manager = new Manager($filterCollection, $logger);
$manager->setConfig(array('test' => 'foo'));
$manager->run($data, $queue, $notify);

Expand All @@ -47,7 +51,7 @@ public function executeFilters($data, $queue = false, $notify = false)
*/
public function testGetSetFilters()
{
$filters = new \Expose\FilterCollection();
$filters = new FilterCollection();
$filters->setFilterData($this->sampleFilters);

$this->manager->setFilters($filters);
Expand Down Expand Up @@ -127,11 +131,11 @@ public function testRunSuccessWithImpactLimit()
)
);

$filterCollection = new \Expose\FilterCollection();
$filterCollection = new FilterCollection();
$filterCollection->setFilterData($this->sampleFilters);

$logger = new MockLogger();
$manager = new \Expose\Manager($filterCollection, $logger);
$manager = new Manager($filterCollection, $logger);
$manager->setImpactLimit(1);
$manager->setConfig(array('test' => 'foo'));
$manager->run($data, false, false);
Expand Down Expand Up @@ -273,11 +277,11 @@ public function testSetupConfigArray()
*/
public function testExceptionIsIgnored()
{
$filterCollection = new \Expose\FilterCollection();
$filterCollection = new FilterCollection();
$filterCollection->setFilterData($this->sampleFilters);

$logger = new MockLogger();
$manager = new \Expose\Manager($filterCollection, $logger);
$manager = new Manager($filterCollection, $logger);
$manager->setConfig(array('test' => 'foo'));
$manager->setException('POST.foo');
$manager->setException('POST.bar.baz');
Expand All @@ -303,11 +307,11 @@ public function testExceptionIsIgnored()
*/
public function testExceptionWildcardIsIgnored()
{
$filterCollection = new \Expose\FilterCollection();
$filterCollection = new FilterCollection();
$filterCollection->setFilterData($this->sampleFilters);

$logger = new MockLogger();
$manager = new \Expose\Manager($filterCollection, $logger);
$manager = new Manager($filterCollection, $logger);
$manager->setConfig(array('test' => 'foo'));
$manager->setException('POST.foo[0-9]+');

Expand Down Expand Up @@ -372,7 +376,7 @@ public function testThresholdLowerThenImpact() {
$filter = new \Expose\Filter();
$filter->setImpact(100);

$collection = new \Expose\FilterCollection();
$collection = new FilterCollection();
$collection->addFilter($filter);

$manager_mock = $this->getMockBuilder('\\Expose\\Manager')
Expand All @@ -393,7 +397,7 @@ public function testThresholdHigherThenImpact() {
$filter = new \Expose\Filter();
$filter->setImpact(5);

$collection = new \Expose\FilterCollection();
$collection = new FilterCollection();
$collection->addFilter($filter);

$manager_mock = $this->getMockBuilder('\\Expose\\Manager')
Expand All @@ -408,4 +412,36 @@ public function testThresholdHigherThenImpact() {
$manager_mock->setThreshold(100);
$manager_mock->run(array('test' => 'test'), false, true);
}

/**
* Test that a proper default ErrorLogHandler is set.
*/
public function testLoggerDefaultErrorLog() {
$filters = new FilterCollection();
$filters->load();

$manager = new Manager($filters);
/** @var Logger $logger */
$logger = $manager->getLogger();
$handlers = $logger->getHandlers();
$this->assertInstanceOf('Monolog\Handler\ErrorLogHandler', $handlers[0], '');
}

/**
* Tests a null logger does not throw exceptions on run().
*/
public function testLoggerUndefined() {
$filters = new FilterCollection();
$filters->load();

$manager = new Manager($filters);
$manager->setThreshold(0);
try {
$result = $manager->run(array('test'), FALSE, TRUE);
$this->assertNotEmpty($result, 'Results returned with an undefined logger');
}
catch (LoggerNotDefined $e) {
$this->fail('Undefined logger does not throw an exception');
}
}
}