001/*
002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thomas Roger
018 *
019 */
020
021package org.nuxeo.runtime.test.runner;
022
023import org.apache.log4j.ConsoleAppender;
024import org.apache.log4j.Level;
025import org.apache.log4j.Logger;
026import org.apache.log4j.Priority;
027
028/**
029 * @since 9.3
030 */
031public class LogFeature extends SimpleFeature {
032
033    protected static final String CONSOLE_APPENDER_NAME = "CONSOLE";
034
035    protected Priority consoleThresold;
036
037    public void hideWarningFromConsoleLog() {
038        setConsoleLogThreshold(Level.ERROR.toString());
039    }
040
041    /**
042     * @since 9.10
043     */
044    public void hideErrorFromConsoleLog() {
045        setConsoleLogThreshold(Level.FATAL.toString());
046    }
047
048    /**
049     * @since 9.10
050     */
051    public void setConsoleLogThreshold(String level) {
052        if (consoleThresold != null) {
053            return;
054        }
055
056        Logger rootLogger = Logger.getRootLogger();
057        ConsoleAppender consoleAppender = (ConsoleAppender) rootLogger.getAppender(CONSOLE_APPENDER_NAME);
058        consoleThresold = consoleAppender.getThreshold();
059        consoleAppender.setThreshold(Level.toLevel(level));
060    }
061
062    public void restoreConsoleLog() {
063        if (consoleThresold == null) {
064            return;
065        }
066
067        Logger rootLogger = Logger.getRootLogger();
068        ConsoleAppender consoleAppender = (ConsoleAppender) rootLogger.getAppender(CONSOLE_APPENDER_NAME);
069        consoleAppender.setThreshold(consoleThresold);
070        consoleThresold = null;
071    }
072
073}