Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/zendframework/zendframework#4455-logger-manager…
Browse files Browse the repository at this point in the history
…-writer-and-processor-config' into develop

Close zendframework/zendframework#4455
  • Loading branch information
Ocramius committed Mar 10, 2014
162 parents 4a22b91 + e9f04d9 + 4112f92 + 6faf2bb + df3a849 + ee5efcb + 2ee5d16 + bcb4d03 + a5c61e2 + 57ffceb + a6fa26b + 9d36595 + bf0040c + 71b1a95 + 5619ba3 + c093ab0 + de13946 + 04b025f + 0933607 + 34ed3ed + 83f6472 + db74e8c + 7b75da1 + bb4c48f + 9fa3c05 + 139c0b1 + a587eef + 7a782f4 + 39cb2e2 + 31f8c80 + a9b7779 + de058f6 + 67b97f6 + 40d86d0 + b5234cc + 17b4e13 + 8af24aa + 1a9df55 + 2c85c99 + 5557fac + fe78a35 + f6430c3 + 6041a1e + 86b9031 + 674bd75 + 67a1bfb + 4d4dd8b + c655082 + dbb18b2 + 97a9134 + 80367df + 98d5255 + 7946c9f + ea67c13 + 44df0b2 + 38e090d + 7af74e7 + 8b311b1 + e2c2b94 + 51601cb + dcc1eaf + 6f4b805 + a30a64f + ee7347d + 8b04bfe + b487f00 + ecdfdf6 + d8cde75 + b7a33bb + 65ecb58 + 571d938 + 625a786 + 6fe4efc + f94838d + 76607e3 + 492076c + 17b56b8 + c5509fe + e0fa433 + 7d74e99 + 309136e + f1dd38a + 7be533f + 6a618c2 + 3a454e3 + f66ca80 + f1bebf2 + 850249d + 674d7ba + 597ff82 + ae87c49 + fb156fb + 9c474de + 1a75c72 + 47d1cd2 + a937ea3 + 4219922 + 49e380f + 5903df6 + 7a9d632 + 2ef7274 + b606220 + a09c5c7 + 21afa5c + 8faa67f + 1731915 + efa1bb3 + 5576455 + 86d3ae8 + de3fd70 + 398a3c4 + 5c56554 + 7ad896f + fa91c9b + 1372a45 + a0c8737 + e368d53 + 462b1f7 + 79675fe + a8104b9 + 91ed9d0 + b1d1358 + 6a0c90c + 5e4258f + 239c0d0 + b457c76 + a2533a4 + beb6598 + 79d0f46 + 1519723 + 5084e3b + ce75f80 + e5fd689 + 6744e82 + 6cb64b8 + 03c6da8 + 7dcfd8f + a18fe5d + 5e5ada1 + 8e92f40 + 77fde49 + 0d988ee + 49bc0b3 + 975bb98 + cadc7b4 + a8af3df + ef19f19 + da1eb0c + fe5af05 + bba7d3a + 91ae243 + 1b1861e + 81c3d1e + a65de03 + ebb4db5 + d074ac7 + d4876c1 + c35719b + 8250024 + 805fd63 + 5c3d30d + 431bf4c commit cb3a6e2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DateTime;
use ErrorException;
use Traversable;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\SplPriorityQueue;

Expand Down Expand Up @@ -147,6 +148,20 @@ public function __construct($options = null)
throw new Exception\InvalidArgumentException('Options must be an array or an object implementing \Traversable ');
}

// Inject writer plugin manager, if available
if (isset($options['writer_plugin_manager'])
&& $options['writer_plugin_manager'] instanceof AbstractPluginManager
) {
$this->setWriterPluginManager($options['writer_plugin_manager']);
}

// Inject processor plugin manager, if available
if (isset($options['processor_plugin_manager'])
&& $options['processor_plugin_manager'] instanceof AbstractPluginManager
) {
$this->setProcessorPluginManager($options['processor_plugin_manager']);
}

