All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.umlg.sqlg.TestAppender Maven / Gradle / Ivy

package org.umlg.sqlg;

import java.util.LinkedList;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

/**
 * Test appender to capture some explicit messages
 * @author JP Moresmau 
 * 
 */
public class TestAppender extends AppenderSkeleton {
	private static final LinkedList eventsList = new LinkedList<>();
		
	public static LinkedList getEventsList() {
		return eventsList;
	}
	
	/**
	 * get last event of the given name AND CLEARS THE LIST
	 * 
	 * @param name the event name (class where event was logged)
	 * @return the event or null if none
	 */
	public static LoggingEvent getLast(String name){
		synchronized (eventsList) {
			if (eventsList.isEmpty()){
				return null;
			}
			LoggingEvent evt=eventsList.removeLast();
			while (evt!=null && !evt.getLoggerName().equals(name)){
				evt=eventsList.removeLast();
			}
			eventsList.clear();
			return evt;
		}
	}
	
	public TestAppender() {
		
	}

	public TestAppender(boolean isActive) {
		super(isActive);
	}

	@Override
	public void close() {

	}

	@Override
	public boolean requiresLayout() {
		return false;
	}

	@Override
	protected void append(LoggingEvent event) {
		synchronized (eventsList) {
			eventsList.add(event);
			// keep memory low, since we want the last event usually anyway
			if (eventsList.size()>10){
				eventsList.removeFirst();
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy