source: branches/comu-ver2/data/module/log4php/php4/log4php/appenders/LoggerAppenderRollingFile.php @ 18220

Revision 18220, 7.9 KB checked in by yokkuns, 15 years ago (diff)

#149 ロガークラス作成

Line 
1<?php
2/**
3 * log4php is a PHP port of the log4j java logging package.
4 *
5 * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
6 * <p>Design, strategies and part of the methods documentation are developed by log4j team
7 * (Ceki Gülcü as log4j project founder and
8 * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
9 *
10 * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
11 * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
12 *
13 * <p>This software is published under the terms of the LGPL License
14 * a copy of which has been included with this distribution in the LICENSE file.</p>
15 *
16 * @package log4php
17 * @subpackage appenders
18 */
19
20/**
21 * @ignore
22 */
23if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
24 
25require_once(LOG4PHP_DIR . '/appenders/LoggerAppenderFile.php');
26
27/**
28 * LoggerAppenderRollingFile extends LoggerAppenderFile to backup the log files
29 * when they reach a certain size.
30 *
31 * <p>Parameters are {@link $maxFileSize}, {@link $maxBackupIndex}.</p>
32 *
33 * <p>Contributors: Sergio Strampelli.</p>
34 *
35 * @author VxR <vxr@vxr.it>
36 * @version $Revision: 1.14 $
37 * @package log4php
38 * @subpackage appenders
39 */
40class LoggerAppenderRollingFile extends LoggerAppenderFile {
41
42    /**
43     * Set the maximum size that the output file is allowed to reach
44     * before being rolled over to backup files.
45     *
46     * <p>In configuration files, the <var>MaxFileSize</var> option takes a
47     * long integer in the range 0 - 2^63. You can specify the value
48     * with the suffixes "KB", "MB" or "GB" so that the integer is
49     * interpreted being expressed respectively in kilobytes, megabytes
50     * or gigabytes. For example, the value "10KB" will be interpreted
51     * as 10240.</p>
52     * <p>The default maximum file size is 10MB.</p>
53     *
54     * <p>Note that MaxFileSize cannot exceed <b>2 GB</b>.</p>
55     *
56     * @var integer
57     */
58    var $maxFileSize = 10485760;
59   
60    /**
61     * Set the maximum number of backup files to keep around.
62     *
63     * <p>The <var>MaxBackupIndex</var> option determines how many backup
64     * files are kept before the oldest is erased. This option takes
65     * a positive integer value. If set to zero, then there will be no
66     * backup files and the log file will be truncated when it reaches
67     * MaxFileSize.</p>
68     * <p>There is one backup file by default.</p>
69     *
70     * @var integer
71     */
72    var $maxBackupIndex  = 1;
73   
74    /**
75     * @var string the filename expanded
76     * @access private
77     */
78    var $expandedFileName = null;
79
80    /**
81     * Constructor.
82     *
83     * @param string $name appender name
84     */
85    function LoggerAppenderRollingFile($name)
86    {
87        $this->LoggerAppenderFile($name);
88    }
89   
90    /**
91     * Returns the value of the MaxBackupIndex option.
92     * @return integer
93     */
94    function getExpandedFileName() {
95        return $this->expandedFileName;
96    }
97
98    /**
99     * Returns the value of the MaxBackupIndex option.
100     * @return integer
101     */
102    function getMaxBackupIndex() {
103        return $this->maxBackupIndex;
104    }
105
106    /**
107     * Get the maximum size that the output file is allowed to reach
108     * before being rolled over to backup files.
109     * @return integer
110     */
111    function getMaximumFileSize() {
112        return $this->maxFileSize;
113    }
114
115    /**
116     * Implements the usual roll over behaviour.
117     *
118     * <p>If MaxBackupIndex is positive, then files File.1, ..., File.MaxBackupIndex -1 are renamed to File.2, ..., File.MaxBackupIndex.
119     * Moreover, File is renamed File.1 and closed. A new File is created to receive further log output.
120     *
121     * <p>If MaxBackupIndex is equal to zero, then the File is truncated with no backup files created.
122     */
123    function rollOver()
124    {
125        // If maxBackups <= 0, then there is no file renaming to be done.
126        if($this->maxBackupIndex > 0) {
127            $fileName = $this->getExpandedFileName();
128            // Delete the oldest file, to keep Windows happy.
129            $file = $fileName . '.' . $this->maxBackupIndex;
130            if (is_writable($file))
131                unlink($file);
132            // Map {(maxBackupIndex - 1), ..., 2, 1} to {maxBackupIndex, ..., 3, 2}
133            for ($i = $this->maxBackupIndex - 1; $i >= 1; $i--) {
134                $file = $fileName . "." . $i;
135                if (is_readable($file)) {
136                    $target = $fileName . '.' . ($i + 1);
137                    rename($file, $target);
138                }
139            }
140   
141            // Rename fileName to fileName.1
142            $target = $fileName . ".1";
143   
144            $this->closeFile(); // keep windows happy.
145   
146            $file = $fileName;
147            rename($file, $target);
148        }
149       
150        $this->setFile($fileName, false);
151        unset($this->fp);
152        $this->activateOptions();
153    }
154   
155    function setFileName($fileName)
156    {
157        $this->fileName = $fileName;
158        $this->expandedFileName = realpath($fileName);
159        LoggerLog::debug("LoggerAppenderRollingFile::setFileName():filename=[{$fileName}]:expandedFileName=[{$this->expandedFileName}]"); 
160    }
161
162
163    /**
164     * Set the maximum number of backup files to keep around.
165     *
166     * <p>The <b>MaxBackupIndex</b> option determines how many backup
167     * files are kept before the oldest is erased. This option takes
168     * a positive integer value. If set to zero, then there will be no
169     * backup files and the log file will be truncated when it reaches
170     * MaxFileSize.
171     *
172     * @param mixed $maxBackups
173     */
174    function setMaxBackupIndex($maxBackups)
175    {
176        if (is_numeric($maxBackups))
177            $this->maxBackupIndex = abs((int)$maxBackups);
178    }
179
180    /**
181     * Set the maximum size that the output file is allowed to reach
182     * before being rolled over to backup files.
183     *
184     * @param mixed $maxFileSize
185     * @see setMaxFileSize()
186     */
187    function setMaximumFileSize($maxFileSize)
188    {
189        $this->setMaxFileSize($maxFileSize);
190    }
191
192    /**
193     * Set the maximum size that the output file is allowed to reach
194     * before being rolled over to backup files.
195     * <p>In configuration files, the <b>MaxFileSize</b> option takes an
196     * long integer in the range 0 - 2^63. You can specify the value
197     * with the suffixes "KB", "MB" or "GB" so that the integer is
198     * interpreted being expressed respectively in kilobytes, megabytes
199     * or gigabytes. For example, the value "10KB" will be interpreted
200     * as 10240.
201     *
202     * @param mixed $value
203     */
204    function setMaxFileSize($value)
205    {
206        $maxFileSize = null;
207        $numpart = substr($value,0, strlen($value) -2);
208        $suffix  = strtoupper(substr($value, -2));
209
210        switch ($suffix) {
211            case 'KB': $maxFileSize = (int)((int)$numpart * 1024); break;
212            case 'MB': $maxFileSize = (int)((int)$numpart * 1024 * 1024); break;
213            case 'GB': $maxFileSize = (int)((int)$numpart * 1024 * 1024 * 1024); break;
214            default:
215                if (is_numeric($value)) {
216                    $maxFileSize = (int)$value;
217                }
218        }
219       
220        if ($maxFileSize === null) {
221            LoggerLog::debug("LoggerAppenderRollingFile::setMaxFileSize():value=[$value] wrong declaration");
222        } else {
223            $this->maxFileSize = abs($maxFileSize);
224        }
225    }
226
227    /**
228     * @param LoggerLoggingEvent $event
229     */
230    function append($event)
231    {
232        if ($this->fp) {
233            parent::append($event);
234            if (ftell($this->fp) > $this->getMaximumFileSize())   
235                $this->rollOver();
236        }
237    }
238}
239?>
Note: See TracBrowser for help on using the repository browser.