package org.stagemonitor.jdbc.p6spy;

import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.P6Logger;

import java.util.LinkedList;
import java.util.List;

public class P6SpyMultiLogger implements P6Logger {

	private static List<P6Logger> loggers = new LinkedList<P6Logger>();

	public static void addLogger(P6Logger logger) {
		if (logger != null) {
			loggers.add(logger);
		}
	}

	@Override
	public void logSQL(int connectionId, String now, long elapsed, Category category, String prepared, String sql) {
		for (P6Logger logger : loggers) {
			if (logger.isCategoryEnabled(category)) {
				logger.logSQL(connectionId, now, elapsed, category, prepared, sql);
			}
		}
	}

	@Override
	public void logException(Exception e) {
		for (P6Logger logger : loggers) {
			logger.logException(e);
		}
	}

	@Override
	public void logText(String text) {
		for (P6Logger logger : loggers) {
			logger.logText(text);
		}
	}

	@Override
	public boolean isCategoryEnabled(Category category) {
		boolean result = false;
		for (P6Logger logger : loggers) {
			result |= logger.isCategoryEnabled(category);
		}
		return result;
	}
}

package org.stagemonitor.jdbc.p6spy;

import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.P6Logger;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class P6SpyMultiLogger implements P6Logger {

	private static List<P6Logger> loggers = new CopyOnWriteArrayList<>();

	public static void addLogger(P6Logger logger) {
		if (logger != null) {
			loggers.add(logger);
		}
	}

	@Override
	public void logSQL(int connectionId, String now, long elapsed, Category category, String prepared, String sql) {
		for (P6Logger logger : loggers) {
			if (logger.isCategoryEnabled(category)) {
				logger.logSQL(connectionId, now, elapsed, category, prepared, sql);
			}
		}
	}

	@Override
	public void logException(Exception e) {
		for (P6Logger logger : loggers) {
			logger.logException(e);
		}
	}

	@Override
	public void logText(String text) {
		for (P6Logger logger : loggers) {
			logger.logText(text);
		}
	}

	@Override
	public boolean isCategoryEnabled(Category category) {
		boolean result = false;
		for (P6Logger logger : loggers) {
			result |= logger.isCategoryEnabled(category);
		}
		return result;
	}
}

Fix non thread safe list
Fix non thread safe list

If seen this in our CI:

[INFO] STDOUT: Caused by: java.lang.NullPointerException
at java.util.LinkedList$ListItr.next(LinkedList.java:893)
~[?:1.8.0_60]
at
org.stagemonitor.jdbc.p6spy.P6SpyMultiLogger.isCategoryEnabled(P6SpyMultiLogger.java:45)
~[stagemonitor-jdbc-0.18.0.jar!/:0.18.0]
at
com.p6spy.engine.common.P6LogQuery.isCategoryOk(P6LogQuery.java:169)
~[p6spy-2.1.4.jar!/:?]
at
com.p6spy.engine.common.P6LogQuery.logElapsed(P6LogQuery.java:209)
~[p6spy-2.1.4.jar!/:?]
at
com.p6spy.engine.common.P6LogQuery.logElapsed(P6LogQuery.java:203)
~[p6spy-2.1.4.jar!/:?]
at
com.p6spy.engine.logging.P6LogStatementExecuteDelegate.invoke(P6LogStatementExecuteDelegate.java:51) ~[p6spy-2.1.4.jar!/:?]

Linked Lists and their iterators are not thread safe, so as we have a shared
static instance here I guess that needs to be somewhat safer.
