2015-10-05 183 views
0

我在Zend Framework 2中開發,並希望擴展由請求url定製的日誌記錄。ZF2日誌記錄:將自定義信息添加到日誌記錄輸出

這樣做的最佳方式是什麼?我可以將$ _REQUEST onBootstrap傳遞給記錄器,並通過覆蓋記錄器類來利用它?

的記錄應該是這樣的:

2015-10-05T09:52:49+02:00 CRIT (2): Log Message, Request URL: http://www.website.com/page?field=value&.... 

的module.config看起來如下:

'log' => array(

     'Log\ErrorHandler' => array(
      'writers' => array(
       array(
        'name' => 'stream', 
        'options' => array(
         'stream' => 'logs/php.log', 
        ) 
       ) 
      ), 
     ), 

     'Log\ExceptionHandler' => array(
      'writers' => array(
       array(
        'name' => 'stream', 
        'options' => array(
         'stream' => 'logs/exception.log', 
        ) 
       ) 
      ), 
     ), 

     'Log\Main' => array(
      'writers' => array(
       array(
        'name' => 'stream', 
        'options' => array(
         'stream' => 'logs/main.log', 
        ) 
       ) 
      ), 
     ), 

    ) 

或者我可以掛接到的所有記錄的地方爲請求添加的額外陣列狀你可以做什麼當你登錄的東西:

$this->getServiceLocator()->get('Log\Main')->crit('Log Message', ['request' => $this->getRequest()]); 

回答

2

你可能想要添加一個程序發送到您的日誌服務。

$logger->addProcessor(new LogExtra()); 

class LogExtra implements ProcessorInterface 
{ 
    public function process(array $event) 
    { 
     if (!isset($event['extra'])) { 
      $event['extra'] = array(); 
     } 

     $event['extra']['request'] = // value you want to log 
     return $event; 
    } 
} 
1

謝謝@ Ed209, 你的回答給我帶來了正確的軌道上。

我已經添加了新的處理器,並且還實現了ServiceLocatorAwareInterface:

<?php 
namespace Your\Namespace\ToProcessor; 

use Zend\Log\Processor\ProcessorInterface; 
use Zend\ServiceManager\ServiceLocatorAwareInterface; 
use Zend\ServiceManager\ServiceLocatorInterface; 

/** 
* Class Request 
* 
* Processor adds some request information to the logger extra 
* 
*/ 
class RequestInformation implements ServiceLocatorAwareInterface, ProcessorInterface 
{ 
    /** 
    * Service Locator 
    * 
    * @var \Zend\Di\ServiceLocator 
    */ 
    protected $serviceLocator = null; 

    /** 
    * Processes the given event and adds request information to the extras 
    * 
    * @param array $event 
    * @return array 
    */ 
    public function process(array $event) 
    { 
     if (!isset($event['extra'])) { 
      $event['extra'] = array(); 
     } 

     $request = $this->getServiceLocator()->getServiceLocator()->get('Request'); 

     if ($request instanceof \Zend\Http\Request) { 
      $event['extra']['requestUrl'] = $request->getUriString(); 
     } elseif ($request instanceof \Zend\Console\Request) { 
      $event['extra']['consoleParameters'] = $request->toString(); 
     } 

     return $event; 
    } 

    /** 
    * Set service locator 
    * 
    * @param ServiceLocatorInterface $serviceLocator 
    */ 
    public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { 
     $this->serviceLocator = $serviceLocator; 
    } 

    /** 
    * Get service locator 
    * 
    * @return ServiceLocatorInterface 
    */ 
    public function getServiceLocator() { 
     return $this->serviceLocator; 
    } 

} 

和改變了我module.config的配置:

'登錄'=>數組(

'Log\Main' => array(
     'writers' => array(
      array(
       'name' => 'stream', 
       'options' => array(
        'stream' => 'logs/main.log', 
       ) 
      ) 
     ), 
     'processors' => array(
      array(
       'name' => '\PathToProcessor\RequestInformation' 
      ) 
     ) 
    ), 
)