Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

use Exception;
use Bootstrap;
use MonologHandlerRotatingFileHandler;
use MonologLogger as MonologLogger;
use MonologHandlerStreamHandler;

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
355 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...