Development

/branches/1.2/lib/task/log/sfLogRotateTask.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/task/log/sfLogRotateTask.class.php

Revision 9890, 4.7 kB (checked in by fabien, 6 years ago)

fxed locking issues with the cache:clear, project:disable, project:enable, and log:rotate tasks (closes #3598)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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  * Rotates an application log files.
13  *
14  * @package    symfony
15  * @subpackage task
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfLogRotateTask extends sfBaseTask
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    * @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', 10),
39       new sfCommandOption('period', null, sfCommandOption::PARAMETER_REQUIRED, 'The period in days', 7),
40     ));
41
42     $this->aliases = array('log-rotate');
43     $this->namespace = 'log';
44     $this->name = 'rotate';
45     $this->briefDescription = 'Rotates an application 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 --history=10 --period=7 log:rotate frontend dev|INFO]
56 EOF;
57   }
58
59   /**
60    * @see sfTask
61    */
62   protected function execute($arguments = array(), $options = array())
63   {
64     $app = $arguments['application'];
65     $env = $arguments['env'];
66
67     $this->rotate($app, $env, $options['period'], $options['history'], true);
68   }
69
70   /**
71    * Rotates log file.
72    *
73    * @param  string $app       Application name
74    * @param  string $env       Enviroment name
75    * @param  string $period    Period
76    * @param  string $history   History
77    * @param  bool   $override  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')->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       $lockFile = sfConfig::get('sf_data_dir').'/'.$app.'_'.$env.'-cli.lck';
123       $this->getFilesystem()->touch($lockFile);
124
125       // change mode so the web user can remove it if we die
126       $this->getFilesystem()->chmod($lockFile, 0777);
127
128       // if log file exists rotate it
129       if (file_exists($srcLog))
130       {
131         // check if the log file has already been rotated today
132         if (file_exists($destLog))
133         {
134           // append log to existing rotated log
135           $handle = fopen($destLog, 'a');
136           $append = file_get_contents($srcLog);
137           fwrite($handle, $append);
138         }
139         else
140         {
141           // copy log
142           copy($srcLog, $destLog);
143         }
144
145         // remove the log file
146         unlink($srcLog);
147
148         // get all log history files for this application and environment
149         $newLogs = sfFinder::type('file')->maxdepth(1)->name($logfile.'_*.log')->in($logdir.'/history/');
150
151         // if the number of logs in history exceeds history then remove the oldest log
152         if (count($newLogs) > $history)
153         {
154           unlink($newLogs[0]);
155         }
156       }
157
158       // release lock
159       $this->getFilesystem()->remove($lockFile);
160     }
161   }
162 }
163
Note: See TracBrowser for help on using the browser.