Hi,
I suppose that most of you had some situation when logging is turned On on high traffic Magento site. If you need to check the logs using FTP access, sometimes that could be a time – consuming task because log files are large and we need to wait to download a complete file every time.
Of course, we can archive this files manually, but, why should we do that if we can create yet another Magento CRON task that will do it for us.

  • The following script will:
  • Check if defined files exist in log folder
  • Check each file size
  • If file size is bigger than 100Mb, then it will be archived
  • Rename file to _filenname to avoid duplicate processing
  • Compress file in tgz
  • Delete original file

Let’s add a new method to our Observer.php file:

//... Observer.php
  
    public function checkBackupLogFiles()
    {
        //Add here file names that we want to be hhandled with this script
        $logFileNames = array(
            'system.log',
            'error.log',
            'mmyCustomFile.log',
        );
        //Get Magento log dir path
        $logDir = Mage::getBaseDir('log');
  
        try {
  
            foreach ($logFileNames as $fileName) {
  
                //Get path to original log file
                $filePath = $logDir . DS . $fileName;
  
                //Prepare new file name - this is done to avoid
                //double backup of same file. We will rename file right away
                //as processing started, so if this script run twice,
                //original file name will not exist any more
                $renamedFilePath = $logDir . DS . '_' . $fileName;
  
                //Check if file exists. If not, just continue to next filename
                if ([email protected]_exists($filePath)) {
                    continue;
                }
  
                //We need to initiate Varien_File_Object for getting
                //file info (to get file size)
                $logFile = new Varien_File_Object($filePath);
  
                //Let's prepare compressed - archive file name / path
                //To avoid possible rewrite on disk, we will add date -time part
                //to file name
                $archiveFileName = sprintf('%s_%s.tgz', $fileName, date('Ymd_Hi'));
                $archiveFilePath = $logDir . DS . $archiveFileName;
  
                //Get file info object
                $fileInfo = $logFile->getFileInfo();
  
                //(Just in case)
                if (empty($fileInfo)) {
                    continue;
                }
  
                //Try to get file size
                $size = $fileInfo->getSize();
                if (!$size) {
                    continue;
                }
  
                $sizeKb = $size / 1024;
                $sizeMb = $sizeKb / 1024;
  
                //If file is smaller than 100Mb, just skip it
                if ($sizeMb < 100) { continue; } //We need to unset this to avoid file already in use //later when using rename function unset($logFile); //Try to rename file $result = rename($filePath, $renamedFilePath); if (!$result) { continue; } //Magento has nice library for compressing files, //let's use it $archive = new Mage_Archive(); $archive->pack($renamedFilePath, $archiveFilePath);
                //Just delete original - renamed log file
                unlink($renamedFilePath);
            }
  
        } Catch (Exception $e) {
            //Mage::logException($e);
        }
  
    }

That’s all folks. We just need to add new CRON job schedule in our config.xml that will run this method. 

<crontab>
   <jobs>
     <check_arhieve_log_files>
       <schedule>
          <cron_expr>0 1 * * *</cron_expr>
       </schedule>
       <run>
          <model>my_custommodule/observer::checkBackupLogFiles</model>
       </run>
     </check_arhieve_log_files>
  </jobs>
</crontab>

Leave a Reply

Your email address will not be published. Required fields are marked *