1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
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 }