[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/log/ -> sfLogManager.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * 
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * Log manager
  13   *
  14   * @package    symfony
  15   * @subpackage log
  16   * @author     Joe Simms
  17   * @version    SVN: $Id: sfLogManager.class.php 3329 2007-01-23 08:29:34Z fabien $
  18   **/
  19  class sfLogManager
  20  {
  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  
  27    /**
  28     * Rotates log file.
  29     *
  30     * @param string Application name
  31     * @param string Enviroment name
  32     * @param string Period 
  33     * @param string History
  34     * @param boolean Override
  35     *
  36     * @author Joe Simms
  37     **/
  38    public static function rotate($app, $env, $period = null, $history = null, $override = false)
  39    {
  40      $logfile = $app.'_'.$env;
  41      $logdir = sfConfig::get('sf_log_dir');
  42  
  43      // set history and period values if not passed to default values
  44      $period = isset($period) ? $period : self::DEF_PERIOD;
  45      $history = isset($history) ? $history : self::DEF_HISTORY;
  46  
  47      // get todays date
  48      $today = date('Ymd');
  49  
  50      // check history folder exists
  51      if (!is_dir($logdir.'/history'))
  52      {
  53        mkdir($logdir.'/history', 0777);
  54      }
  55  
  56      // determine date of last rotation
  57      $logs = sfFinder::type('file')->ignore_version_control()->maxdepth(1)->name($logfile.'_*.log')->in($logdir.'/history/');
  58      $recentlog = is_array($logs) ? array_pop($logs) : null;
  59  
  60      if ($recentlog)
  61      {
  62        // calculate date to rotate logs on
  63        $last_rotated_on = filemtime($recentlog);
  64        $rotate_on = date('Ymd', strtotime('+ '.$period.' days', $last_rotated_on));
  65      }
  66      else
  67      {
  68        // no rotation has occured yet
  69        $rotate_on = null;
  70      }
  71  
  72      $src_log = $logdir.'/'.$logfile.'.log';
  73      $dest_log = $logdir.'/history/'.$logfile.'_'.$today.'.log';
  74  
  75      // if rotate log on date doesn't exist, or that date is today, then rotate the log
  76      if (!$rotate_on || ($rotate_on == $today) || $override)
  77      {
  78        // create a lock file
  79        $lock_name = $app.'_'.$env.'.lck';
  80        touch(sfConfig::get('sf_root_dir').'/'.$lock_name);
  81  
  82        // if log file exists rotate it
  83        if (file_exists($src_log))
  84        {
  85          // check if the log file has already been rotated today
  86          if (file_exists($dest_log))
  87          {
  88            // append log to existing rotated log
  89            $handle = fopen($dest_log, 'a');
  90            $append = file_get_contents($src_log);
  91            fwrite($handle, $append);
  92          }
  93          else
  94          {
  95            // copy log
  96            copy($src_log, $dest_log);
  97          }
  98  
  99          // remove the log file
 100          unlink($src_log);
 101  
 102          // get all log history files for this application and environment
 103          $new_logs = sfFinder::type('file')->ignore_version_control()->maxdepth(1)->name($logfile.'_*.log')->in($logdir.'/history/');
 104  
 105          // if the number of logs in history exceeds history then remove the oldest log
 106          if (count($new_logs) > $history)
 107          {
 108            unlink($new_logs[0]);
 109          }
 110        }
 111      }
 112    }
 113  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7