org.zodiac.sdk.log.factory.LoggerSpaceFactory4CommonsLoggingBuilder Maven / Gradle / Ivy
package org.zodiac.sdk.log.factory;
import org.zodiac.sdk.log.LogSpace;
import org.zodiac.sdk.log.adapter.level.AdapterLevel;
import org.zodiac.sdk.toolkit.space.SpaceId;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* 日志实现仍然使用Log4j
,但兼容了commons-logging
日志打印的情况。
*/
public class LoggerSpaceFactory4CommonsLoggingBuilder extends AbstractLoggerSpaceFactoryBuilder {
public LoggerSpaceFactory4CommonsLoggingBuilder(SpaceId spaceId, LogSpace spaceInfo) {
super(spaceId, spaceInfo);
}
@Override
protected String getLoggingToolName() {
return "log4j";
}
@Override
public AbstractLoggerSpaceFactory doBuild(String spaceName, ClassLoader spaceClassloader, URL url) {
try {
final org.apache.log4j.spi.LoggerRepository repo = new org.apache.log4j.Hierarchy(new org.apache.log4j.spi.RootLogger((org.apache.log4j.Level) org.apache.log4j.Level.WARN));
org.apache.log4j.xml.DOMConfigurator domConfigurator = new org.apache.log4j.xml.DOMConfigurator();
Field field = org.apache.log4j.xml.DOMConfigurator.class.getDeclaredField("props");
field.setAccessible(true);
field.set(domConfigurator, getProperties());
domConfigurator.doConfigure(url, repo);
return new AbstractLoggerSpaceFactory(getLoggingToolName()) {
ConcurrentMap loggerMap = new ConcurrentHashMap();
@Override
public org.slf4j.Logger setLevel(String loggerName, AdapterLevel adapterLevel) throws Exception {
org.slf4j.impl.JCLLoggerAdapter jclLoggerAdapter = (org.slf4j.impl.JCLLoggerAdapter) this
.getLogger(loggerName);
/*Obtain Log4j instance.*/
org.apache.log4j.Logger log4j = repo.getLogger(loggerName);
org.apache.log4j.Level log4jLevel = this.toLog4jLevel(adapterLevel);
log4j.setLevel(log4jLevel);
return jclLoggerAdapter;
}
@Override
public org.slf4j.Logger getLogger(String name) {
org.slf4j.impl.JCLLoggerAdapter slf4jLogger = (org.slf4j.impl.JCLLoggerAdapter) this.loggerMap.get(name);
if (slf4jLogger != null) {
return slf4jLogger;
}
org.slf4j.impl.JCLLoggerAdapter newInst = createCommonsLoggingLoggerAdapter2Slf4j(name);
org.slf4j.impl.JCLLoggerAdapter oldInst = this.loggerMap.putIfAbsent(name, newInst);
return oldInst == null ? newInst : oldInst;
}
private org.slf4j.impl.JCLLoggerAdapter createCommonsLoggingLoggerAdapter2Slf4j(String name) {
/*Initialize Log4j instance.*/
org.apache.log4j.Logger log4j = repo.getLogger(name);
try {
org.apache.commons.logging.impl.Log4JLogger log4jLogger = new org.apache.commons.logging.impl.Log4JLogger(
log4j);
Constructor constructor = org.slf4j.impl.JCLLoggerAdapter.class
.getDeclaredConstructor(org.apache.commons.logging.Log.class, String.class);
constructor.setAccessible(true);
return constructor.newInstance(log4jLogger, name);
} catch (Throwable e) {
throw new IllegalStateException(
"get adapter from commons-logging to slf4j logger from Log4j err!", e);
}
}
private org.apache.log4j.Level toLog4jLevel(AdapterLevel adapterLevel) {
if (adapterLevel == null) {
throw new IllegalStateException(
"AdapterLevel is NULL when adapter common-logging and log4j.");
}
switch (adapterLevel) {
case TRACE:
return org.apache.log4j.Level.TRACE;
case DEBUG:
return org.apache.log4j.Level.DEBUG;
case INFO:
return org.apache.log4j.Level.INFO;
case WARN:
return org.apache.log4j.Level.WARN;
case ERROR:
return org.apache.log4j.Level.ERROR;
default:
throw new IllegalStateException(adapterLevel + " is unknown when adapter common-logging and log4j.");
}
}
};
} catch (Throwable e) {
throw new IllegalStateException("Log4j for commons-logging loggerSpaceFactory build error!", e);
}
}
}