001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.http.log;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.BufferedReader;
023import java.io.InputStreamReader;
024import java.io.PrintStream;
025import java.net.URI;
026import java.net.URL;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.http.HttpServer;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.net.NetUtils;
032import org.apache.log4j.Level;
033import org.apache.log4j.LogManager;
034import org.apache.log4j.Logger;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.slf4j.LoggerFactory;
039import org.slf4j.impl.Log4jLoggerAdapter;
040
041@Category({MiscTests.class, SmallTests.class})
042public class TestLogLevel {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046      HBaseClassTestRule.forClass(TestLogLevel.class);
047
048  static final PrintStream out = System.out;
049
050  @Test
051  @SuppressWarnings("deprecation")
052  public void testDynamicLogLevel() throws Exception {
053    String logName = TestLogLevel.class.getName();
054    org.slf4j.Logger testlog = LoggerFactory.getLogger(logName);
055
056    //only test Log4JLogger
057    if (testlog instanceof Log4jLoggerAdapter) {
058      Logger log = LogManager.getLogger(logName);
059      log.debug("log.debug1");
060      log.info("log.info1");
061      log.error("log.error1");
062      assertTrue(!Level.ERROR.equals(log.getEffectiveLevel()));
063
064      HttpServer server = null;
065      try {
066        server = new HttpServer.Builder().setName("..")
067            .addEndpoint(new URI("http://localhost:0")).setFindPort(true)
068            .build();
069
070        server.start();
071        String authority = NetUtils.getHostPortString(server
072            .getConnectorAddress(0));
073
074        //servlet
075        URL url =
076            new URL("http://" + authority + "/logLevel?log=" + logName + "&level=" + Level.ERROR);
077        out.println("*** Connecting to " + url);
078        try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
079          for(String line; (line = in.readLine()) != null; out.println(line));
080        }
081        log.debug("log.debug2");
082        log.info("log.info2");
083        log.error("log.error2");
084        assertTrue(Level.ERROR.equals(log.getEffectiveLevel()));
085
086        //command line
087        String[] args = {"-setlevel", authority, logName, Level.DEBUG.toString()};
088        LogLevel.main(args);
089        log.debug("log.debug3");
090        log.info("log.info3");
091        log.error("log.error3");
092        assertTrue(Level.DEBUG.equals(log.getEffectiveLevel()));
093      } finally {
094        if (server != null) {
095          server.stop();
096        }
097      }
098    }
099    else {
100      out.println(testlog.getClass() + " not tested.");
101    }
102  }
103}