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

Revision 18220, 7.7 KB checked in by yokkuns, 11 years ago (diff)

#149 ロガークラス作成

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