From 8ca29265189e1ee2abb0d0d9f583dc78fda45e6f Mon Sep 17 00:00:00 2001 From: Raphael Antonmattei Date: Thu, 18 Jun 2015 10:56:42 -0400 Subject: [PATCH] Made a 'create' factory method instead of inheriting from \Monolog\Logger --- src/MonoLogger.php | 18 +++++++++++------- tests/MonoLoggerTest.php | 10 ++++++++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/MonoLogger.php b/src/MonoLogger.php index 8db7135..baa1966 100644 --- a/src/MonoLogger.php +++ b/src/MonoLogger.php @@ -10,11 +10,11 @@ /** * Module class that manages Monolog Logger object * @see Monolog\Logger + * @see Monolog\Registry * - * @todo remove inheritance. There is no need to extend Logger * @author Raphael Antonmattei */ -class MonoLogger extends Logger +class MonoLogger { /** * Config class that holds options for all registered loggers @@ -24,7 +24,7 @@ class MonoLogger extends Logger protected static $config = null; /** - * Instantiate a new MonoLogger object + * Create a new Logger object and push it to the registry * * @see Monolog\Logger::__construct * @@ -34,8 +34,10 @@ class MonoLogger extends Logger * @param callable[] $processors Optional array of processors * * @throws \InvalidArgumentException: if no name is given + * + * @return Monolog\Logger newly created Logger */ - public function __construct( + public static function createLogger( $name, array $handlers = array(), array $processors = array() @@ -45,8 +47,10 @@ public function __construct( throw new \InvalidArgumentException('Logger name is required.'); } - parent::__construct($name, $handlers, $processors); - Registry::addLogger($this); + $logger = new Logger($name, $handlers, $processors); + Registry::addLogger($logger); + + return $logger; } /** @@ -58,7 +62,7 @@ public function __construct( */ public static function getLogger($name) { - return Registry::hasLogger($name) ? Registry::getInstance($name) : new MonoLogger($name); + return Registry::hasLogger($name) ? Registry::getInstance($name) : self::createLogger($name); } /** diff --git a/tests/MonoLoggerTest.php b/tests/MonoLoggerTest.php index fe46c26..df0b9fc 100644 --- a/tests/MonoLoggerTest.php +++ b/tests/MonoLoggerTest.php @@ -20,13 +20,19 @@ public function teardown() parent::teardown(); } - public function testRegistry() + public function testCreateLogger() { - $logger = MonoLogger::getLogger('test'); + $logger = MonoLogger::createLogger('test'); $this->assertTrue($logger instanceof Logger); $this->assertEquals('test', $logger->getName()); $this->assertTrue(Registry::hasLogger('test')); + } + + public function testRegistry() + { + // Creates the logger and push it to the registry + $logger = MonoLogger::getLogger('test'); // We should get the logger from the registry this time $logger2 = MonoLogger::getLogger('test');