尝试2: 自己写Adapter。 可以,但是日志中打印的类名仍然不对,没查到原因。。
日志如下
***************************************************
Application: AK47
Version: 0.1.0
Description: A date generator and loader for nwims
***************************************************
2016-10-11 17:14:54,831 INFO [ak47-main] Log4j2LogAdapter$Log4j2Logger - AK47 has started. Please enjoy it!
代码如下
public class AK47 {
private static Log log = null;
private static Ioc ioc = null;
static {
System.setProperty("log4j.configurationFile", "etc/log4j2.xml");
Log4j2LogAdapter adapter = new Log4j2LogAdapter();
if (adapter.canWork()) {
Logs.setAdapter(adapter);
} else {
System.out.println("日志组件没有正常启动!");
}
log = Logs.get();
}
public static void main(String[] args) {
ThreadContext.put("threadName", Thread.currentThread().getName());
Thread.currentThread().setName("ak47-main");
welcome();
ioc = IocMaster.getInstance();
// loadIoc();
// now();
}
private static void welcome() {
System.out.println("***************************************************");
System.out.println("Application: AK47");
System.out.println("Version: 0.1.0");
System.out.println("Description: A date generator and loader for nwims");
System.out.println("***************************************************");
log.info("AK47 has started. Please enjoy it!");
//
}
// private static void now() {
// Dao dao = ioc.get(Dao.class);
// Sql sql = Sqls.fetchString("select sysdate from dual");
// sql.setCallback(new FetchTimestampCallback());
// dao.execute(sql);
// log.info("Server time: " + sql.getString());
// }
}
public class Log4j2LogAdapter implements LogAdapter {
public Log4j2LogAdapter() {
}
public boolean canWork() {
try {
Class.forName("org.apache.logging.log4j.Logger", false, Log4j2LogAdapter.class.getClassLoader());
return true;
} catch (Throwable t) {
return false;
}
}
public Log getLogger(String className) {
return new Log4j2LogAdapter.Log4j2Logger(className);
}
static class Log4j2Logger extends AbstractLog {
private Logger logger;
private static boolean hasTrace;
Log4j2Logger(String className) {
this.logger = LogManager.getLogger(className);
this.isFatalEnabled = this.logger.isEnabled(Level.FATAL);
this.isErrorEnabled = this.logger.isEnabled(Level.ERROR);
this.isWarnEnabled = this.logger.isEnabled(Level.WARN);
this.isInfoEnabled = this.logger.isEnabled(Level.INFO);
this.isDebugEnabled = this.logger.isEnabled(Level.DEBUG);
if (hasTrace) {
this.isTraceEnabled = this.logger.isEnabled(Level.TRACE);
}
}
public void debug(Object message, Throwable t) {
if (this.isDebugEnabled()) {
this.logger.log(Level.DEBUG, message, t);
}
}
public void error(Object message, Throwable t) {
if (this.isErrorEnabled()) {
this.logger.log(Level.ERROR, message, t);
}
}
public void fatal(Object message, Throwable t) {
if (this.isFatalEnabled()) {
this.logger.log(Level.FATAL, message, t);
}
}
public void info(Object message, Throwable t) {
if (this.isInfoEnabled()) {
this.logger.log(Level.INFO, message, t);
}
}
public void trace(Object message, Throwable t) {
if (this.isTraceEnabled()) {
this.logger.log(Level.TRACE, message, t);
} else if (!hasTrace && this.isDebugEnabled()) {
this.logger.log(Level.DEBUG, message, t);
}
}
public void warn(Object message, Throwable t) {
if (this.isWarnEnabled()) {
this.logger.log(Level.WARN, message, t);
}
}
protected void log(int level, Object message, Throwable t) {
switch (level) {
case 0:
if (hasTrace) {
this.logger.log(Level.TRACE, message, t);
} else {
this.logger.log(Level.DEBUG, message, t);
}
break;
case 10:
this.logger.log(Level.DEBUG, message, t);
break;
case 20:
this.logger.log(Level.INFO, message, t);
break;
case 30:
this.logger.log(Level.WARN, message, t);
break;
case 40:
this.logger.log(Level.ERROR, message, t);
break;
case 50:
this.logger.log(Level.FATAL, message, t);
}
}
public boolean isDebugEnabled() {
return this.logger.isDebugEnabled();
}
public boolean isErrorEnabled() {
return this.logger.isEnabled(Level.ERROR);
}
public boolean isFatalEnabled() {
return this.logger.isEnabled(Level.FATAL);
}
public boolean isInfoEnabled() {
return this.logger.isInfoEnabled();
}
public boolean isTraceEnabled() {
return !hasTrace ? this.logger.isDebugEnabled() : this.logger.isTraceEnabled();
}
public boolean isWarnEnabled() {
return this.logger.isEnabled(Level.WARN);
}
static {
try {
Level.class.getDeclaredField("TRACE");
hasTrace = true;
} catch (Throwable var1) {
System.out.println(var1.getMessage());;
var1.printStackTrace();
}
}
}
}