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

com.app.common.logger.AsyncSlf4jLoggerFactory Maven / Gradle / Ivy

The newest version!
package com.app.common.logger;

import java.util.Hashtable;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Level;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.app.common.thread.AsyncThreadGroup;

 
public class AsyncSlf4jLoggerFactory {

	private static AsyncThreadGroup threadGroup;
	private static Hashtable hashtable = new Hashtable();
	private static boolean isInit = false;

 	public synchronized static void initialize(PropertiesConfiguration pc) {
	 
		PropertyConfigurator.configureAndWatch(pc.getString("log4j.file"), 1000);
		 
		if (pc.getBoolean("log4j.async", true) && !isInit) {
			final int maxSize=pc.getInt("log4j.size", 10);
			AsyncLogger.setWriteTime(pc.getBoolean("log4j.writeTime", false));
			threadGroup = new AsyncThreadGroup(pc.getInt("log4j.thread", 10)) {
				{
					this.maxQueueSize=maxSize;
				}
				@Override
				protected void process(Object[] t) {
					asyncLog((AsyncSlf4jLogger) t[0], (String) t[1], (Integer) t[2], (Throwable) t[3], (Object[]) t[4],
							(Long) t[5], false);
				}
			};
			 
			isInit = true;
		}
	}

	public synchronized static Logger getLogger(Class class1) {
		Logger slf4jLogger = LoggerFactory.getLogger(class1);
		if (!isInit) {
			return slf4jLogger;
		}
		AsyncSlf4jLogger logger = hashtable.get(slf4jLogger);
		if (logger == null) {
			logger = new AsyncSlf4jLogger(slf4jLogger);
			hashtable.put(slf4jLogger, logger);
		}
		return logger;
	}

	public static void asyncLog(AsyncSlf4jLogger logger, String format, int logLevel, Throwable t, Object[] arguments,
			long time, boolean async) {
		if (async) {
			threadGroup.add(new Object[] { logger, format, logLevel, t, arguments, time });
		} else {
			switch (logLevel) {
			case Level.DEBUG_INT:
				if (logger.isDebugEnabled()) {
					AsyncLogger.debug(logger.getSlf4jLogger(), format, t, arguments, time);
				}
				break;
			case Level.INFO_INT:
				if (logger.isInfoEnabled()) {
					AsyncLogger.info(logger.getSlf4jLogger(), format, t, arguments, time);
				}
				break;
			case Level.WARN_INT:
				if (logger.isWarnEnabled()) {
					AsyncLogger.warn(logger.getSlf4jLogger(), format, t, arguments, time);
				}
				break;
			case Level.ERROR_INT:
				if (logger.isErrorEnabled()) {
					AsyncLogger.error(logger.getSlf4jLogger(), format, t, arguments, time);
				}
				break;
			default:
				break;
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy