123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- class sfLogRotateTask extends sfBaseTask
- {
-
- const DEF_PERIOD = 7;
-
- const DEF_HISTORY = 10;
-
- protected function configure()
- {
- $this->addArguments(array(
- new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
- new sfCommandArgument('env', sfCommandArgument::REQUIRED, 'The environment name'),
- ));
- $this->addOptions(array(
- new sfCommandOption('history', null, sfCommandOption::PARAMETER_REQUIRED, 'The maximum number of old log files to keep', 10),
- new sfCommandOption('period', null, sfCommandOption::PARAMETER_REQUIRED, 'The period in days', 7),
- ));
- $this->aliases = array('log-rotate');
- $this->namespace = 'log';
- $this->name = 'rotate';
- $this->briefDescription = 'Rotates an application log files';
- $this->detailedDescription = <<<EOF
- The [log:rotate|INFO] task rotates application log files for a given
- environment:
- [./symfony log:rotate frontend dev|INFO]
- You can specify a [period|COMMENT] or a [history|COMMENT] option:
- [./symfony --history=10 --period=7 log:rotate frontend dev|INFO]
- EOF;
- }
-
- protected function execute($arguments = array(), $options = array())
- {
- $app = $arguments['application'];
- $env = $arguments['env'];
- $this->rotate($app, $env, $options['period'], $options['history'], true);
- }
-
- public function rotate($app, $env, $period = null, $history = null, $override = false)
- {
- $logfile = $app.'_'.$env;
- $logdir = sfConfig::get('sf_log_dir');
-
- $period = isset($period) ? $period : self::DEF_PERIOD;
- $history = isset($history) ? $history : self::DEF_HISTORY;
-
- $today = date('Ymd');
-
- if (!is_dir($logdir.'/history'))
- {
- mkdir($logdir.'/history', 0777);
- }
-
- $logs = sfFinder::type('file')->maxdepth(1)->name($logfile.'_*.log')->in($logdir.'/history/');
- $recentlog = is_array($logs) ? array_pop($logs) : null;
- if ($recentlog)
- {
-
- $lastRotatedOn = filemtime($recentlog);
- $rotateOn = date('Ymd', strtotime('+ '.$period.' days', $lastRotatedOn));
- }
- else
- {
-
- $rotateOn = null;
- }
- $srcLog = $logdir.'/'.$logfile.'.log';
- $destLog = $logdir.'/history/'.$logfile.'_'.$today.'.log';
-
- if (!$rotateOn || ($rotateOn == $today) || $override)
- {
-
- $lockFile = sfConfig::get('sf_data_dir').'/'.$app.'_'.$env.'-cli.lck';
- $this->getFilesystem()->touch($lockFile);
-
- $this->getFilesystem()->chmod($lockFile, 0777);
-
- if (file_exists($srcLog))
- {
-
- if (file_exists($destLog))
- {
-
- $handle = fopen($destLog, 'a');
- $append = file_get_contents($srcLog);
- fwrite($handle, $append);
- }
- else
- {
-
- copy($srcLog, $destLog);
- }
-
- unlink($srcLog);
-
- $newLogs = sfFinder::type('file')->maxdepth(1)->name($logfile.'_*.log')->in($logdir.'/history/');
-
- if (count($newLogs) > $history)
- {
- unlink($newLogs[0]);
- }
- }
-
- $this->getFilesystem()->remove($lockFile);
- }
- }
- }
|