Development

/branches/1.0/lib/log/sfLogManager.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/log/sfLogManager.class.php

Revision 9893, 3.2 kB (checked in by fabien, 6 years ago)

fixed locking issues with the clear-cache, log-rotate, enable, and disable tasks (closes #3827)

  • 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  * Log manager
13  *
14  * @package    symfony
15  * @subpackage log
16  * @author     Joe Simms
17  * @version    SVN: $Id$
18  **/
19 class sfLogManager
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    * Rotates log file.
29    *
30    * @param string Application name
31    * @param string Enviroment name
32    * @param string Period
33    * @param string History
34    * @param boolean Override
35    *
36    * @author Joe Simms
37    **/
38   public static function rotate($app, $env, $period = null, $history = null, $override = false)
39   {
40     $logfile = $app.'_'.$env;
41     $logdir = sfConfig::get('sf_log_dir');
42
43     // set history and period values if not passed to default values
44     $period = isset($period) ? $period : self::DEF_PERIOD;
45     $history = isset($history) ? $history : self::DEF_HISTORY;
46
47     // get todays date
48     $today = date('Ymd');
49
50     // check history folder exists
51     if (!is_dir($logdir.'/history'))
52     {
53       mkdir($logdir.'/history', 0777);
54     }
55
56     // determine date of last rotation
57     $logs = sfFinder::type('file')->ignore_version_control()->maxdepth(1)->name($logfile.'_*.log')->in($logdir.'/history/');
58     $recentlog = is_array($logs) ? array_pop($logs) : null;
59
60     if ($recentlog)
61     {
62       // calculate date to rotate logs on
63       $last_rotated_on = filemtime($recentlog);
64       $rotate_on = date('Ymd', strtotime('+ '.$period.' days', $last_rotated_on));
65     }
66     else
67     {
68       // no rotation has occured yet
69       $rotate_on = null;
70     }
71
72     $src_log = $logdir.'/'.$logfile.'.log';
73     $dest_log = $logdir.'/history/'.$logfile.'_'.$today.'.log';
74
75     // if rotate log on date doesn't exist, or that date is today, then rotate the log
76     if (!$rotate_on || ($rotate_on == $today) || $override)
77     {
78       // create a lock file
79       $lockFile = sfConfig::get('sf_root_dir').'/'.$app.'_'.$env.'-cli.lck';
80       touch($lockFile);
81       chmod($lockFile, 0777);
82
83       // if log file exists rotate it
84       if (file_exists($src_log))
85       {
86         // check if the log file has already been rotated today
87         if (file_exists($dest_log))
88         {
89           // append log to existing rotated log
90           $handle = fopen($dest_log, 'a');
91           $append = file_get_contents($src_log);
92           fwrite($handle, $append);
93         }
94         else
95         {
96           // copy log
97           copy($src_log, $dest_log);
98         }
99
100         // remove the log file
101         unlink($src_log);
102
103         // get all log history files for this application and environment
104         $new_logs = sfFinder::type('file')->ignore_version_control()->maxdepth(1)->name($logfile.'_*.log')->in($logdir.'/history/');
105
106         // if the number of logs in history exceeds history then remove the oldest log
107         if (count($new_logs) > $history)
108         {
109           unlink($new_logs[0]);
110         }
111       }
112
113       unlink($lockFile);
114     }
115   }
116 }
117
Note: See TracBrowser for help on using the browser.