Skip to content

Commit

Permalink
LoggerConfig::getFor always returns the same instance of Logger for t…
Browse files Browse the repository at this point in the history
…he same $name.

This avoid issues when push handlers/processors to that logger.
Use the Monolog\ErrorHandler to log errors and exceptions.
PHPCI/Logging/Handler becomes PHPCI/ErrorHandler.
And it only throws ErrorException for reported errors.
No need to initialize a second $loggerConfig in daemonise.

Close #892
  • Loading branch information
Adirelle authored and tvbeek committed Apr 23, 2015
1 parent 5688d9c commit f46a8be
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 7 deletions.
68 changes: 68 additions & 0 deletions PHPCI/ErrorHandler.php
@@ -0,0 +1,68 @@
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/

namespace PHPCI;

/**
* Error Handler
*
* @package PHPCI\Logging
*/
class ErrorHandler
{
/**
* @var array
*/
protected $levels = array(
E_WARNING => 'Warning',
E_NOTICE => 'Notice',
E_USER_ERROR => 'User Error',
E_USER_WARNING => 'User Warning',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Runtime Notice',
E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
E_DEPRECATED => 'Deprecated',
E_USER_DEPRECATED => 'User Deprecated',
);

/**
* Registers an instance of the error handler to throw ErrorException.
*/
public static function register()
{
$handler = new static();
set_error_handler(array($handler, 'handleError'));
}

/**
* @param integer $level
* @param string $message
* @param string $file
* @param integer $line
*
* @throws \ErrorException
*
* @internal
*/
public function handleError($level, $message, $file, $line)
{
if (error_reporting() & $level === 0) {
return;
}

$exceptionLevel = isset($this->levels[$level]) ? $this->levels[$level] : $level;
throw new \ErrorException(
sprintf('%s: %s in %s line %d', $exceptionLevel, $message, $file, $line),
0,
$level,
$file,
$line
);
}
}
17 changes: 15 additions & 2 deletions PHPCI/Logging/LoggerConfig.php
Expand Up @@ -9,6 +9,7 @@


namespace PHPCI\Logging; namespace PHPCI\Logging;


use Monolog\ErrorHandler;
use Monolog\Logger; use Monolog\Logger;


/** /**
Expand All @@ -19,6 +20,7 @@ class LoggerConfig
{ {
const KEY_ALWAYS_LOADED = "_"; const KEY_ALWAYS_LOADED = "_";
private $config; private $config;
private $cache = array();


/** /**
* The filepath is expected to return an array which will be * The filepath is expected to return an array which will be
Expand Down Expand Up @@ -56,9 +58,20 @@ public function __construct(array $configArray = array())
*/ */
public function getFor($name) public function getFor($name)
{ {
if (isset($this->cache[$name])) {
return $this->cache[$name];
}

$handlers = $this->getHandlers(self::KEY_ALWAYS_LOADED); $handlers = $this->getHandlers(self::KEY_ALWAYS_LOADED);
$handlers = array_merge($handlers, $this->getHandlers($name)); if ($name !== self::KEY_ALWAYS_LOADED) {
return new Logger($name, $handlers); $handlers = array_merge($handlers, $this->getHandlers($name));
}

$logger = new Logger($name, $handlers);
ErrorHandler::register($logger);
$this->cache[$name] = $logger;

return $logger;
} }


/** /**
Expand Down
11 changes: 10 additions & 1 deletion Tests/PHPCI/Logging/LoggerConfigTest.php
Expand Up @@ -81,5 +81,14 @@ public function testGetFor_IgnoresAlternativeHandlers()
$this->assertSame($expectedHandler, $actualHandler); $this->assertSame($expectedHandler, $actualHandler);
$this->assertNotSame($alternativeHandler, $actualHandler); $this->assertNotSame($alternativeHandler, $actualHandler);
} }
}


public function testGetFor_SameInstance()
{
$config = new LoggerConfig(array());

$logger1 = $config->getFor("something");
$logger2 = $config->getFor("something");

$this->assertSame($logger1, $logger2);
}
}
4 changes: 2 additions & 2 deletions bootstrap.php
Expand Up @@ -8,7 +8,6 @@
*/ */


// Let PHP take a guess as to the default timezone, if the user hasn't set one: // Let PHP take a guess as to the default timezone, if the user hasn't set one:
use PHPCI\Logging\Handler;
use PHPCI\Logging\LoggerConfig; use PHPCI\Logging\LoggerConfig;


$timezone = ini_get('date.timezone'); $timezone = ini_get('date.timezone');
Expand Down Expand Up @@ -43,9 +42,10 @@
// Load Composer autoloader: // Load Composer autoloader:
require_once(dirname(__FILE__) . '/vendor/autoload.php'); require_once(dirname(__FILE__) . '/vendor/autoload.php');


\PHPCI\ErrorHandler::register();

if (defined('PHPCI_IS_CONSOLE') && PHPCI_IS_CONSOLE) { if (defined('PHPCI_IS_CONSOLE') && PHPCI_IS_CONSOLE) {
$loggerConfig = LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php"); $loggerConfig = LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php");
Handler::register($loggerConfig->getFor('_'));
} }


// Load configuration if present: // Load configuration if present:
Expand Down
2 changes: 0 additions & 2 deletions daemonise
Expand Up @@ -15,8 +15,6 @@ require('bootstrap.php');
use PHPCI\Command\DaemoniseCommand; use PHPCI\Command\DaemoniseCommand;
use Symfony\Component\Console\Application; use Symfony\Component\Console\Application;


$loggerConfig = \PHPCI\Logging\LoggerConfig::newFromFile(__DIR__ . "/loggerconfig.php");

$application = new Application(); $application = new Application();
$application->add(new DaemoniseCommand($loggerConfig->getFor('DaemoniseCommand'))); $application->add(new DaemoniseCommand($loggerConfig->getFor('DaemoniseCommand')));
$application->run(); $application->run();

0 comments on commit f46a8be

Please sign in to comment.