Wednesday, December 31, 2014

Magento - archiving log files

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 complete file every time.
Of course, we can archive this files manualy, but, why should we do that if we can create yet another Magento CRON task that will do it for us :-P

Following script will do:
  • Check if defined files exist in log folder
  • Check each file size
  • If file size is bigger than 100Mb, then it wil be archived
  • Rename file to _filenname to avoid duplicate processing
  • Compress file in tgz
  • Delete original file
Let's add new method inside 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 (!@file_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.
    
        
            
                
                    0 1 * * *
                
                
                    my_custommodule/observer::checkBackupLogFiles
                
            
        
    


 Cheers :-)

,

0 comments:

Post a Comment