View Javadoc

1   /**
2   * Licensed to the Apache Software Foundation (ASF) under one
3   * or more contributor license agreements.  See the NOTICE file
4   * distributed with this work for additional information
5   * regarding copyright ownership.  The ASF licenses this file
6   * to you under the Apache License, Version 2.0 (the
7   * "License"); you may not use this file except in compliance
8   * with 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  package org.apache.hadoop.hbase.http.log;
19  
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.*;
23  import java.net.*;
24  
25  import org.apache.hadoop.hbase.testclassification.SmallTests;
26  import org.apache.hadoop.hbase.http.HttpServer;
27  import org.apache.hadoop.net.NetUtils;
28  import org.apache.commons.logging.*;
29  import org.apache.commons.logging.impl.*;
30  import org.apache.log4j.*;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  @Category(SmallTests.class)
35  public class TestLogLevel {
36    static final PrintStream out = System.out;
37  
38    @Test (timeout=60000)
39    @SuppressWarnings("deprecation")
40    public void testDynamicLogLevel() throws Exception {
41      String logName = TestLogLevel.class.getName();
42      Log testlog = LogFactory.getLog(logName);
43  
44      //only test Log4JLogger
45      if (testlog instanceof Log4JLogger) {
46        Logger log = ((Log4JLogger)testlog).getLogger();
47        log.debug("log.debug1");
48        log.info("log.info1");
49        log.error("log.error1");
50        assertTrue(!Level.ERROR.equals(log.getEffectiveLevel()));
51  
52        HttpServer server = null;
53        try {
54          server = new HttpServer.Builder().setName("..")
55              .addEndpoint(new URI("http://localhost:0")).setFindPort(true)
56              .build();
57  
58          server.start();
59          String authority = NetUtils.getHostPortString(server
60              .getConnectorAddress(0));
61  
62          //servlet
63          URL url = new URL("http://" + authority + "/logLevel?log=" + logName
64              + "&level=" + Level.ERROR);
65          out.println("*** Connecting to " + url);
66          HttpURLConnection connection = (HttpURLConnection)url.openConnection();
67          connection.connect();
68  
69          BufferedReader in = new BufferedReader(new InputStreamReader(
70              connection.getInputStream()));
71          for(String line; (line = in.readLine()) != null; out.println(line));
72          in.close();
73          connection.disconnect();
74  
75          log.debug("log.debug2");
76          log.info("log.info2");
77          log.error("log.error2");
78          assertTrue(Level.ERROR.equals(log.getEffectiveLevel()));
79  
80          //command line
81          String[] args = {"-setlevel", authority, logName, Level.DEBUG.toString()};
82          LogLevel.main(args);
83          log.debug("log.debug3");
84          log.info("log.info3");
85          log.error("log.error3");
86          assertTrue(Level.DEBUG.equals(log.getEffectiveLevel()));
87        } finally {
88          if (server != null) {
89            server.stop();
90          }
91        }
92      }
93      else {
94        out.println(testlog.getClass() + " not tested.");
95      }
96    }
97  }