简体   繁体   中英

Rotate logs with standalone Monolog Library

I work with a legacy project. I had added monolog logger as a standalone library (not with Symfony) and need to implement log rotation.

Bellow is a Logger class and I am not sure how to implement log rotation functionality.

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

Looking at the code ( https://github.com/Seldaek/monolog/blob/main/src/Monolog/Handler/RotatingFileHandler.php )...

class RotatingFileHandler extends StreamHandler

so if you pass an instance of RotatingFileHandler to the pushHandler , then this should work...

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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