升级 log4j到 log4j2 解决boot日志兼容问题

This commit is contained in:
zxm 2018-05-01 17:45:02 +08:00
parent c42b9bcc47
commit 9b151e34cc
1 changed files with 98 additions and 96 deletions

View File

@ -8,117 +8,119 @@ import com.len.base.BaseService;
import com.len.base.CurrentUser; import com.len.base.CurrentUser;
import com.len.exception.MyException; import com.len.exception.MyException;
import com.len.util.ReType; import com.len.util.ReType;
import org.apache.logging.log4j.LogManager;
import org.apache.shiro.SecurityUtils;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* @author zhuxiaomeng * @author zhuxiaomeng
* @date 2017/12/13. * @date 2017/12/13.
* @email 154040976@qq.com * @email 154040976@qq.com
*/ */
public abstract class BaseServiceImpl <T,E extends Serializable> implements BaseService<T,E> { public abstract class BaseServiceImpl<T, E extends Serializable> implements BaseService<T, E> {
private static Logger logger= LoggerFactory.getLogger(BaseServiceImpl.class); private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(BaseServiceImpl.class);
public abstract BaseMapper<T,E> getMappser(); public abstract BaseMapper<T, E> getMappser();
@Override @Override
public int deleteByPrimaryKey(E id) { public int deleteByPrimaryKey(E id) {
return getMappser().deleteByPrimaryKey(id); return getMappser().deleteByPrimaryKey(id);
}
@Override
public int insert(T record) {
record=addValue(record,true);
return getMappser().insert(record);
}
/**
* 通用注入创建 更新信息 可通过super调用
* @param record
* @param flag
* @return
*/
public T addValue(T record,boolean flag){
CurrentUser currentUser= (CurrentUser) SecurityUtils.getSubject().getSession().getAttribute("curentUser");
//统一处理公共字段
Class<?> clazz=record.getClass();
try {
if(flag){
Field field=clazz.getDeclaredField("createBy");
field.setAccessible(true);
field.set(record,currentUser.getId());
Field fieldDate=clazz.getDeclaredField("createDate");
fieldDate.setAccessible(true);
fieldDate.set(record,new Date());
}else{
Field field=clazz.getDeclaredField("updateBy");
field.setAccessible(true);
field.set(record,currentUser.getId());
Field fieldDate=clazz.getDeclaredField("updateDate");
fieldDate.setAccessible(true);
fieldDate.set(record,new Date());
}
} catch (NoSuchFieldException e) {
//无此字段
} catch (IllegalAccessException e) {
e.printStackTrace();
} }
return record;
}
@Override @Override
public int insertSelective(T record) { public int insert(T record) {
record=addValue(record,true); record = addValue(record, true);
return getMappser().insertSelective(record); return getMappser().insert(record);
} }
@Override /**
public T selectByPrimaryKey(E id) { * 通用注入创建 更新信息 可通过super调用
return getMappser().selectByPrimaryKey(id); *
} * @param record
* @param flag
@Override * @return
public int updateByPrimaryKeySelective(T record) { */
record=addValue(record,false); public T addValue(T record, boolean flag) {
return getMappser().updateByPrimaryKeySelective(record); CurrentUser currentUser = (CurrentUser) SecurityUtils.getSubject().getSession().getAttribute("curentUser");
} //统一处理公共字段
Class<?> clazz = record.getClass();
@Override try {
public int updateByPrimaryKey(T record) { if (flag) {
record=addValue(record,false); Field field = clazz.getDeclaredField("createBy");
return getMappser().updateByPrimaryKey(record); field.setAccessible(true);
} field.set(record, currentUser.getId());
Field fieldDate = clazz.getDeclaredField("createDate");
@Override fieldDate.setAccessible(true);
public List<T> selectListByPage(T record){ fieldDate.set(record, new Date());
return getMappser().selectListByPage(record); } else {
} Field field = clazz.getDeclaredField("updateBy");
field.setAccessible(true);
/** field.set(record, currentUser.getId());
* 公共展示类 Field fieldDate = clazz.getDeclaredField("updateDate");
* @param t 实体 fieldDate.setAccessible(true);
* @param page fieldDate.set(record, new Date());
* @param limit }
* @return } catch (NoSuchFieldException e) {
*/ //无此字段
@Override } catch (IllegalAccessException e) {
public String show(T t,int page,int limit){ e.printStackTrace();
List<T> tList=null; }
Page<T> tPage= PageHelper.startPage(page,limit); return record;
try{ }
tList=getMappser().selectListByPage(t);
}catch (MyException e){ @Override
logger.error("class:BaseServiceImpl ->method:show->message:"+e.getMessage()); public int insertSelective(T record) {
e.printStackTrace(); record = addValue(record, true);
return getMappser().insertSelective(record);
}
@Override
public T selectByPrimaryKey(E id) {
return getMappser().selectByPrimaryKey(id);
}
@Override
public int updateByPrimaryKeySelective(T record) {
record = addValue(record, false);
return getMappser().updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(T record) {
record = addValue(record, false);
return getMappser().updateByPrimaryKey(record);
}
@Override
public List<T> selectListByPage(T record) {
return getMappser().selectListByPage(record);
}
/**
* 公共展示类
*
* @param t 实体
* @param page
* @param limit
* @return
*/
@Override
public String show(T t, int page, int limit) {
List<T> tList = null;
Page<T> tPage = PageHelper.startPage(page, limit);
try {
tList = getMappser().selectListByPage(t);
} catch (MyException e) {
logger.error("class:BaseServiceImpl ->method:show->message:" + e.getMessage());
e.printStackTrace();
}
ReType reType = new ReType(tPage.getTotal(), tList);
return JSON.toJSONString(reType);
} }
ReType reType=new ReType(tPage.getTotal(),tList);
return JSON.toJSONString(reType);
}
} }