package de.ugoe.cs.quest.log4j;

import java.util.logging.Level;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import de.ugoe.cs.util.console.Console;
import de.ugoe.cs.util.console.listener.ICommandListener;
import de.ugoe.cs.util.console.listener.IErrorListener;
import de.ugoe.cs.util.console.listener.IExceptionListener;
import de.ugoe.cs.util.console.listener.ITraceListener;

/**
 * <p>
 * Implements logging based on the log4j API.
 * </p>
 * 
 * @author Steffen Herbold
 * @version 1.0
 */
public class Log4JLogger implements IErrorListener, ITraceListener,
		IExceptionListener, ICommandListener {

	/**
	 * <p>
	 * Reference to the logger.
	 * </p>
	 */
	Logger logger;

	/**
	 * <p>
	 * Constructor. Creates a new Log4JLogger and registers the implemented
	 * listener with the {@link Console}.
	 * </p>
	 */
	public Log4JLogger() {
		PropertyConfigurator.configure("data/log4j.properties");
		logger = Logger.getLogger("de.ugoe.cs.quest");
		Console.getInstance().registerErrorListener(this);
		Console.getInstance().registerTraceListener(this);
		Console.getInstance().registerExceptionListener(this);
		Console.getInstance().registerCommandListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.ugoe.cs.util.console.listener.ICommandListener#commandNotification
	 * (java.lang.String)
	 */
	@Override
	public void commandNotification(String command) {
		logger.info("Command executed: " + command);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.ugoe.cs.util.console.listener.IExceptionListener#printStacktrace(java
	 * .lang.Exception)
	 */
	@Override
	public void logException(Exception e) {
		logger.error("", e);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.ugoe.cs.util.console.listener.ITraceListener#traceMsg(java.lang.String
	 * )
	 */
	@Override
	public void traceMsg(String traceMessage, Level level) {
	    logger.log(convertToLog4JLevel(level), traceMessage);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * de.ugoe.cs.util.console.listener.IErrorListener#errorMsg(java.lang.String
	 * )
	 */
	@Override
	public void errorMsg(String errMessage) {
		logger.error(errMessage);
	}
	
	/**
	 * <p>
	 * Converts the log level described by {@link Level} into a {@link org.apache.log4j.Level}.
	 * </p>
	 *
	 * @param level java.util.logger.Level severity
	 * @return org.apache.log4j.Level severity
	 */
	private org.apache.log4j.Level convertToLog4JLevel(Level level) {
	    if(level==Level.OFF) {
	        return org.apache.log4j.Level.OFF;
	    }
	    if(level==Level.SEVERE) {
	        return org.apache.log4j.Level.FATAL;
	    }
	    if(level==Level.WARNING) {
	        return org.apache.log4j.Level.WARN;
	    }
	    if(level==Level.INFO || level==Level.CONFIG) {
	        return org.apache.log4j.Level.INFO;
	    }
	    // remaining levels: FINE, FINER, FINEST, ALL
	    return org.apache.log4j.Level.ALL;
	}
}
