2016-03-08 48 views
1

我們有一箇舊的應用程序部分,至今沒有使用Monolog。此應用程序需要一次來自日誌的整個輸出,因此它可以將其打印出來,僅用於開發人員可見的隱藏div。獲取Monolog以登錄到陣列

很像現場調試... 問題是我不能弄清楚如何讓Monolog登錄到一個數組或設置一個局部變量的處理程序,或從日誌的輸出獲得特定部分代碼... 這就是我想出現在直到:

protected function getHandlers() 
    { 
     $handlers = array(); 

     $logsDir = $this->getLogsDir(); 
     $logFile = $logsDir . DIRECTORY_SEPARATOR . 'application.log'; 

     $logfileHandler = new \Monolog\Handler\FingersCrossedHandler($logFile, Logger::ERROR); 

     array_push($handlers, $logfileHandler); 


     // When in CLI, we're going to push the logs through STDERR as well 
     // This way, if needed, we can easily redirect STDERR to STDOUT or to some specified file 
     if (php_sapi_name() == 'cli') { 
      $stderrHandler = new StreamHandler('php://stderr', Logger::INFO); 
      array_push($handlers, $stderrHandler); 
     } 

     return $handlers; 
    } 

任何人任何想法至極處理程序是適合的? (歡迎舉例)

回答

1

對於具有相同邏輯柱的thouse來說還不錯。 我使用自定義自定義處理程序完成它:

<?php 

namespace Log\Handler; 

use Monolog\Logger; 
use Monolog\Handler\AbstractProcessingHandler; 

/** 
* Description of runtimeHandler 
* 
* @author Sinisa Culic <[email protected]> 
*/ 
class RuntimeHandler extends AbstractProcessingHandler 
{ 

    protected $log; 

    /** 
    * @param integer $level The minimum logging level at which this handler will be triggered 
    * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not 
    */ 
    public function __construct($level = Logger::DEBUG, $bubble = true) 
    { 
     parent::__construct($level, $bubble); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function close() 
    { 
     return $this->log; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    protected function write(array $record) 
    { 
     $this->log[] = $record; 
    } 

}