Development

Changeset 4886

You must first sign up to be able to contribute.

Changeset 4886

Show
Ignore:
Timestamp:
08/22/07 18:51:31 (1 year ago)
Author:
fabien
Message:

removed sfLogManager class and moved code to sfLogRotateTask

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/task/log/sfLogRotateTask.class.php

    r4841 r4886  
    1919class sfLogRotateTask extends sfBaseTask 
    2020{ 
     21  /** the default period to rotate logs in days */ 
     22  const DEF_PERIOD = 7; 
     23 
     24  /** the default number of log historys to store, one history is created for every period */ 
     25  const DEF_HISTORY = 10; 
     26 
    2127  /** 
    2228   * @see sfTask 
     
    5965    $env = $arguments['env']; 
    6066 
    61     sfLogManager::rotate($app, $env, $options['period'], $options['history'], true); 
     67    $this->rotate($app, $env, $options['period'], $options['history'], true); 
     68  } 
     69 
     70  /** 
     71   * Rotates log file. 
     72   * 
     73   * @param string Application name 
     74   * @param string Enviroment name 
     75   * @param string Period  
     76   * @param string History 
     77   * @param boolean Override 
     78   * 
     79   * @author Joe Simms 
     80   **/ 
     81  public function rotate($app, $env, $period = null, $history = null, $override = false) 
     82  { 
     83    $logfile = $app.'_'.$env; 
     84    $logdir = sfConfig::get('sf_log_dir'); 
     85 
     86    // set history and period values if not passed to default values 
     87    $period = isset($period) ? $period : self::DEF_PERIOD; 
     88    $history = isset($history) ? $history : self::DEF_HISTORY; 
     89 
     90    // get todays date 
     91    $today = date('Ymd'); 
     92 
     93    // check history folder exists 
     94    if (!is_dir($logdir.'/history')) 
     95    { 
     96      mkdir($logdir.'/history', 0777); 
     97    } 
     98 
     99    // determine date of last rotation 
     100    $logs = sfFinder::type('file')->ignore_version_control()->maxdepth(1)->name($logfile.'_*.log')->in($logdir.'/history/'); 
     101    $recentlog = is_array($logs) ? array_pop($logs) : null; 
     102 
     103    if ($recentlog) 
     104    { 
     105      // calculate date to rotate logs on 
     106      $lastRotatedOn = filemtime($recentlog); 
     107      $rotateOn = date('Ymd', strtotime('+ '.$period.' days', $lastRotatedOn)); 
     108    } 
     109    else 
     110    { 
     111      // no rotation has occured yet 
     112      $rotateOn = null; 
     113    } 
     114 
     115    $srcLog = $logdir.'/'.$logfile.'.log'; 
     116    $destLog = $logdir.'/history/'.$logfile.'_'.$today.'.log'; 
     117 
     118    // if rotate log on date doesn't exist, or that date is today, then rotate the log 
     119    if (!$rotateOn || ($rotateOn == $today) || $override) 
     120    { 
     121      // create a lock file 
     122      touch(sfConfig::get('sf_root_dir').'/'.$app.'_'.$env.'.lck'); 
     123 
     124      // if log file exists rotate it 
     125      if (file_exists($srcLog)) 
     126      { 
     127        // check if the log file has already been rotated today 
     128        if (file_exists($destLog)) 
     129        { 
     130          // append log to existing rotated log 
     131          $handle = fopen($destLog, 'a'); 
     132          $append = file_get_contents($srcLog); 
     133          fwrite($handle, $append); 
     134        } 
     135        else 
     136        { 
     137          // copy log 
     138          copy($srcLog, $destLog); 
     139        } 
     140 
     141        // remove the log file 
     142        unlink($srcLog); 
     143 
     144        // get all log history files for this application and environment 
     145        $newLogs = sfFinder::type('file')->ignore_version_control()->maxdepth(1)->name($logfile.'_*.log')->in($logdir.'/history/'); 
     146 
     147        // if the number of logs in history exceeds history then remove the oldest log 
     148        if (count($newLogs) > $history) 
     149        { 
     150          unlink($newLogs[0]); 
     151        } 
     152      } 
     153    } 
    62154  } 
    63155}