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