sfLogRotateTask.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Rotates an application log files.
  11. *
  12. * @package symfony
  13. * @subpackage task
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfLogRotateTask.class.php 9890 2008-06-26 11:35:01Z fabien $
  16. */
  17. class sfLogRotateTask extends sfBaseTask
  18. {
  19. /** the default period to rotate logs in days */
  20. const DEF_PERIOD = 7;
  21. /** the default number of log historys to store, one history is created for every period */
  22. const DEF_HISTORY = 10;
  23. /**
  24. * @see sfTask
  25. */
  26. protected function configure()
  27. {
  28. $this->addArguments(array(
  29. new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
  30. new sfCommandArgument('env', sfCommandArgument::REQUIRED, 'The environment name'),
  31. ));
  32. $this->addOptions(array(
  33. new sfCommandOption('history', null, sfCommandOption::PARAMETER_REQUIRED, 'The maximum number of old log files to keep', 10),
  34. new sfCommandOption('period', null, sfCommandOption::PARAMETER_REQUIRED, 'The period in days', 7),
  35. ));
  36. $this->aliases = array('log-rotate');
  37. $this->namespace = 'log';
  38. $this->name = 'rotate';
  39. $this->briefDescription = 'Rotates an application log files';
  40. $this->detailedDescription = <<<EOF
  41. The [log:rotate|INFO] task rotates application log files for a given
  42. environment:
  43. [./symfony log:rotate frontend dev|INFO]
  44. You can specify a [period|COMMENT] or a [history|COMMENT] option:
  45. [./symfony --history=10 --period=7 log:rotate frontend dev|INFO]
  46. EOF;
  47. }
  48. /**
  49. * @see sfTask
  50. */
  51. protected function execute($arguments = array(), $options = array())
  52. {
  53. $app = $arguments['application'];
  54. $env = $arguments['env'];
  55. $this->rotate($app, $env, $options['period'], $options['history'], true);
  56. }
  57. /**
  58. * Rotates log file.
  59. *
  60. * @param string $app Application name
  61. * @param string $env Enviroment name
  62. * @param string $period Period
  63. * @param string $history History
  64. * @param bool $override Override
  65. *
  66. * @author Joe Simms
  67. **/
  68. public function rotate($app, $env, $period = null, $history = null, $override = false)
  69. {
  70. $logfile = $app.'_'.$env;
  71. $logdir = sfConfig::get('sf_log_dir');
  72. // set history and period values if not passed to default values
  73. $period = isset($period) ? $period : self::DEF_PERIOD;
  74. $history = isset($history) ? $history : self::DEF_HISTORY;
  75. // get todays date
  76. $today = date('Ymd');
  77. // check history folder exists
  78. if (!is_dir($logdir.'/history'))
  79. {
  80. mkdir($logdir.'/history', 0777);
  81. }
  82. // determine date of last rotation
  83. $logs = sfFinder::type('file')->maxdepth(1)->name($logfile.'_*.log')->in($logdir.'/history/');
  84. $recentlog = is_array($logs) ? array_pop($logs) : null;
  85. if ($recentlog)
  86. {
  87. // calculate date to rotate logs on
  88. $lastRotatedOn = filemtime($recentlog);
  89. $rotateOn = date('Ymd', strtotime('+ '.$period.' days', $lastRotatedOn));
  90. }
  91. else
  92. {
  93. // no rotation has occured yet
  94. $rotateOn = null;
  95. }
  96. $srcLog = $logdir.'/'.$logfile.'.log';
  97. $destLog = $logdir.'/history/'.$logfile.'_'.$today.'.log';
  98. // if rotate log on date doesn't exist, or that date is today, then rotate the log
  99. if (!$rotateOn || ($rotateOn == $today) || $override)
  100. {
  101. // create a lock file
  102. $lockFile = sfConfig::get('sf_data_dir').'/'.$app.'_'.$env.'-cli.lck';
  103. $this->getFilesystem()->touch($lockFile);
  104. // change mode so the web user can remove it if we die
  105. $this->getFilesystem()->chmod($lockFile, 0777);
  106. // if log file exists rotate it
  107. if (file_exists($srcLog))
  108. {
  109. // check if the log file has already been rotated today
  110. if (file_exists($destLog))
  111. {
  112. // append log to existing rotated log
  113. $handle = fopen($destLog, 'a');
  114. $append = file_get_contents($srcLog);
  115. fwrite($handle, $append);
  116. }
  117. else
  118. {
  119. // copy log
  120. copy($srcLog, $destLog);
  121. }
  122. // remove the log file
  123. unlink($srcLog);
  124. // get all log history files for this application and environment
  125. $newLogs = sfFinder::type('file')->maxdepth(1)->name($logfile.'_*.log')->in($logdir.'/history/');
  126. // if the number of logs in history exceeds history then remove the oldest log
  127. if (count($newLogs) > $history)
  128. {
  129. unlink($newLogs[0]);
  130. }
  131. }
  132. // release lock
  133. $this->getFilesystem()->remove($lockFile);
  134. }
  135. }
  136. }