Development

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

You must first sign up to be able to contribute.

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

Revision 24331, 4.8 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3, 1.4] updated log:rotate to explicitly sort files by name and use filesystem methods when possible (closes #7683)

  • 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', 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     // set history and period values if not passed to default values
84     $period = isset($period) ? $period : self::DEF_PERIOD;
85     $history = isset($history) ? $history : self::DEF_HISTORY;
86
87     // get todays date
88     $today = date('Ymd');
89
90     // check history folder exists
91     if (!is_dir($logdir.'/history'))
92     {
93       $this->getFilesystem()->mkdirs($logdir.'/history');
94     }
95
96     // determine date of last rotation
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       // calculate date to rotate logs on
103       $lastRotatedOn = filemtime($recentlog);
104       $rotateOn = date('Ymd', strtotime('+ '.$period.' days', $lastRotatedOn));
105     }
106     else
107     {
108       // no rotation has occured yet
109       $rotateOn = null;
110     }
111
112     $srcLog = $logdir.'/'.$logfile.'.log';
113     $destLog = $logdir.'/history/'.$logfile.'_'.$today.'.log';
114
115     // if rotate log on date doesn't exist, or that date is today, then rotate the log
116     if (!$rotateOn || ($rotateOn == $today) || $override)
117     {
118       // create a lock file
119       $lockFile = sfConfig::get('sf_data_dir').'/'.$app.'_'.$env.'-cli.lck';
120       $this->getFilesystem()->touch($lockFile);
121
122       // change mode so the web user can remove it if we die
123       $this->getFilesystem()->chmod($lockFile, 0777);
124
125       // if log file exists rotate it
126       if (file_exists($srcLog))
127       {
128         // check if the log file has already been rotated today
129         if (file_exists($destLog))
130         {
131           // append log to existing rotated log
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           // copy log
141           $this->getFilesystem()->copy($srcLog, $destLog);
142         }
143
144         // remove the log file
145         $this->getFilesystem()->remove($srcLog);
146
147         // get all log history files for this application and environment
148         $newLogs = sfFinder::type('file')->maxdepth(1)->name($logfile.'_*.log')->sort_by_name()->in($logdir.'/history');
149
150         // if the number of logs in history exceeds history then remove the oldest log
151         if (count($newLogs) > $history)
152         {
153           $this->getFilesystem()->remove($newLogs[0]);
154         }
155       }
156
157       // release lock
158       $this->getFilesystem()->remove($lockFile);
159     }
160   }
161 }
162
Note: See TracBrowser for help on using the browser.