View Javadoc

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