runRotation(); } function runRotation() { if ( !Configure::read('Log.rotate') ) { return; } $cache = Cache::getInstance(); $last_daily = $cache->read('Log.daily'); $last_weekly = $cache->read('Log.weekly'); $last_monthly = $cache->read('Log.monthly'); if ( $last_daily < strtotime('today') ) { $this->loadConfiguration(); $this->rotate($this->conf['daily']); $cache->write('Log.daily',time()); } if ( $last_weekly < strtotime('-1 monday') ) { $this->loadConfiguration(); $this->rotate($this->conf['weekly']); $cache->write('Log.weekly',time()); } if ( $last_monthly < mktime(0, 0, 0, date('m'), 1, date('Y')) ) { $this->loadConfiguration(); $this->rotate($this->conf['monthly']); $cache->write('Log.monthly',time()); } } function loadConfiguration() { if (empty($this->conf)) { $this->conf = Configure::read('Log.rotate'); $configured_files = array(); foreach ( $this->conf as $interval => $files ) { foreach ( $files as $type => $num_logs ) { if ( !is_numeric($num_logs) ) { $num_logs = 5; } if ( $type == '*' && !isset($default) ) { $default = array($interval, $num_logs); } else { $configured_files[] = basename($this->getFilename($type)); } } } if ( isset($default) ) { list($interval, $num_logs) = $default; unset($this->conf[$interval]['*']); if ($this->log_folder == null) { $this->log_folder = new Folder(LOGS); } $files = $this->log_folder->find('.*\.log',true); foreach ( $files as $filename ) { if ( !in_array($filename,$configured_files) ) { $this->conf[$interval][basename($filename,'.log')] = $num_logs; } } } } } function rotate($files) { foreach ( $files as $type => $num_logs) { $this->_rotate($type, $num_logs); } } function _rotate($type, $num_logs) { $filename = $this->getFilename($type); if ($this->log_folder == null) { $this->log_folder = new Folder(LOGS); } $files = $this->log_folder->find(basename($filename).'.*',true); $files = array_reverse($files); foreach ( $files as $file ) { $info = pathinfo(LOGS.$file); if ( is_numeric($info['extension']) ) { // this one of the numbered logfiles in rotation if ( ($num_logs > 0) && ($info['extension']+1) > $num_logs ) { unlink(LOGS.$file); continue; } $newfile = basename($file,$info['extension']) . ($info['extension']+1); $move = array('from' => LOGS.$file, 'to' => LOGS.$newfile); } else { // this is the active logfile $move = array('from' => LOGS.$file, 'to' => LOGS.$file.'.1'); } rename($move['from'],$move['to']); } } function getFilename($type) { /* pasted directly from CakeLog::write() */ if (!defined('LOG_ERROR')) { define('LOG_ERROR', 2); } if (!defined('LOG_ERR')) { define('LOG_ERR', LOG_ERROR); } $levels = array( LOG_WARNING => 'warning', LOG_NOTICE => 'notice', LOG_INFO => 'info', LOG_DEBUG => 'debug', LOG_ERR => 'error', LOG_ERROR => 'error' ); if (is_int($type) && isset($levels[$type])) { $type = $levels[$type]; } if ($type == 'error' || $type == 'warning') { $filename = LOGS . 'error.log'; } elseif (in_array($type, $levels)) { $filename = LOGS . 'debug.log'; } else { $filename = LOGS . $type . '.log'; } /* END pasted directly from CakeLog::write() */ return $filename; } } ?>