[英]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.