CodeIgniter Forums
logrotate for log files - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: logrotate for log files (/showthread.php?tid=73960)



logrotate for log files - gmgj - 06-29-2019

I would like to logrotate my CI log files and keep 3 days worth of logs (3 as an example to keep the examples small) 

my  /etc/logrotate.d/gjtst    logrotate conf file
 
 /var/www/html/application/logs/*.php {
su www-data www-data
    rotate 3
    daily
    missingok
}

Before folder entries
log-2019-06-29.php
log-2019-06-28.php
log-2019-06-27.php
log-2019-06-26.php
...

After this run  logrotate -f -v /etc/logrotate.d/gjtst

After folder entries
log-2019-06-29.php.1
log-2019-06-28.php.1
log-2019-06-27.php.1
log-2019-06-26.php.1
...


Desired folder entries  I can live with changing the file name, I just want 3 versions

log-2019-06-29.php
log-2019-06-28.php
log-2019-06-27.php

What do I change to get the desired effect?

Or is there a simpler procedure for a lamp stack based system
a bash or php script I could put in a cron job?


RE: logrotate for log files - gmgj - 06-29-2019

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




RE: logrotate for log files - sushiguru - 11-02-2022

Late to the party with this one, but this is what I use in cron to keep CI logs under control:

Code:
3 23 * * * find "/var/www/html" -name "log*.php" -mtime +14 -exec rm {} \;

For me, run this at 11.03pm every day, find all CI logs in all apps on my server which are older than 14 days old, and unceremoniously delete them.


RE: logrotate for log files - akbarkhanhrp - 11-10-2022

(06-29-2019, 07:18 AM)gmgj Wrote: I would like to logrotate my CI log files and keep 3 days worth of logs (3 as an example to keep the examples small) 

my  /etc/logrotate.d/gjtst    logrotate conf file
 
 /var/www/html/application/logs/*.php {
su www-data www-data
    rotate 3
    daily
    missingok
}

Before folder entries
log-2019-06-29.php
log-2019-06-28.php
log-2019-06-27.php
log-2019-06-26.php
...

After this run  logrotate -f -v /etc/logrotate.d/gjtst

After folder entries
log-2019-06-29.php.1
log-2019-06-28.php.1
log-2019-06-27.php.1
log-2019-06-26.php.1
...


Desired folder entries  I can live with changing the file name, I just want 3 versions

log-2019-06-29.php
log-2019-06-28.php
log-2019-06-27.php

What do I change to get the desired effect?

Or is there a simpler procedure for a lamp stack based system
a bash or php script I could put in a cron job?

What codes you have used in these.