| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfLogRotateTask extends sfBaseTask |
|---|
| 20 |
{ |
|---|
| 21 |
|
|---|
| 22 |
const DEF_PERIOD = 7; |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
const DEF_HISTORY = 10; |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
* @see sfTask |
|---|
| 29 |
*/ |
|---|
| 30 |
protected function configure() |
|---|
| 31 |
{ |
|---|
| 32 |
$this->addArguments(array( |
|---|
| 33 |
new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'), |
|---|
| 34 |
new sfCommandArgument('env', sfCommandArgument::REQUIRED, 'The environment name'), |
|---|
| 35 |
)); |
|---|
| 36 |
|
|---|
| 37 |
$this->addOptions(array( |
|---|
| 38 |
new sfCommandOption('history', null, sfCommandOption::PARAMETER_REQUIRED, 'The maximum number of old log files to keep', self::DEF_HISTORY), |
|---|
| 39 |
new sfCommandOption('period', null, sfCommandOption::PARAMETER_REQUIRED, 'The period in days', self::DEF_PERIOD), |
|---|
| 40 |
)); |
|---|
| 41 |
|
|---|
| 42 |
$this->aliases = array('log-rotate'); |
|---|
| 43 |
$this->namespace = 'log'; |
|---|
| 44 |
$this->name = 'rotate'; |
|---|
| 45 |
$this->briefDescription = 'Rotates an application\'s log files'; |
|---|
| 46 |
|
|---|
| 47 |
$this->detailedDescription = <<<EOF |
|---|
| 48 |
The [log:rotate|INFO] task rotates application log files for a given |
|---|
| 49 |
environment: |
|---|
| 50 |
|
|---|
| 51 |
[./symfony log:rotate frontend dev|INFO] |
|---|
| 52 |
|
|---|
| 53 |
You can specify a [period|COMMENT] or a [history|COMMENT] option: |
|---|
| 54 |
|
|---|
| 55 |
[./symfony log:rotate frontend dev --history=10 --period=7|INFO] |
|---|
| 56 |
EOF; |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
* @see sfTask |
|---|
| 61 |
*/ |
|---|
| 62 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 63 |
{ |
|---|
| 64 |
$this->rotate($arguments['application'], $arguments['env'], $options['period'], $options['history'], true); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
* Rotates log file. |
|---|
| 69 |
* |
|---|
| 70 |
* @param string $app Application name |
|---|
| 71 |
* @param string $env Enviroment name |
|---|
| 72 |
* @param string $period Period |
|---|
| 73 |
* @param string $history History |
|---|
| 74 |
* @param bool $override Override |
|---|
| 75 |
* |
|---|
| 76 |
* @author Joe Simms |
|---|
| 77 |
**/ |
|---|
| 78 |
public function rotate($app, $env, $period = null, $history = null, $override = false) |
|---|
| 79 |
{ |
|---|
| 80 |
$logfile = $app.'_'.$env; |
|---|
| 81 |
$logdir = sfConfig::get('sf_log_dir'); |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
$period = isset($period) ? $period : self::DEF_PERIOD; |
|---|
| 85 |
$history = isset($history) ? $history : self::DEF_HISTORY; |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
$today = date('Ymd'); |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
if (!is_dir($logdir.'/history')) |
|---|
| 92 |
{ |
|---|
| 93 |
$this->getFilesystem()->mkdirs($logdir.'/history'); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
$logs = sfFinder::type('file')->maxdepth(1)->name($logfile.'_*.log')->sort_by_name()->in($logdir.'/history'); |
|---|
| 98 |
$recentlog = is_array($logs) ? array_pop($logs) : null; |
|---|
| 99 |
|
|---|
| 100 |
if ($recentlog) |
|---|
| 101 |
{ |
|---|
| 102 |
|
|---|
| 103 |
$lastRotatedOn = filemtime($recentlog); |
|---|
| 104 |
$rotateOn = date('Ymd', strtotime('+ '.$period.' days', $lastRotatedOn)); |
|---|
| 105 |
} |
|---|
| 106 |
else |
|---|
| 107 |
{ |
|---|
| 108 |
|
|---|
| 109 |
$rotateOn = null; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
$srcLog = $logdir.'/'.$logfile.'.log'; |
|---|
| 113 |
$destLog = $logdir.'/history/'.$logfile.'_'.$today.'.log'; |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
if (!$rotateOn || ($rotateOn == $today) || $override) |
|---|
| 117 |
{ |
|---|
| 118 |
|
|---|
| 119 |
$lockFile = sfConfig::get('sf_data_dir').'/'.$app.'_'.$env.'-cli.lck'; |
|---|
| 120 |
$this->getFilesystem()->touch($lockFile); |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
$this->getFilesystem()->chmod($lockFile, 0777); |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
if (file_exists($srcLog)) |
|---|
| 127 |
{ |
|---|
| 128 |
|
|---|
| 129 |
if (file_exists($destLog)) |
|---|
| 130 |
{ |
|---|
| 131 |
|
|---|
| 132 |
$handle = fopen($destLog, 'a'); |
|---|
| 133 |
$append = file_get_contents($srcLog); |
|---|
| 134 |
|
|---|
| 135 |
$this->logSection('file+', $destLog); |
|---|
| 136 |
fwrite($handle, $append); |
|---|
| 137 |
} |
|---|
| 138 |
else |
|---|
| 139 |
{ |
|---|
| 140 |
|
|---|
| 141 |
$this->getFilesystem()->copy($srcLog, $destLog); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
$this->getFilesystem()->remove($srcLog); |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
$newLogs = sfFinder::type('file')->maxdepth(1)->name($logfile.'_*.log')->sort_by_name()->in($logdir.'/history'); |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
if (count($newLogs) > $history) |
|---|
| 152 |
{ |
|---|
| 153 |
$this->getFilesystem()->remove($newLogs[0]); |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
$this->getFilesystem()->remove($lockFile); |
|---|
| 159 |
} |
|---|
| 160 |
} |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|