利用slf4j
打日志的正确方法
什么时候应该打日志
根基名目
必需利用参数化信息的方法:
logger.debug("Processing trade with id:[{}] and symbol : [{}] ", id, symbol);
对付debug日志,必需判定是否为debug级别后,才举办利用:
if (logger.isDebugEnabled()) {
logger.debug("Processing trade with id: " +id + " symbol: " + symbol);
}
不要举办字符串拼接,那样会发生许多String工具,占用空间,影响机能。
反例(不要这么做):
logger.debug("Processing trade with id: " + id + " symbol: " + symbol);
利用[]举办参数变量断绝
如有参数变量,应该写成如下写法:
logger.debug("Processing trade with id:[{}] and symbol : [{}] ", id, symbol);
这样的名目写法,可读性更好,对付排盘查题更有辅佐。
差异级此外利用
影响到措施正常运行、当前请求正常运行的异常环境:
不该该呈现的环境:
假如有Throwable信息,需要记录完成的仓库信息:
log.error("获取用户[{}]的用户信息时堕落",userName,e);
反例(不要这么做):
try{
....
}catch(Exception ex){
String errorMessage=String.format("Error while reading information of user [%s]",userName);
logger.error(errorMessage,ex);
throw new UserServiceException(errorMessage,ex);
}
不该该呈现可是不影响措施、当前请求正常运行的异常环境:
即将靠近临界值的时候,劳务派遣管理系统,譬喻:
业务异常的记录,好比:
系统运行信息
外部接口部门
public List listByBaseType(Integer baseTypeId) {
log.info("开始查询基地");
BaseExample ex=new BaseExample();
BaseExample.Criteria ctr = ex.createCriteria();
ctr.andIsDeleteEqualTo(IsDelete.USE.getValue());
Optionals.doIfPresent(baseTypeId, ctr::andBaseTypeIdEqualTo);
log.info("查询基地竣事");
return baseRepository.selectByExample(ex);
}