org.slf4j.Logger中info()方法的使用

  • 如果info()方法参数为以下类型:
    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());
        }
    }

使用范例1:

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]

使用范例2:

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]
  • 如果info()方法参数如下:
public void info(String msg)
    {
        logger.log(FQCN, Level.INFO, msg, null);
    }

使用范例1:

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]
  • 如果info()方法参数如下:
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());
        }
    }

使用范例1:

log.info("delete backlog -> backlogId={}", backlogId);

输出如下:

delete backlog -> backlogId=69

总结:如果第二个参数是Object,则第一个String参数里要有”{}”对应,如果是Object[]数组,数组里有几个对象,前面的String参数就要有几个{}花括号对应。

你可能感兴趣的:(Java,Log)