package common;

import java.util.logging.ErrorManager;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;

/**
 * A class to dictate to which output logs are sent to
 */
public class LogConsoleHandler extends Handler {
	@Override
	public void close() {
		throw new UnsupportedOperationException("Not Implemented");
	}

	@Override
	public void flush() {
		throw new UnsupportedOperationException("Not Implemented");
	}

	@Override
	public void publish(LogRecord record) {
		if (getFormatter() == null) {
			setFormatter(new SimpleFormatter());
		}

		try {
			String message = getFormatter().format(record);
			if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
				System.err.write(message.getBytes());
			} else {
				System.out.write(message.getBytes());
			}
		} catch (Exception exception) {
			reportError(null, exception, ErrorManager.FORMAT_FAILURE);
		}

	}
}
