Skip to content

Commit

Permalink
Merge pull request #19 from rantonmattei/monologger
Browse files Browse the repository at this point in the history
Made a 'create' factory method instead of inheriting from \Monolog\Lo…
  • Loading branch information
rantonmattei committed Jun 18, 2015
2 parents bc02f7a + 8ca2926 commit 74dd158
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/MonoLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
class MonoLogger extends Logger
class MonoLogger
{
/**
* Config class that holds options for all registered loggers
Expand All @@ -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
*
Expand All @@ -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()
Expand All @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand Down
10 changes: 8 additions & 2 deletions tests/MonoLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 74dd158

Please sign in to comment.