Why use SLF4J instead of Log4J

  Every Java programmer knows logs for any Java application server program is especially vital, and many programmers already familiar with a variety of different logging libraries such as java.util.logging, Apache log4j, logback . But if you do not know SLF4J (Simple logging facade for Java), then it is time to learn to use the SLF4J in your project.

  In this article, we will learn how to use SLF4J than log4j or java.util.logging to be excellent. Since the last time I wrote a Java programmer 10 log skill has been for some time, and I do not remember everything I wrote about a log.

  Anyway, let's get back to this topic, SLF4J different from other log library, and other are very different. SLF4J (Simple logging Facade for Java) is not a real log realization, but a layer of abstraction (abstraction layer), which allows you to use any of the log library in the background. If you are in or generic API library written for internal and external can be used, then you really do not want to use your library clients must use logging library of your choice.

  If a project has been used log4j, and you load a library, say Apache Active MQ-- it relies on another log library logback, then you need to load it into account. But if the Apache Active MQ uses SLF4J, you can continue to use your log library and silent suffering endured loads and maintain a new log frame.

  Overall, SLF4J make your code independent of any one particular log API, which is an API for developers to develop a good idea. Although the idea is not abstract journal libraries new things and Apache commons logging has also been using this idea, but now SLF4J Java is quickly becoming the world's standard log. Let's look at a few SLF4J use instead of log4j, logback or java.util.logging the grounds.

  SLF4J对比Log4J,logback和java.util.Logging的优势

  正如我之前说的,在你的代码中使用SLF4J写日志语句的主要出发点是使得你的程序独立于任意特定的日志类库,依赖于特定类可能需要不同与你已有的配置,并且导致更多维护的麻烦。但除此之外,还要一个SLF4J API的特性使得我坚持使用SLF4J而抛弃我长期间钟爱的Lof4j的理由,是被称为占位符(place holder),在代码中表示为“{}”的特性。占位符是一个非常类似于在String的format()方法中的%s,因为它会在运行时被某个提供的实际字符串所替换。这不仅降低了你代码中字符串连接次数,而且还节省了新建的String对象。即使你可能没需要那些对象,但这个依旧成立,取决于你的生产环境的日志级别,例如在DEBUG或者INFO级别的字符串连接。因为String对象是不可修改的并且它们建立在一个String池中,它们消耗堆内存( heap memory)而且大多数时间他们是不被需要的,例如当你的应用程序在生产环境以ERROR级别运行时候,一个String使用在DEBUG语句就是不被需要的。通过使用SLF4J,你可以在运行时延迟字符串的建立,这意味着只有需要的String对象才被建立。而如果你已经使用log4j,那么你已经对于在if条件中使用debug语句这种变通方案十分熟悉了,但SLF4J的占位符就比这个好用得多。

  这是你在Log4j中使用的方案,但肯定这一点都不有趣并且降低了代码可读性因为增加了不必要的繁琐重复代码(boiler-plate code):

  if (logger.isDebugEnabled()) {

  logger.debug(Processing trade with id: + id + symbol: + symbol);

  }

  另一方面,如果你使用SLF4J的话,你可以得到在极简洁的格式的结果,就像以下展示的一样:

  logger.debug(Processing trade with id: {} and symbol : {} , id, symbol);

  在SLF4J,我们不需要字符串连接而且不会导致暂时不需要的字符串消耗。取而代之的,我们在一个以占位符和以参数传递实际值的模板格式下写日志信息。你可能会在想万一我有很个参数怎么办?嗯,那么你可以选择使用变量参数版本的日志方法或者用以Object数组传递。这是一个相当的方便和高效方法的打日志方法。记住,在生产最终日志信息的字符串之前,这个方法会检查一个特定的日志级别是不是打开了,这不仅降低了内存消耗而且预先降低了CPU去处理字符串连接命令的时间。这里是使用SLF4J日志方法的代码,来自于slf4j-log4j12-1.6.1.jar中的Log4j的适配器类Log4jLoggerAdapter。

  public void debug(String format, Object arg1, Object arg2) {

  if (logger.isDebugEnabled()) {

  FormattingTuple ft = MessageFormatter.format(format, arg1, arg2);

  logger.log(FQCN, Level.DEBUG, ft.getMessage(), ft.getThrowable());

  }

  }

  同时,我们也很值得知道打日志是对应用程序的性能有着很大影响的,在生产环节上只进行必要的日志记录是我们所建议的。

  怎么用SLF4J做Log4J的日志记录

  除了以上好处,我想还有一个告诫,就是为了使用SLF4J,你不仅需要包含SLF4J的API jar包,例如 slf4j-api-1.6.1.jar,还需要相关Jar包,这取决于你在后台使用的日志类库。如果你想要使用和Log4J 一起使用SLF4J ,Simple Logging Facade for Java,,你需要包含以下的Jar包在你的classpath中,取决于哪个SLF4J和你在使用的Log4J的版本。例如:

  slf4j-api-1.6.1.jar – JAR for SLF4J API

  log4j-1.2.16.jar – JAR for Log4J API

  slf4j-log4j12-1.6.1.jar – Log4J Adapter for SLF4J

  如果你在使用Maven去管理你的项目依赖,你只需要包含SLF4J JAR包,maven会包含它的依赖的相关包。为了和SLF4J一起中使用Log4J,你可以包含以下的依赖在你项目中的pom.xml。

  

  org.slf4j

  slf4j-log4j12

  1.6.1

  

  

  org.slf4j

  slf4j-log4j12

  1.6.1

  

  还有,如果你对于使用变量参数版本(variable argument version )的日志方法感兴趣的话,那么就导入SLF4J 1.7的版本吧。

  总结

  总结这次说的,我建议使用SLF4J的而不是直接使用 Log4j, commons logging, logback 或者 java.util.logging 已经足够充分了。

  在你的开源或内部类库中使用SLF4J会使得它独立于任何一个特定的日志实现,这意味着不需要管理多个日志配置或者多个日志类库,你的客户端会很感激这点。

  SLF4J提供了基于占位符的日志方法,这通过去除检查isDebugEnabled(), isInfoEnabled()等等,提高了代码可读性。

  通过使用SLF4J的日志方法,你可以延迟构建日志信息(Srting)的开销,直到你真正需要,这对于内存和CPU都是高效的。

  作为附注,更少的暂时的字符串意味着垃圾回收器(Garbage Collector)需要做更好的工作,这意味着你的应用程序有为更好的吞吐量和性能。

  这些好处只是冰山一角,你将在开始使用SL4J和阅读其中代码的时候知道更多的好处。我强烈建议,任何一个新的Java程序员,都应该使用SLF4J做日志而不是使用包括Log4J在内的其他日志API。

  日志便于分析错误和解决bug,但是频繁的输出日志很烦

 

Guess you like

Origin www.cnblogs.com/qfjavabd/p/10983876.html