View Javadoc

1   /*
2    * Copyright 1999,2005 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.log4j.rolling;
18  
19  import org.apache.log4j.Appender;
20  import org.apache.log4j.spi.LoggingEvent;
21  import org.apache.log4j.spi.OptionHandler;
22  
23  
24  /***
25   * SizeBasedTriggeringPolicy looks at size of the file being
26   * currently written to.
27   *
28   * @author Ceki Gülcü
29   * @author Curt Arnold
30   *
31   */
32  public final class SizeBasedTriggeringPolicy implements TriggeringPolicy,
33    OptionHandler {
34    /***
35     * Rollover threshold size in bytes.
36     */
37    private long maxFileSize = 10 * 1024 * 1024; // let 10 MB the default max size
38  
39    /***
40     * Constructs a new instance.
41     */
42    public SizeBasedTriggeringPolicy() {
43    }
44  
45    /***
46     * Constructs an new instance.
47     * @param maxFileSize rollover threshold size in bytes.
48     */
49    public SizeBasedTriggeringPolicy(final long maxFileSize) {
50      this.maxFileSize = maxFileSize;
51    }
52  
53    /***
54     * {@inheritDoc}
55     */
56    public boolean isTriggeringEvent(
57      final Appender appender, final LoggingEvent event, final String file,
58      final long fileLength) {
59      //System.out.println("Size"+file.length());
60      return (fileLength >= maxFileSize);
61    }
62  
63    /***
64     * Gets rollover threshold size in bytes.
65     * @return rollover threshold size in bytes.
66     */
67    public long getMaxFileSize() {
68      return maxFileSize;
69    }
70  
71    /***
72     * Sets rollover threshold size in bytes.
73     * @param l new value for rollover threshold size.
74     */
75    public void setMaxFileSize(long l) {
76      maxFileSize = l;
77    }
78  
79    /***
80     * Prepares policy for use.
81     */
82    public void activateOptions() {
83    }
84  }