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.logging;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.testclassification.MiscTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.apache.log4j.Level;
028import org.apache.log4j.LogManager;
029import org.apache.log4j.Logger;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034/**
035 * This should be in the hbase-logging module but the {@link HBaseClassTestRule} is in hbase-common
036 * so we can only put the class in hbase-common module for now...
037 */
038@Category({ MiscTests.class, SmallTests.class })
039public class TestLog4jUtils {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestLog4jUtils.class);
044
045  @Test
046  public void test() {
047    Logger zk = LogManager.getLogger("org.apache.zookeeper");
048    Level zkLevel = zk.getEffectiveLevel();
049    Logger hbaseZk = LogManager.getLogger("org.apache.hadoop.hbase.zookeeper");
050    Level hbaseZkLevel = hbaseZk.getEffectiveLevel();
051    Logger client = LogManager.getLogger("org.apache.hadoop.hbase.client");
052    Level clientLevel = client.getEffectiveLevel();
053    Log4jUtils.disableZkAndClientLoggers();
054    assertEquals(Level.OFF, zk.getLevel());
055    assertEquals(Level.OFF.toString(), Log4jUtils.getEffectiveLevel(zk.getName()));
056    assertEquals(Level.OFF, hbaseZk.getLevel());
057    assertEquals(Level.OFF.toString(), Log4jUtils.getEffectiveLevel(hbaseZk.getName()));
058    assertEquals(Level.OFF, client.getLevel());
059    assertEquals(Level.OFF.toString(), Log4jUtils.getEffectiveLevel(client.getName()));
060    // restore the level
061    zk.setLevel(zkLevel);
062    hbaseZk.setLevel(hbaseZkLevel);
063    client.setLevel(clientLevel);
064  }
065
066  @Test
067  public void testGetLogFiles() throws IOException {
068    // we use console appender in tests so the active log files should be empty
069    assertTrue(Log4jUtils.getActiveLogFiles().isEmpty());
070  }
071}