Welcome Guest, Not a member yet? Register   Sign In
Log maintenance library
#4

[eluser]Brian Nowell[/eluser]
I found that this lib didn't work when [log_path] config var was blank. So added some code from the system/libraries/log.php file to test for blank and default. Anyway, quick and dirty.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Log_maintenance {
/*
Updated by: Brian Nowell
Date:       08/07/2011

Description: Did not work when log_path config var was blank. Added code from System/Libraries/Log.php
to check for blank path and replace with default value. Library will keep current day plus number of [log_days_to_keep]
value.

Usage: Add this to Config.php

// Log_maintenance library: how many days to keep log files
$config['log_days_to_keep'] = 15;   // will keep 16 files

*/
    function Log_maintenance(){
        log_message('debug','Log_Maintenance : class loaded');
        $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');
        $dir = ($dir != '') ? $dir : BASEPATH.'logs/';

        log_message('debug','Log_Maintenance : log dir: '.$dir);

        if( ! is_dir($dir) OR ! is_really_writable($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;
    }
}


Messages In This Thread
Log maintenance library - by El Forum - 06-22-2009, 05:42 PM
Log maintenance library - by El Forum - 06-23-2009, 02:55 AM
Log maintenance library - by El Forum - 07-16-2009, 09:13 PM
Log maintenance library - by El Forum - 08-07-2011, 09:17 AM
Log maintenance library - by El Forum - 08-07-2011, 08:12 PM
Log maintenance library - by El Forum - 08-09-2011, 01:07 AM
Log maintenance library - by El Forum - 08-09-2011, 02:58 AM
Log maintenance library - by El Forum - 05-28-2013, 08:17 PM
Log maintenance library - by El Forum - 07-17-2013, 11:38 PM
Log maintenance library - by El Forum - 07-24-2013, 12:57 AM
Log maintenance library - by El Forum - 08-30-2013, 01:48 AM



Theme © iAndrew 2016 - Forum software by © MyBB