如下是slf4j-api包下的Logger接口类里其中几个方法的声明:

package org.slf4j;

public interface Logger {
/**
* Log a message at the INFO level.
*
* @param msg the message string to be logged
*/
public void info(String msg);
/**
* Log a message at the INFO level according to the specified format and argument.
*
* @param format the format string
* @param arg the argument
*/
public void info(String format, Object arg);
/**
* Log a message at the INFO level according to the specified format and arguments.
*
* @param format the format string
* @param arguments a list of 3 or more arguments
*/
public void info(String format, Object... arguments); /**
* Log a message at the ERROR level.
*
* @param msg the message string to be logged
*/
public void error(String msg);
public void error(String format, Object arg);
public void error(String format, Object... arguments); /**
* Log an exception (throwable) at the ERROR level with an accompanying message.
*
* @param msg the message accompanying the exception
* @param t the exception (throwable) to log
*/
public void error(String msg, Throwable t);
}

slf4j(Simple Logging Facade for Java)是Facade模式的典型应用,它定义了一套标准的日志接口,诸如logback、log4j、slf4j-simple等框架都是这个日志接口的具体实现。从这一点来看,slf4j的标准化显得相当重要,当然,从上面这些方法可见,它做到了!

我这里要点赞的也是这几个方法的定义。注意观察比较这几个有参的info/error方法的第一个参数:有的是format,有的是msg。

充分展现了代码的整洁之道,由此可以看出来作者是很讲究代码的可读性的。

看上面几个方法,

  • 如果记录异常信息,不妨调用error(String msg, Throwable t)方法。这时,第一个参数不是format,是msg。所以下面语句里的“{}”就有画蛇添足之嫌了:
try {
......
}
} catch (IOException e) {
LOG.error("#PayCenterHttpTransport,http调用出错!异常信息:{}", e);
}
  • 如果要打印更详细的info日志,可以调用logger.info那几个重载方法,支持用format形式。
log.info("融宝请求url:{},请求报文:{}", url, json);
  • logger.error也是支持format的。如下是Logger接口类里这个error重载方法的定义。注意调用方式是

    log.error("执行请求{}出现异常,",1,new Exception("test"));
  /**
* Log a message at the ERROR level according to the specified format and arguments.
* <p/>
* <p>This form avoids superfluous object creation when the logger is disabled for the ERROR level. </p>
*
* @param format the format string
* @param arg1 the first argument
* @param arg2 the second argument
*/
public void error(String format, Object arg1, Object arg2);

打印的异常日志是:

14:55:25.997 [main] ERROR ddd - 执行请求1出现异常,
java.lang.Exception: test
at com.emax.paycenter.common.util.MailUtil.main(MailUtil.java:100) [classes/:na]

=====over=====

最新文章

  1. c++的转换
  2. hdu-4496-D-City
  3. Facebook内部分享:25个高效工作的小技巧
  4. JDBC中的事务-Transaction
  5. C#:Func的同步、异步调用(转)
  6. 用宏定义封装LoadLibrary,方便的动态加载dll
  7. Android进阶笔记13:RoboBinding(实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架)
  8. poj 1696 Space Ant 极角排序
  9. Linux 系统管理06--磁盘管理
  10. obj-c中-fobjc-arc-exceptions的解释
  11. css点滴1—八种方式实现元素垂直居中
  12. 学习 Spring (十四) Introduction
  13. jQuery的位置信息
  14. ASP.Net MVC OA项目笔记&lt;五&gt;
  15. 使用docker-compose运行Django
  16. 如何在MyEclipse配置Gradle
  17. Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置
  18. 机器学习--聚类系列--K-means算法
  19. URAL 1997 Those are not the droids you&#39;re looking for
  20. React.js 小书 Lesson21 - ref 和 React.js 中的 DOM 操作

热门文章

  1. [转]用JAVA在读取EXCEL文件时如何判断列隐藏
  2. IFRAME练习 各种调用
  3. JavaScript鼠标拖动div且可调整div大小
  4. SpringMVC工作原理 : HandlerMapping和HandlerAdapter
  5. javascript:变量的作用域
  6. Windows补丁更新Tips
  7. 扁平数组构建DOM树
  8. 接口配置信息修改 请填写接口配置信息,此信息需要你有自己的服务器资源,填写的URL需要正确响应微信发送的Token验证
  9. Linux提权:从入门到放弃
  10. Java之JVM监控工具分享