if (isset($options['writers']) && is_array($options['writers'])) {
foreach ($options['writers'] as $writer) {
if (!isset($writer['name'])) {
Expand Down
29 changes: 29 additions & 0 deletions src/LoggerAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Log;

use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
Expand Down Expand Up @@ -90,6 +91,34 @@ protected function getConfig(ServiceLocatorInterface $services)

protected function processConfig(&$config, ServiceLocatorInterface $services)
{
if (isset($config['writer_plugin_manager'])
&& is_string($config['writer_plugin_manager'])
&& $services->has($config['writer_plugin_manager'])
) {
$config['writer_plugin_manager'] = $services->get($config['writer_plugin_manager']);
}

if ((!isset($config['writer_plugin_manager'])
|| ! $config['writer_plugin_manager'] instanceof AbstractPluginManager)
&& $services->has('LogWriterManager')
) {
$config['writer_plugin_manager'] = $services->get('LogWriterManager');
}

if (isset($config['processor_plugin_manager'])
&& is_string($config['processor_plugin_manager'])
&& $services->has($config['processor_plugin_manager'])
) {
$config['processor_plugin_manager'] = $services->get($config['processor_plugin_manager']);
}

if ((!isset($config['processor_plugin_manager'])
|| ! $config['processor_plugin_manager'] instanceof AbstractPluginManager)
&& $services->has('LogProcessorManager')
) {
$config['processor_plugin_manager'] = $services->get('LogProcessorManager');
}

if (!isset($config['writers'])) {
return;
}
Expand Down
59 changes: 59 additions & 0 deletions test/LoggerAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace ZendTest\Log;

use Zend\Log\ProcessorPluginManager;
use Zend\Log\WriterPluginManager;
use Zend\Log\Writer\Db as DbWriter;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
Expand Down Expand Up @@ -128,4 +130,61 @@ public function testRetrievesDatabaseServiceFromServiceManagerWhenEncounteringDb
$this->assertTrue($found, 'Did not find expected DB writer');
$this->assertAttributeSame($db, 'db', $writer);
}

/**
* @group 4455
*/
public function testWillInjectWriterPluginManagerIfAvailable()
{
$writers = new WriterPluginManager();
$mockWriter = $this->getMock('Zend\Log\Writer\WriterInterface');
$writers->setService('CustomWriter', $mockWriter);

$services = new ServiceManager(new ServiceManagerConfig(array(
'abstract_factories' => array('Zend\Log\LoggerAbstractServiceFactory'),
)));
$services->setService('LogWriterManager', $writers);
$services->setService('Config', array(
'log' => array(
'Application\Frontend' => array(
'writers' => array(array('name' => 'CustomWriter')),
),
),
));

$log = $services->get('Application\Frontend');
$logWriters = $log->getWriters();
$this->assertEquals(1, count($logWriters));
$writer = $logWriters->current();
$this->assertSame($mockWriter, $writer);
}

/**
* @group 4455
*/
public function testWillInjectProcessorPluginManagerIfAvailable()
{
$processors = new ProcessorPluginManager();
$mockProcessor = $this->getMock('Zend\Log\Processor\ProcessorInterface');
$processors->setService('CustomProcessor', $mockProcessor);

$services = new ServiceManager(new ServiceManagerConfig(array(
'abstract_factories' => array('Zend\Log\LoggerAbstractServiceFactory'),
)));
$services->setService('LogProcessorManager', $processors);
$services->setService('Config', array(
'log' => array(
'Application\Frontend' => array(
'writers' => array(array('name' => 'Null')),
'processors' => array(array('name' => 'CustomProcessor')),
),
),
));

$log = $services->get('Application\Frontend');
$logProcessors = $log->getProcessors();
$this->assertEquals(1, count($logProcessors));
$processor = $logProcessors->current();
$this->assertSame($mockProcessor, $processor);
}
}

0 comments on commit cb3a6e2

Please sign in to comment.