【问题标题】:Issue with zend 2 logZend 2日志问题
【发布时间】:2015-03-20 05:44:15
【问题描述】:

我目前正在使用 zend 2 记录器。我有一个表error_log,其中包含以下字段:

  • error_log_id
  • 留言
  • 时间戳
  • 优先级名称
  • 优先级
  • ip
  • user_id

以下是我的代码

$db         = $this->service->get('Zend\Db\Adapter\Adapter');
$uId        = 0;

if ($auth->hasIdentity()) {
    $oId        = $auth->getIdentity();
    $uId        = $oId->user_id;
}

$mapping = array(
    'timestamp'     => 'timestamp',
    'priority'      => 'priority',
    'priorityName'  => 'priorityName',
    'message'       => 'message',
);
$writer = new \Zend\Log\Writer\Db($db, 'error_log_table', $mapping);
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);

$logger->info('Informational message');

这工作正常,以下字段将被更新。时间戳、优先级、priorityName 和消息。

我还想更新 user_id 和 ip 字段。我怎样才能做到这一点? (在 zend 1 中,我们可以使用 setEventItem() 函数来做到这一点)。我必须调用什么函数才能在 zend 2 日志中添加额外的参数?

有人帮忙吗?

【问题讨论】:

    标签: zend-framework2 zend-log


    【解决方案1】:

    您可以将 $logger->info() 与第二个参数“extra”一起使用。这应该是一个遍历,例如数组。

    试试下面的。

    $logger->info('message', array('user_id' => $uId, 'ip' => $_SERVER['REMOTE_ADDR']));
    

    我猜你必须添加一个额外的数据库列“extra”,额外的日志事件数据存储在其中。

    【讨论】:

    • 我使用上面的代码,我也添加了下面的代码..$mapping = array( 'timestamp' => 'timestamp', 'priority' => 'priority', 'priorityName' => 'priorityName', 'message' => 'message', 'extra' => array('user_id'=> 'user_id', 'ip'=>'ip') ); $writer = new \Zend\Log\Writer\Db($db, 'error_log_table', $mapping);现在它工作正常..
    【解决方案2】:

    简而言之:声明自己的 logger 依赖于 AuthenticationService,覆盖 log 方法,使用 $extra 变量存储附加数据

    详细说明:

    Application\Log\LoggerFactory.php

    class LoggerFactory implements FactoryInterface
    {
        protected $mapping = array(
            // Your mapping here
        );
    
        public function createService(ServiceLocatorInterface $serviceLocator)
        {
            $authenticationService = $serviceLocator->get('Zend\Authentication\AuthenticationService');
    
            $dbAdapter = $serviceLocator->get('Zend\Db\Adapter\StatisticAdapter');
            $writer = new DbWriter($dbAdapter, 'error_log_table', $this->mapping);
    
            $logger = new Logger();
            $logger->addWriter($writer);
    
            $logger->setAuthenticationService($authenticationService);
    
            return $logger;
        }
    
    }
    

    Application\Log\Logger.php

    class Logger extends \Zend\Log\Logger
    {
    
        protected $authenticationService;
    
        public function setAuthenticationService($authenticationService)
        {
             $this->authenticationService = $authenticationService;
        }
    
        public function log($priority, $message, $extra = array())
        {
    
            $remoteAddress = new RemoteAddress;
            $ip = $remoteAddress->setUseProxy(true)->getIpAddress();   
    
            $userId = null;
            if ($this->authenticationService->hasIdentity()) {
                 $userId = $this->authenticationService->getIdentity()->getId();
            } 
    
            $extra = array_merge($extra, array(
                'ip' => ip2long($ip),
                'user_id' => $userId
            ));
    
            return parent::log($priority, $message, $extra);
        }
    

    module.config.php

    'service_manager' => array(
        'factories' => array(
            'Zend\Log' => 'Application\Log\LoggerFactory',
        ),
    

    您的控制器

    $this->getServiceLocator()->get('Zend\Log')->info('Info');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      相关资源
      最近更新 更多