【问题标题】:Get Monolog to log to an Array获取 Monolog 以记录到数组
【发布时间】:2016-06-22 05:37:53
【问题描述】:

我们有一个旧的应用程序部分还没有使用 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 debugging logging monolog


    【解决方案1】:

    对于具有相同逻辑柱的 thouse 来说还可以。 我使用自定义的自定义处理程序做到了:

    <?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;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 2021-09-04
      • 2016-12-15
      • 2018-07-24
      • 2020-07-04
      相关资源
      最近更新 更多