deleting old log files while creating new one – Magento2


In my app I have got an handler for my custom log file:

<?php

namespace VendorAppLogger;

use MagentoFrameworkFilesystemDriverInterface;
use MonologFormatterLineFormatter;
use MonologHandlerStreamHandler;
use MonologLogger;
 
class Handler extends MagentoFrameworkLoggerHandlerBase
{
    protected $loggerType = Logger::INFO;
    public $fileName = '';
    public $customfileName = 'NO_PATH';
    protected $_localeDate;

    public function __construct(
        DriverInterface $filesystem,
        MagentoFrameworkFilesystem $corefilesystem,
        MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate,
        $filepath = null
    ) {
        $this->localeDate = $localeDate;
        $corefilesystem = $corefilesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::VAR_DIR);
        $logpath = $corefilesystem->getAbsolutePath('log/');
        $filename = 'f24_trace_'. Date('d_m_Y'). '.log';
        $filepath = $logpath . $filename;
        $this->customfileName = $filepath;
        parent::__construct(
            $filesystem,
            $filepath
        );
        //$this->setFormatter(new LineFormatter('%message%'. PHP_EOL, null, true));
    }
}

I’d like to add a logic to delete log files older than seven days, like in this sample code:

$today = DateTime::createFromFormat('d_m_Y', date('d_m_Y'));
    foreach (glob($filepath. '*24*.log') as $file) {
        $creationDay = DateTime::createFromFormat('d_m_Y', date('d_m_Y', filectime($file)));
        $result = $creationDay->diff($today);
        $different_month = $result->m;
        $different_days = $result->d;
        if ($different_days > 7) {
            unlink($file);
        }
    }

Any suggestions?
Thanks