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