<commit_before>/*
 *    ALMA - Atacama Large Millimiter Array
 *    (c) European Southern Observatory, 2004
 *    Copyright by ESO (in the framework of the ALMA collaboration),
 *    All rights reserved
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *    MA 02111-1307  USA
 */
package alma.acs.testsupport;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.ConsoleHandler;
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;

/**
 * Creates a Logger that collects all <code>LogRecord</code>s produced by calls to the various log methods.
 * This can be used to verify log messages in a pure JUnit environment, without the need to use TAT reference files. 
 * 
 * @author hsommer
 * created Apr 20, 2005 2:30:47 PM
 */
public class LogRecordCollectingLogger extends Logger {

    private List<LogRecord> logRecordList = Collections.synchronizedList(new ArrayList<LogRecord>());
    
    private boolean suppressLogs = false;
    private Logger delegate;
    
    /**
     * @see java.util.logging.Logger#getLogger(java.lang.String)
     */
    public static synchronized LogRecordCollectingLogger getCollectingLogger(String name) {
    	return getCollectingLogger(name, LogRecordCollectingLogger.class);
    }

    public static synchronized LogRecordCollectingLogger getCollectingLogger(String name, Class<? extends LogRecordCollectingLogger> loggerClass) {
        LogManager manager = LogManager.getLogManager();
        Logger logger = manager.getLogger(name);
        if (logger != null && !loggerClass.isInstance(logger)) {
        	throw new ClassCastException("Logger '" + name + "' is already used and is of wrong type " + logger.getClass().getName());
        }
        LogRecordCollectingLogger myLogger = (LogRecordCollectingLogger) logger;
        if (myLogger == null) {
        	try {
				Constructor<? extends LogRecordCollectingLogger> ctor = loggerClass.getConstructor(String.class, String.class);
				myLogger = ctor.newInstance(name, null);
				manager.addLogger(myLogger);
				myLogger = (LogRecordCollectingLogger) manager.getLogger(name);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        myLogger.setLevel(Level.FINEST);
        myLogger.setUseParentHandlers(false);
        Handler logHandler = new ConsoleHandler();
        logHandler.setLevel(Level.FINEST);
        myLogger.addHandler(logHandler);      

        return myLogger;
    }

    protected LogRecordCollectingLogger(String name, String resourceBundleName) {
        super(name, resourceBundleName);
    }

    /**
     * Here we intercept all logging activities of the base class.
     * <p>
     * 
     * @see java.util.logging.Logger#log(java.util.logging.LogRecord)
     */
    public void log(LogRecord record) {
        // must trigger a call to LogRecord#inferCaller before the log record gets formatted in a different thread        
        record.getSourceClassName();
        logRecordList.add(record);
        if (delegate != null && !suppressLogs) {
            delegate.log(record);
        }
    }

    
    public LogRecord[] getCollectedLogRecords() {
        return logRecordList.toArray(new LogRecord[logRecordList.size()]);
    }

    public void clearLogRecords() {
        logRecordList.clear();
    }
    
    /**<commit_msg>ctor must be public, otherwise static creator method fails to retrieve Constructor object (strange...)<commit_after>/*
 *    ALMA - Atacama Large Millimiter Array
 *    (c) European Southern Observatory, 2004
 *    Copyright by ESO (in the framework of the ALMA collaboration),
 *    All rights reserved
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *    MA 02111-1307  USA
 */
package alma.acs.testsupport;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.ConsoleHandler;
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;

/**
 * Creates a Logger that collects all <code>LogRecord</code>s produced by calls to the various log methods.
 * This can be used to verify log messages in a pure JUnit environment, without the need to use TAT reference files. 
 * 
 * @author hsommer
 * created Apr 20, 2005 2:30:47 PM
 */
public class LogRecordCollectingLogger extends Logger {

    private List<LogRecord> logRecordList = Collections.synchronizedList(new ArrayList<LogRecord>());
    
    private boolean suppressLogs = false;
    private Logger delegate;
    
    /**
     * @see java.util.logging.Logger#getLogger(java.lang.String)
     */
    public static synchronized LogRecordCollectingLogger getCollectingLogger(String name) {
    	return getCollectingLogger(name, LogRecordCollectingLogger.class);
    }

    public static synchronized LogRecordCollectingLogger getCollectingLogger(String name, Class<? extends LogRecordCollectingLogger> loggerClass) {
        LogManager manager = LogManager.getLogManager();
        Logger logger = manager.getLogger(name);
        if (logger != null && !loggerClass.isInstance(logger)) {
        	throw new ClassCastException("Logger '" + name + "' is already used and is of wrong type " + logger.getClass().getName());
        }
        LogRecordCollectingLogger myLogger = (LogRecordCollectingLogger) logger;
        if (myLogger == null) {
        	try {
				Constructor<? extends LogRecordCollectingLogger> ctor = loggerClass.getConstructor(String.class, String.class);
				myLogger = ctor.newInstance(name, null);
				manager.addLogger(myLogger); 
				myLogger = (LogRecordCollectingLogger) manager.getLogger(name);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        myLogger.setLevel(Level.FINEST);
        myLogger.setUseParentHandlers(false);
        Handler logHandler = new ConsoleHandler();
        logHandler.setLevel(Level.FINEST);
        myLogger.addHandler(logHandler);      

        return myLogger; 
    }

    public LogRecordCollectingLogger(String name, String resourceBundleName) {
        super(name, resourceBundleName);
    }

    /**
     * Here we intercept all logging activities of the base class.
     * <p>
     * 
     * @see java.util.logging.Logger#log(java.util.logging.LogRecord)
     */
    public void log(LogRecord record) {
        // must trigger a call to LogRecord#inferCaller before the log record gets formatted in a different thread        
        record.getSourceClassName();
        logRecordList.add(record);
        if (delegate != null && !suppressLogs) {
            delegate.log(record);
        }
    }

    
    public LogRecord[] getCollectedLogRecords() {
        return logRecordList.toArray(new LogRecord[logRecordList.size()]);
    }

    public void clearLogRecords() {
        logRecordList.clear();
    }
    
    /**
{'fraction_non_alphanumeric': 0.08556024378809189, 'fraction_numerical': 0.007266760431317393, 'mean_word_length': 3.226349678058445, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4673826', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2237, 'n_words': 739}