时间:2022-07-19 10:17:59 | 栏目:JAVA代码 | 点击:次
public void info(String format, Object argArray[]) { if(logger.isInfoEnabled()) { FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable()); } }
private final Logger log = LoggerFactory.getLogger(getClass()); log.info("backlog={}", new Object[]{backlog});//backlog为Java对象,可重写toString()方法来实现输出具体属性
输出如下:
backlog=Backlog [backlogId=null, employeeId=36, backlogHead=Test, backlogBody=时间:2016-10-1,地点:二楼四号会议室, createDate=Tue Nov 01 16:44:03 CST 2016, scheduledDate=Mon Feb 01 16:44:03 CST 2016]
private final Logger log = LoggerFactory.getLogger(getClass()); log.info("add Backlog -> employeeId={}, backlogHead={}, scheduledDate={}", new Object[]{ backlog.getEmployeeId(), backlog.getBacklogHead(), backlog.getScheduledDate()});
输出如下:
add Backlog -> employeeId=36, backlogHead=Test, scheduledDate=Mon Feb 01 16:44:03 CST 2016]
public void info(String msg) { logger.log(FQCN, Level.INFO, msg, null); }
log.info("backlog=" + backlog);
输出结果:
backlog=Backlog [backlogId=null, employeeId=36, backlogHead=Test, backlogBody=时间:2016-10-1,地点:二楼四号会议室, createDate=Tue Nov 01 16:44:03 CST 2016, scheduledDate=Mon Feb 01 16:44:03 CST 2016]
public void info(String format, Object arg) { if(logger.isInfoEnabled()) { FormattingTuple ft = MessageFormatter.format(format, arg); logger.log(FQCN, Level.INFO, ft.getMessage(), ft.getThrowable()); } }
log.info("delete backlog -> backlogId={}", backlogId);
输出如下:
delete backlog -> backlogId=69
总结:如果第二个参数是Object,则第一个String参数里要有”{}”对应,如果是Object[]数组,数组里有几个对象,前面的String参数就要有几个{}花括号对应。
不知道项目里用的是啥 ** 版本的 log4j
居然不能传入变长参数
logger.info(String.format("%s %s %s", username, feature, "1111"));
只好采用这种方式啦