简体   繁体   English

获取 Monolog 以登录到数组

[英]Get Monolog to log to an Array

We have an old Application part which does not use Monolog yet.我们有一个不使用 Monolog 的旧应用程序部分。 This application needs on one time the whole output from the log so it can print it out in a hidden div visible only for developers.此应用程序一次性需要日志的整个输出,以便它可以将其打印在仅对开发人员可见的隐藏 div 中。

Much like live debug...很像实时调试...
The problem is I can't figure out how to get Monolog to log to an array or set the handler for a local variable, or to get the output from the log on a particular part of code.问题是我不知道如何让 Monolog 记录到数组或设置局部变量的处理程序,或者从代码的特定部分的日志中获取输出。

This is what I came up with until now:这是我想出的直到现在:

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

Anyone any idea which handler is suitable for that?有人知道哪个处理程序适合吗? (examples are welcome) (欢迎举例)

Ok for thouse who have the same logical columdrum.对于具有相同逻辑柱的人来说还可以。 I did it with a custom custom handler:我用一个自定义的自定义处理程序做到了:

<?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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM