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