Welcome Guest, Not a member yet? Register   Sign In
logrotate for log files
#2

(This post was last modified: 06-29-2019, 10:52 AM by gmgj.)

PHP Code:
// 1- get a list of file in the CI default CI log file

$LogPathCI '/var/www/html/Zapplication/logs';

$f1 = new FilesystemIterator($LogPathCI FilesystemIterator::SKIP_DOTS);

$gjcount=0// start at
$gjstart=10// total number to keep

if(iterator_count($f1) < $gjstart){
    
gjLog('Number of Files less than number to keep - Nothing to unlink' .PHP_EOL,'test.log');
}

// 2- create an array of files to sort by Modified Time in step 3

$thefiles = array();
foreach(
$f1 as $fileinfo) {
    
$akey $f1->getFileName();
    if((
strtolower($akey)) != 'index.html') {
        
$thefiles[$akey] = $f1->getMTime();
    }
}

// 3- sort the files by date - descending ┬ánewest first

arsort($thefiles);

// 4- start at 0, after 10 files delete the rest


foreach ($thefiles as $key => $value) {

    if(
$gjstart $gjcount){
        
gjLog('unlink these key: ' $key .' value: '$value .PHP_EOL,'test.log');
        
$killme $LogPathCI DIRECTORY_SEPARATOR $key;
        if(!
unlink($killme)) {
            
gjLog('unable to unlink '$killme .PHP_EOL,'test.log');
        }
    } else {
        
gjLog('keep these key: ' $key .' value: '$value .PHP_EOL,'test.log');
    }
    
$gjcount++;

I wrote my first program in 15 minutes. It took me 3 hours to keypunch it.

I wrote my first BASH script in 5 minutes. It took me a day to find out I had to put a . (period) in front of it to get it to execute.
Reply


Messages In This Thread
logrotate for log files - by gmgj - 06-29-2019, 07:18 AM
RE: logrotate for log files - by gmgj - 06-29-2019, 10:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB