[eluser]deadelvis[/eluser]
... or actually just a quick way to get rid of the growing number of log files (my website constantly logs info messages for performance monitoring), by just deleting the old ones.
Add this to your config:
Code:
// Log_maintenance library: how many days to keep log files
$config['log_days_to_keep'] = 15;
Save this as Log_maintenance.php in your libraries folder:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Log_maintenance {
function Log_maintenance(){
$this->CI =& get_instance();
$this->log_days_to_keep = $this->CI->config->item('log_days_to_keep');
$this->delete_old_logs(); // delete the old log files
}
function delete_old_logs(){
$dir = $this->CI->config->item('log_path');
if( !is_dir($dir) ){ return false; }
$dh = opendir($dir);
$deleted = 0;
$kept = 0;
while ( ($file = readdir($dh)) !== false){
// check log filename: log*.php
if ( substr( strtolower($file), -4, 4 )=='.php' && substr( strtolower($file), 0, 3 )=='log'){
// check how old they are
if( filemtime($dir.$file) < strtotime('-'.$this->log_days_to_keep.' days') ) {
unlink($dir.$file);
$deleted++;
}else{
$kept++;
}
}
}
closedir($dh);
$total = $deleted+$kept;
if( $deleted>0 ){
log_message('info', $total.' log files: '.$deleted.' deleted, '.$kept.' kept.');
}
$a = array('total'=>$total, 'deleted'=>$deleted, 'kept'=>$kept);
return $a;
}
}
?>
Just load the library somewhere in your code to have the old log files deleted.
It's my first library... so do review the code if you plan on using. Any comments/improvements/suggestions are very much welcome.