<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.commons.weaver.maven;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

/**
 * This class redirects calls to java.util.Logging to Mojo logging.
 */
public class JavaLoggingToMojoLoggingRedirector {
    private JDKLogHandler activeHandler;
    private List<Handler> removedHandlers = new ArrayList<Handler>();

    /**
     * The Maven mojo logger to delegate messages to.
     */
    private Log mojoLogger;

    /**
     * @param mojoLogger the Maven mojo logger to delegate messages to.
     */
    public JavaLoggingToMojoLoggingRedirector(Log mojoLogger) {
        this.mojoLogger = mojoLogger;
    }

    /**
     * Activates this feature.
     */
    public void activate() throws MojoExecutionException {
        try {
            Logger rootLogger = LogManager.getLogManager().getLogger("");
            // remove old handlers
            for (Handler handler : rootLogger.getHandlers()) {
                rootLogger.removeHandler(handler);
                removedHandlers.add(handler);
            }
            if (removedHandlers.size() == 0) {
                throw new MojoExecutionException("could not remove any handler. aborting.");
            }

            // add our own
            activeHandler = new JDKLogHandler();
            activeHandler.setLevel(Level.ALL);
            rootLogger.setLevel(Level.ALL);

            rootLogger.addHandler(activeHandler);
        } catch (Exception exc) {
            throw new MojoExecutionException("failed to activate the jul logging redirector", exc);
        }
    }

    /**
     * deactivate the redirection and put the original Handlers back in place again.
     */
    public void deactivate() {
        Logger rootLogger = LogManager.getLogManager().getLogger("");
        // remove old handlers
        for (Handler handler : rootLogger.getHandlers()) {
            if (handler == activeHandler) {
                rootLogger.removeHandler(handler);
            }
        }

        for (Handler oldHandler : removedHandlers) {
            rootLogger.addHandler(oldHandler);
        }
    }

    private class JDKLogHandler extends Handler {

        @Override
        public void publish(LogRecord record) {
            Throwable exception = record.getThrown();
            Level level = record.getLevel();
            if (level == Level.SEVERE && mojoLogger.isErrorEnabled()) {
                mojoLogger.error(getMessage(record), exception);
            } else if (level == Level.WARNING && mojoLogger.isWarnEnabled()) {
                mojoLogger.warn(getMessage(record), exception);
            } else if (level == Level.INFO && mojoLogger.isInfoEnabled()) {
                mojoLogger.info(getMessage(record), exception);
            } else if (level == Level.CONFIG && mojoLogger.isDebugEnabled()) {
                mojoLogger.debug(getMessage(record), exception);
            } else if (mojoLogger.isDebugEnabled()) {
                mojoLogger.debug(getMessage(record), exception);
            }<commit_msg>some plexus Logger implementations, namely that used by m2eclipse, assume that a Throwable argument is non-null<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.commons.weaver.maven;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;

/**
 * This class redirects calls to java.util.Logging to Mojo logging.
 */
public class JavaLoggingToMojoLoggingRedirector {
    private JDKLogHandler activeHandler;
    private List<Handler> removedHandlers = new ArrayList<Handler>();

    /**
     * The Maven mojo logger to delegate messages to.
     */
    private Log mojoLogger;

    /**
     * @param mojoLogger
     *            the Maven mojo logger to delegate messages to.
     */
    public JavaLoggingToMojoLoggingRedirector(Log mojoLogger) {
        this.mojoLogger = mojoLogger;
    }

    /**
     * Activates this feature.
     */
    public void activate() throws MojoExecutionException {
        try {
            Logger rootLogger = LogManager.getLogManager().getLogger("");
            // remove old handlers
            for (Handler handler : rootLogger.getHandlers()) {
                rootLogger.removeHandler(handler);
                removedHandlers.add(handler);
            }
            if (removedHandlers.size() == 0) {
                throw new MojoExecutionException("could not remove any handler. aborting.");
            }

            // add our own
            activeHandler = new JDKLogHandler();
            activeHandler.setLevel(Level.ALL);
            rootLogger.setLevel(Level.ALL);

            rootLogger.addHandler(activeHandler);
        } catch (Exception exc) {
            throw new MojoExecutionException("failed to activate the jul logging redirector", exc);
        }
    }

    /**
     * deactivate the redirection and put the original Handlers back in place
     * again.
     */
    public void deactivate() {
        Logger rootLogger = LogManager.getLogManager().getLogger("");
        // remove old handlers
        for (Handler handler : rootLogger.getHandlers()) {
            if (handler == activeHandler) {
                rootLogger.removeHandler(handler);
            }
        }

        for (Handler oldHandler : removedHandlers) {
            rootLogger.addHandler(oldHandler);
        }
    }

    private class JDKLogHandler extends Handler {

        @Override
        public void publish(LogRecord record) {
            final Throwable exception = record.getThrown();
            final Level level = record.getLevel();
            if (level == Level.SEVERE && mojoLogger.isErrorEnabled()) {
                if (exception == null) {
                    mojoLogger.error(getMessage(record));
                } else {
                    mojoLogger.error(getMessage(record), exception);
                }
            } else if (level == Level.WARNING && mojoLogger.isWarnEnabled()) {
                if (exception == null) {
                    mojoLogger.warn(getMessage(record));
                } else {
                    mojoLogger.warn(getMessage(record), exception);
                }
            } else if (level == Level.INFO && mojoLogger.isInfoEnabled()) {
                if (exception == null) {
                    mojoLogger.info(getMessage(record));
                } else {
                    mojoLogger.info(getMessage(record), exception);
                }
            } else if (mojoLogger.isDebugEnabled()) {
                if (exception == null) {
                    mojoLogger.debug(getMessage(record));
                } else {
                    mojoLogger.debug(getMessage(record), exception);
                }
            }
{'fraction_non_alphanumeric': 0.09081712936945038, 'fraction_numerical': 0.0012363718107227155, 'mean_word_length': 2.3577358490566036, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3878285', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2193, 'n_words': 668}