[英]Get Monolog to log to an Array
我們有一個不使用 Monolog 的舊應用程序部分。 此應用程序一次性需要日志的整個輸出,以便它可以將其打印在僅對開發人員可見的隱藏 div 中。
很像實時調試...
問題是我不知道如何讓 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;
}
有人知道哪個處理程序適合嗎? (歡迎舉例)
對於具有相同邏輯柱的人來說還可以。 我用一個自定義的自定義處理程序做到了:
<?php
namespace Log\Handler;
use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;
/**
* Description of runtimeHandler
*
* @author Sinisa Culic <sinisaculic@gmail.com>
*/
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.