【问题标题】:Is there a way to add a log writer factory into the logger configuration?有没有办法将日志写入器工厂添加到记录器配置中?
【发布时间】:2013-05-09 20:47:31
【问题描述】:

我编写记录器配置并使用 Zend\Log\LoggerServiceFactory 来配置记录器。如果我使用基础作家所有工作。但是我想将我自己的由工厂创建的作家添加到记录器中,这是行不通的。

有没有办法使用 config 和 base logger factory 从自己的工厂添加 writer?

更新:这是我的代码

这是我的配置,我为 Logger 定义工厂,为编写器定义工厂,为基础编写器定义配置

// config/autoload/global.php

return array(
    'service_manager' => array(
        'factories' => array(
            'Logger'  => 'Zend\Log\LoggerServiceFactory',
            'Rollbar' => 'Yassa\Rollbar\Log\Writer\Rollbar'
        ),
    ),
    'log' => array(
        'writers' => array(
            array(
                'name' => 'stream',
                'options' => array(
                    ...
                ),
            ),
            array(
                'name' => 'stream',
                'options' => array(
                    ...
                ),
            ),
           array(
               'name' => 'Rollbar',
           ),
        ),
    ),
);

Yassa\Rollbar\Log\Writer\Rollbar - 它是 yassa\rollbar 模块 (github) 的工厂

如果没有 Rollbar 写入器,此配置可以满足我的需要 - 创建和配置标准写入器。

因此我从 aontroller 调用 logger:

$this->getServiceLocator()->get('Logger')->info('test');

【问题讨论】:

  • 也许你可以给我们看一些代码来帮助你
  • 我为延迟道歉。添加代码示例

标签: zend-framework2


【解决方案1】:

我研究了组件代码,但没有找到使用工厂创建自己的作家的方法。不幸的是,目前这是不可能的。

【讨论】:

    【解决方案2】:

    我想我找到了解决方案。解决方案不是很好,但我想通了。

    我为标准工厂 (\Zend\Log\LoggerServiceFactory) 编写了一个替代品,并从我工厂的 Logger 构造函数中传输记录器配置。另外,我添加了对 writer factory 的检查。

    这是生成的类:

    <?php
    namespace Application\Factory;
    
    use Zend\Log\Exception\InvalidArgumentException;
    use Zend\Log\Logger;
    use Zend\ServiceManager\FactoryInterface;
    use Zend\ServiceManager\ServiceLocatorInterface;
    
    class LoggerServiceFactory implements FactoryInterface
    {
        public function createService(ServiceLocatorInterface $serviceLocator)
        {
            // Configure the logger
            $config = $serviceLocator->get('Config');
            $options = isset($config['log']) ? $config['log'] : array();
            $logger = new Logger();
            if (is_array($options)) {
                if (isset($options['writers']) && is_array($options['writers'])) {
                    foreach ($options['writers'] as $writer) {
    
                        if (!isset($writer['name'])) {
                            throw new InvalidArgumentException('Options must contain a name for the writer');
                        }
    
                        $priority      = (isset($writer['priority'])) ? $writer['priority'] : null;
                        $writerOptions = (isset($writer['options'])) ? $writer['options'] : null;
    
                        if ($serviceLocator->has($writer['name'])) {
                            $writer = $serviceLocator->get($writer['name']);
                            $logger->addWriter($writer, $priority, $writerOptions);
                        } else {
                            $logger->addWriter($writer['name'], $priority, $writerOptions);
                        }
                    }
                }
    
                if (isset($options['exceptionhandler']) && $options['exceptionhandler'] === true) {
                    Logger::registerExceptionHandler($logger);
                }
    
                if (isset($options['errorhandler']) && $options['errorhandler'] === true) {
                    Logger::registerErrorHandler($logger);
                }
    
            }
            return $logger;
        }
    }
    

    谁能提出更好的解决方案?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      相关资源
      最近更新 更多