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 junit.framework.TestCase;
21  import org.apache.log4j.Appender;
22  import org.apache.log4j.ConsoleAppender;
23  import org.apache.log4j.Level;
24  import org.apache.log4j.LogManager;
25  import org.apache.log4j.Logger;
26  import org.apache.log4j.PatternLayout;
27  import org.apache.log4j.filter.LevelRangeFilter;
28  import org.apache.log4j.util.Compare;
29  import org.apache.log4j.extras.DOMConfigurator;
30  
31  import java.io.FileNotFoundException;
32  import java.io.IOException;
33  import java.io.InputStream;
34  
35  
36  /***
37   *
38   * Tests of rolling file appender with a filter based triggering policy.
39   *
40   * @author Curt Arnold
41   *
42   */
43  public class FilterBasedRollingTest extends TestCase {
44    public FilterBasedRollingTest(String name) {
45      super(name);
46    }
47  
48    public void setUp() {
49        Appender ca = new ConsoleAppender(new PatternLayout("%d %level %c -%m%n"));
50        ca.setName("CONSOLE");
51        Logger.getRootLogger().addAppender(ca);
52    }
53  
54    public void tearDown() {
55      LogManager.getLoggerRepository().resetConfiguration();
56    }
57  
58      private static void configure(final String configName) throws IOException {
59        String resourceName = configName;
60        int lastSlash = resourceName.lastIndexOf("/");
61        if (lastSlash >= 0) {
62            resourceName = resourceName.substring(lastSlash + 1);
63        }
64        InputStream is = TimeBasedRollingTest.class.getResourceAsStream(resourceName);
65        if (is == null) {
66            throw new FileNotFoundException("Could not find resource " + resourceName);
67        }
68        DOMConfigurator configurator = new DOMConfigurator();
69        configurator.doConfigure(is, LogManager.getLoggerRepository());
70    }
71    /***
72     * Test basic rolling functionality using configuration file.
73     */
74    public void test1() throws Exception {
75      configure("./input/rolling/filter1.xml");
76      common("filterBased-test1");
77    }
78  
79    /***
80     * Test basic rolling functionality using explicit configuration.
81     * Test fails when run immediately after test1.
82     */
83    public void test2() throws Exception {
84      PatternLayout layout = new PatternLayout("%m\n");
85      RollingFileAppender rfa = new RollingFileAppender();
86      rfa.setName("ROLLING");
87      rfa.setLayout(layout);
88  
89      FixedWindowRollingPolicy swrp = new FixedWindowRollingPolicy();
90      FilterBasedTriggeringPolicy fbtp = new FilterBasedTriggeringPolicy();
91  
92      LevelRangeFilter rf = new LevelRangeFilter();
93      rf.setLevelMin(Level.INFO);
94      fbtp.addFilter(rf);
95      fbtp.activateOptions();
96  
97      swrp.setMinIndex(0);
98      rfa.setFile("filterBased-test2.log");
99      rfa.setAppend(false);
100 
101     swrp.setFileNamePattern("filterBased-test2.%i");
102     swrp.activateOptions();
103 
104     rfa.setRollingPolicy(swrp);
105     rfa.setTriggeringPolicy(fbtp);
106     rfa.activateOptions();
107     Logger.getRootLogger().addAppender(rfa);
108     Logger.getRootLogger().setLevel(Level.DEBUG);
109 
110     common("filterBased-test2");
111   }
112 
113   /***
114    *   Common aspects of test1 and test2
115    */
116   private void common(String baseName) throws Exception {
117     Logger logger = Logger.getLogger(FilterBasedRollingTest.class);
118 
119     // Write exactly 10 bytes with each log
120     for (int i = 0; i < 25; i++) {
121       Thread.sleep(100);
122 
123       if (i < 10) {
124         logger.debug("Hello---" + i);
125       } else if (i < 100) {
126         if ((i % 10) == 0) {
127           //  on the 10th and 20th request, raise the severity
128           logger.warn("Hello--" + i);
129         } else {
130           logger.debug("Hello--" + i);
131         }
132       }
133     }
134 
135     //
136     //  test was constructed to mimic SizeBasedRollingTest.test2
137     //
138     assertTrue(
139       Compare.compare(FilterBasedRollingTest.class,
140               baseName + ".log", "witness/rolling/sbr-test2.log"));
141     assertTrue(
142       Compare.compare(FilterBasedRollingTest.class,
143               baseName + ".0", "witness/rolling/sbr-test2.0"));
144     assertTrue(
145       Compare.compare(FilterBasedRollingTest.class,
146               baseName + ".1", "witness/rolling/sbr-test2.1"));
147   }
148 }