[120] | 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.log; |
---|
| 19 | |
---|
| 20 | import java.io.*; |
---|
| 21 | import java.net.*; |
---|
| 22 | |
---|
| 23 | import org.apache.hadoop.http.HttpServer; |
---|
| 24 | |
---|
| 25 | import junit.framework.TestCase; |
---|
| 26 | import org.apache.commons.logging.*; |
---|
| 27 | import org.apache.commons.logging.impl.*; |
---|
| 28 | import org.apache.log4j.*; |
---|
| 29 | |
---|
| 30 | public class TestLogLevel extends TestCase { |
---|
| 31 | static final PrintStream out = System.out; |
---|
| 32 | |
---|
| 33 | public void testDynamicLogLevel() throws Exception { |
---|
| 34 | String logName = TestLogLevel.class.getName(); |
---|
| 35 | Log testlog = LogFactory.getLog(logName); |
---|
| 36 | |
---|
| 37 | //only test Log4JLogger |
---|
| 38 | if (testlog instanceof Log4JLogger) { |
---|
| 39 | Logger log = ((Log4JLogger)testlog).getLogger(); |
---|
| 40 | log.debug("log.debug1"); |
---|
| 41 | log.info("log.info1"); |
---|
| 42 | log.error("log.error1"); |
---|
| 43 | assertTrue(!Level.ERROR.equals(log.getEffectiveLevel())); |
---|
| 44 | |
---|
| 45 | HttpServer server = new HttpServer("..", "localhost", 22222, true); |
---|
| 46 | server.start(); |
---|
| 47 | int port = server.getPort(); |
---|
| 48 | |
---|
| 49 | //servlet |
---|
| 50 | URL url = new URL("http://localhost:" + port |
---|
| 51 | + "/logLevel?log=" + logName + "&level=" + Level.ERROR); |
---|
| 52 | out.println("*** Connecting to " + url); |
---|
| 53 | URLConnection connection = url.openConnection(); |
---|
| 54 | connection.connect(); |
---|
| 55 | |
---|
| 56 | BufferedReader in = new BufferedReader(new InputStreamReader( |
---|
| 57 | connection.getInputStream())); |
---|
| 58 | for(String line; (line = in.readLine()) != null; out.println(line)); |
---|
| 59 | in.close(); |
---|
| 60 | |
---|
| 61 | log.debug("log.debug2"); |
---|
| 62 | log.info("log.info2"); |
---|
| 63 | log.error("log.error2"); |
---|
| 64 | assertTrue(Level.ERROR.equals(log.getEffectiveLevel())); |
---|
| 65 | |
---|
| 66 | //command line |
---|
| 67 | String[] args = {"-setlevel", "localhost:"+port, logName,""+Level.DEBUG}; |
---|
| 68 | LogLevel.main(args); |
---|
| 69 | log.debug("log.debug3"); |
---|
| 70 | log.info("log.info3"); |
---|
| 71 | log.error("log.error3"); |
---|
| 72 | assertTrue(Level.DEBUG.equals(log.getEffectiveLevel())); |
---|
| 73 | } |
---|
| 74 | else { |
---|
| 75 | out.println(testlog.getClass() + " not tested."); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | } |
---|