package org.ihtsdo.otf.mapping.jpa.helpers;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class LoggerUtility {

	private static Map<String, Boolean> initializationFlags = new HashMap<String, Boolean>();
	private static String fileName;
	private static String loggerName;

	private static void intializeLogger(String loggerName) {

		// remove the previous file of the same name
		removeFile();

		final PatternLayout layOut = new PatternLayout();
		layOut.setConversionPattern("%d{yyyy-MM-dd_HH:mm:ss.SSS} %p - %m%n");

		final FileAppender appender = new FileAppender();
		appender.setName(loggerName);
		appender.setAppend(true);
		// appender.setMaxFileSize("1MB");
		// appender.setMaxBackupIndex(1);
		appender.setFile(fileName);
		appender.setLayout(layOut);
		appender.activateOptions();

		Logger.getLogger(loggerName).addAppender(appender);
		Logger.getLogger(loggerName).setLevel(Level.INFO);

	}

	/**
	 * Return the logger for the given name.
	 * 
	 * @param loggerName
	 * @return Loggger {@see org.apache.log4j.Logger}
	 */
	public static Logger getLogger(String loggerName) {

		LoggerUtility.loggerName = loggerName;

		if (initializationFlags != null) {
			if (initializationFlags.get(loggerName) == null
					|| initializationFlags.get(loggerName) == false) {
				intializeLogger(loggerName);
				initializationFlags.put(loggerName, true);
				return Logger.getLogger(loggerName);
			} else {
				return Logger.getLogger(loggerName);
			}
		} else {
			return Logger.getLogger(loggerName);
		}
	}

	/**
	 * Sets the configuration for the logger
	 * 
	 * @param loggerName Name of logger to be used for logging
	 * @param fileName Full path including file name
	 */
	public static void setConfiguration(String loggerName, String fileName) {
		LoggerUtility.fileName = fileName;
		LoggerUtility.loggerName = loggerName;
	}

	/**
	 * Remove a logger when no longer needed.
	 * 
	 * @param loggerName Name of the logger to be removed.
	 */
	public static void removeLogger(String loggerName) {
		if (initializationFlags.containsKey(loggerName)) {
			initializationFlags.remove(loggerName);
		}
		Logger.getLogger(loggerName).removeAppender(loggerName);

	}

	private static void removeFile() {

		try {
			File file = new File(LoggerUtility.fileName);
			file.delete();
		} catch (Exception e) {
			// noop
		}
	}

}

@@ -1375,24 +1375,41 @@
 t(loggerName
+ + %22%7C%22 + fileName
 ) == null%0A%09%09
@@ -1444,24 +1444,41 @@
 t(loggerName
+ + %22%7C%22 + fileName
 ) == false) 
@@ -1505,24 +1505,41 @@
 r(loggerName
+ + %22%7C%22 + fileName
 );%0A%09%09%09%09initi
@@ -1567,16 +1567,33 @@
 ggerName
+ + %22%7C%22 + fileName
 , true);
@@ -1627,24 +1627,41 @@
 r(loggerName
+ + %22%7C%22 + fileName
 );%0A%09%09%09%7D else
@@ -1697,24 +1697,41 @@
 r(loggerName
+ + %22%7C%22 + fileName
 );%0A%09%09%09%7D%0A%09%09%7D 
@@ -1766,32 +1766,49 @@
 ogger(loggerName
+ + %22%7C%22 + fileName
 );%0A%09%09%7D%0A%09%7D%0A%0A%09/**%0A

update to logging so each project logs to its own file.