尝试将 log4j 打印到 Nutz Boot ioc 管理的数据库里,但会导致循环引用,因为ioc里也调用了 log() 方法,请问如何解决?
log4j.rootLogger=debug,Console,DB
log4j.appender.DB=io.gitee.overviewer.utils.LogDBPoolAppender
log4j.appender.DB.sql=INSERT INTO LOGS VALUES('%x','%d','%C','%p','%m')
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
log4j.appender.DB.bufferSize=100
package io.gitee.overviewer.utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.log4j.jdbc.JDBCAppender;
import org.nutz.boot.AppContext;
import org.nutz.dao.impl.NutDao;
import org.nutz.ioc.Ioc;
public class LogDBPoolAppender extends JDBCAppender {
protected Connection getConnection() throws SQLException {
AppContext ctx = AppContext.getDefault();
Ioc ioc = ctx.ioc();
NutDao dao = ioc.get(NutDao.class, "dao");
DataSource ds = dao.getDataSource();
return ds.getConnection();
}
protected void closeConnection(Connection con) {
try {
if(con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}