commons-logging提供了通用得接口和各种log得实现接合,我原来使用得log4j,所以就想
把log4j得代码改成commons-logging,找了一篇文章就照着做,把我原来得一个类改了下:
public class JdbcMap extends BaseMap {
/**
* All logging goes through this logger
*/
private static Log log = LogFactory.getLog(JdbcMap.class);
private Statement sta = null;
/**
* 通过session获得connection
* @return 当前session得connection
*/
public Connection getConnection() throws HibernateException{
return getSession().connection();
}
public Statement getStatement() throws HibernateException, SQLException{
if (sta == null){
sta = getSession().connection().createStatement();
}
return sta;
}
public void closeStatement() throws SQLException{
if (sta != null){
sta.close();
sta = null;
}
}
/**
* 执行查询
*
* @param sql 查询sql
* @return
* @throws HibernateException if can't get connection from session
* @throws SQLException if can't executequery from connection
*/
public ResultSet executeQuery(String sql)
throws HibernateException, SQLException{
if (log.isDebugEnabled()) {
log.debug("debug execute query: " + sql);
}
if (log.isInfoEnabled()) {
log.info("info execute query: " + sql);
}
Statement sta = getStatement();
ResultSet res = sta.executeQuery(sql);
return res;
}
public static void main(String[] args) {
if (log.isDebugEnabled()) {
log.debug("debug execute query: " );
}
if (log.isInfoEnabled()) {
log.info("info execute query: " );
}
}
}
我在web-inf/classses目录下建了commons-logging.properties和log4j.properties,我建了两个appender,
commons-logging.properties内容:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
log4j.properties内容:
log4j.rootLogger=debug, dest1,dest2
log4j.appender.dest1=org.apache.log4j.ConsoleAppender
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d %-5p %-5c{3} %x -> %m%n
log4j.appender.dest2=org.apache.log4j.RollingFileAppender
log4j.appender.dest2.File=c://bridge.log
log4j.appender.dest2.MaxFileSize=100KB
# Keep one backup file
log4j.appender.dest2.MaxBackupIndex=3
log4j.appender.dest2.layout=org.apache.log4j.PatternLayout
log4j.appender.dest2.layout.ConversionPattern=%d [%t] %-5p %-5c{3}(%L) %x -> %m%n
并把commons-logging.jar和log4j.jar都拷贝到了web-inf/lib目录下,所有准备工作都做完了,然后
我建立了一个servlet,调用了
jdbcmap map= new jdbcmap();
map.executeQuery(sql);
但是运行servlet后,只在控制台打出了这一句log.info("info execute query: " + sql); c://bridge.log
这个文件没有建立,所以得出结论commons-logging没有调用log4j,而是调用得jdklog(commons-logging默认先
找log4j,找不到就用jdk1.4log,要不然就simplelog),于是我就直接运行JdbcMap(见main函数),结果得到了我
想要得结果,c://bridge.log建立了,里面得内容和控制台打出得一样,log.debug("debug execute query: " )
这一句也打了出来.
终于说完了,有点罗嗦,大家帮我看看,最好试一试,有知道得就告诉我,
我怀疑是不是classloader得问题.
本文地址:http://com.8s8s.com/it/it16316.htm