最近遇到了一个问题,使用commons-logging 结合log4j 日志无法正常输入到控制台及文件,结果想和大家分享一下
因为引用Jar包都是通过以前项目拷贝过来的,所以也没有排除那些不用的jar包。
因为现在公司产品都使用slf4j打印日志,这个是没有问题的,但是如果使用commons-logging,当存在以下jar包时候就会有问题
Jcl-over-slf4j.jar
这个jar默认会找slf4的logfactory进行创建日志,因此commons-logging就无效了。
去掉这个jar是不是就可以了呢?虽然现在可以使用commons-logging输出日志了,但是没有按照Log4j.properties的配置输出日志,并且日志名称为jxl.log,这个日志明显是操作excel的jxl生成的日志,所以找到jxl.jar
发现jxl的log4jLogger.class 覆盖了log4j的配置,适用了jxl的配置,因此无法按照我们自己的log4j.properties进行日志输出,除非在程序手动加载log4j.properties,否则那种默认直接加载classpath下的log4j.properties是无法适用的。
package cn.framework.dao.impl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import oracle.sql.CLOB;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.internal.CriteriaImpl;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.hibernate.persister.entity.SingleTableEntityPersister;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StringType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import cn.framework.dao.BaseHibernateDao;
import cn.framework.dao.model.OrderBy;
import cn.framework.dao.util.DaoUtil;
import cn.framework.util.Page;
import cn.framework.util.PropertyFilter;
import cn.framework.util.ReflectUtil;
import cn.framework.util.ReflectionUtils;
import cn.framework.util.SimpleUtils;
import cn.framework.util.StringUtil;
import cn.framework.util.PropertyFilter.MatchType;
@Repository
@SuppressWarnings("unchecked")
public abstract class BaseHibernateDaoImpl<T> implements BaseHibernateDao{
private LinkedList<OrderBy> orderList = new LinkedList<OrderBy>();
private final Logger logger=Logger.getLogger(getClass());
protected SessionFactory sessionFactory;
protected Class<T> entityClass;
String orderPropertyName = "";
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
/**
* 用于Dao层子类使用的构造函数.
* 通过子类的泛型定义取得对象类型Class.
* eg.
* public class UserDao extends SimpleHibernateDao<User, Long>
*/
public BaseHibernateDaoImpl() {
this.entityClass = ReflectionUtils.getSuperClassGenricType(getClass());
}
/**
* 用于用于省略Dao层, 在Service层直接使用通用SimpleHibernateDao的构造函数.
* 在构造函数中定义对象类型Class.
* eg.
* SimpleHibernateDao<User, Long> userDao = new SimpleHibernateDao<User, Long>(sessionFactory, User.class);
*/
public BaseHibernateDaoImpl(final SessionFactory sessionFactory, final Class<T> entityClass) {
this.sessionFactory = sessionFactory;
this.entityClass = entityClass;
}
private boolean desc = false;
@Autowired
DaoUtil daoUtil;
public boolean delete(Object domain) {
try {
getSession().delete(domain);
getSession().flush();
return true;
} catch (Exception e) {
logger.error(e);
return false;
}
}
/**
* 根据Criterion条件创建Criteria.
*
* 本类封装的find()函数全部默认返回对象类型为T,当不为T时使用本函数.
*
* @param criterions 数量可变的Criterion.
*/
public Criteria createCriteria(final Criterion... criterions) {
Criteria criteria = getSession().createCriteria(entityClass);
for (Criterion c : criterions) {
criteria.add(c);
}
return criteria;
}
public boolean deleteByKey(Serializable id) {
try {
getSession().delete(findById(id));
getSession().flush();
return true;
} catch (Exception e) {
logger.error(e);
return false;
}
}
public boolean deleteListByKey(String tableName,String keyColumn,String keyValues){
try {
StringBuffer sql=new StringBuffer("delete from ");
if(StringUtils.isBlank(tableName)){
return false;
}
if(StringUtils.isBlank(keyColumn) && StringUtils.isBlank(keyValues)){
}
sql.append(tableName).append(" where 1=1").append(" and ").append(keyColumn).append(" in (").append(keyValues).append(")");
System.out.println("deleteListByKey:"+sql.toString());
return executeSql(sql.toString());
} catch (Exception e) {
logger.error(e);
return false;
}
}
public boolean deleteListByKey(Class obj,String keyValues){
String keyColumn=getPrimaryKeyColumn(obj);
String tableName=getTableName();
return deleteListByKey(tableName,keyColumn,keyValues);
}
public boolean deleteListByObj(List domainList) {
try {
deleteAll(domainList);
return true;
} catch (Exception e) {
logger.error(e);
return false;
}
}
public T save(Object domain)throws Exception {
getSession().save(domain);
getSession().flush();
return (T) domain;
}
public T saveOrUpdate(Object domain)throws Exception {
getSession().saveOrUpdate(domain);
getSession().flush();
return (T) domain;
}
public T update(Object domain)throws Exception {
getSession().update(domain);
getSession().flush();
return (T) domain;
}
public T merge(Object domain) throws Exception{
getSession().merge(domain);
getSession().flush();
return (T) domain;
}
public List<T> findListByObj(Object value) {
return findListByObj(value);
}
public List<T> findListByObjR(Object object) {
return findListByObjR(object, null, null);
}
public List<T> findListByObjR(Object object, Integer firstResult,
Integer size) {
final Object f_object = object;
final Integer f_firstResult = firstResult;
final Integer f_size = size;
Criteria criteria = getSession().createCriteria(f_object.getClass());
Example exampleAtt = Example.create(f_object);
criteria.add(exampleAtt);
Map<String, Class> map = ReflectUtil
.getUserClassTypePropertyMap(f_object.getClass());
if (map != null && !map.isEmpty()) {
for (String key : map.keySet()) {
String getterMethodName = daoUtil.propertyToGetterMethod(key);
Object beanValue = ReflectUtil.invokeMethod(f_object,
getterMethodName);
if (beanValue != null) {
Class cla = map.get(key);
if (cla != null) {
String keyProperty = getPrimaryKeyParam(cla);
if (StringUtils.isNotBlank(keyProperty)) {
// criteria.createCriteria(key);
String _getterMethodName = daoUtil
//后台
[HttpPost]
public ActionResult GetNextLazyLoadProduct(int[] productIdList){
var products = _productService.GetProductsByIds(productIdList);
var preparedModel = PrepareProductOverviewModels(products);
return PartialView("_LazyLoadProducts", preparedModel);
}
//前台
var url = "@Url.Action("GetNextLazyLoadProduct")";
$.ajax({
url: url,
type: "POST",
datatype: "json",
contentType: "application/json",
data: JSON.stringify({ productIdList: array }),
error: function () {
},
success: function (data) {
$("#lazyScrollLoading").append(data);
$("#lazyLoadNum").attr("value", nextLazyLoadNum);
}
});