使用独立的 Monolog 库旋转日志

Rotate logs with standalone Monolog Library

我在处理遗留项目。 我已经将 monolog 记录器添加为独立库(不使用 Symfony)并且需要实现日志轮换。

Bellow 是一个 Logger class,我不确定如何实现日志轮换功能。

namespace Utils\Logger;

use Exception;
use Bootstrap;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger as MonologLogger;
use Monolog\Handler\StreamHandler;

/**
 * Class Logger
 */
class Logger
{
    /** @var MonologLogger $logger */
    private $logger;
    const DEBUG    = MonologLogger::DEBUG;
    const NOTICE   = MonologLogger::NOTICE;
    const WARNING  = MonologLogger::WARNING;
    const ERROR    = MonologLogger::ERROR;
    const CRITICAL = MonologLogger::CRITICAL;

    public function __construct()
    {
        $bootstrap  = Bootstrap::getInstance();
        $projectDir = $bootstrap->getProjectDir();
        $host       = $bootstrap->initHost();
        $domain     = $host->getDomain();

        $this->logger = new MonologLogger($host->getDomain());
        $this->logger->
        $this->logger->pushHandler(
            new StreamHandler(
                "$projectDir/var/log/$domain/$domain.log",
                MonologLogger::DEBUG)
        );

        $logRotate = new RotatingFileHandler("$projectDir/var/log/$domain/$domain.log");
    }

    /**
     * @param $level
     * @param $message
     * @param $context
     *
     * @return void
     */
    public function log($level, $message, array $context = [])
    {
        $message   = \is_string($message) ? $message : '';
        $exception = $this->getException($context);

        if ($exception) {
            $message = "{$exception->getMessage()} at line {$exception->getLine()} in file {$exception->getFile()} Code: {$exception->getCode()}";
        }

        switch ($level) {
            case self::DEBUG:
                $this->logger->debug($message);
                break;
            case self::NOTICE:
                $this->logger->notice($message);
                break;
            case self::WARNING:
                $this->logger->warning($message);
                break;
            case self::ERROR:
                $this->logger->error($message);
                break;
            case self::CRITICAL:
                $this->logger->critical($message);
                break;
            default:
                $this->logger->info($message);
                break;
        }
    }

    /**
     * @param array $context
     *
     * @return Exception|null
     */
    private function getException($context)
    {
        return array_key_exists('exception', $context) && $context['exception'] instanceof Exception
            ? $context['exception']
            : null;
    }
}

查看代码 (https://github.com/Seldaek/monolog/blob/main/src/Monolog/Handler/RotatingFileHandler.php)...

class RotatingFileHandler extends StreamHandler

因此,如果您将 RotatingFileHandler 的实例传递给 pushHandler,那么这应该有效...

    $this->logger = new MonologLogger($host->getDomain());
    $logRotate = new RotatingFileHandler("$projectDir/var/log/$domain/$domain.log");        

    $this->logger->pushHandler($logRotate);