Java 基于String创建的slf4j和log4j的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19270797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between slf4j and log4j on the basis of String creation
提问by raw
In log4j if we write
在 log4j 中,如果我们写
**logger.debug("Processing trade with id: " + id + " symbol: " + symbol);**
it will create String in string pool but when we use slf4j we use parameter based like this
它会在字符串池中创建字符串,但是当我们使用 slf4j 时,我们使用基于这样的参数
**logger.debug("Processing trade with id: {} and symbol : {} ", id, symbol);**
So what is the difference between these two statement, slf4j will create String at run time or not ?
那么这两个语句有什么区别,slf4j 会不会在运行时创建 String 呢?
回答by Suresh Atta
I would say to increase performance by reducing String concatenations
.
我会说通过减少 String 来提高性能concatenations
。
When you write this
当你写这个
"Processing trade with id: " + id + " symbol: " + symbol
You are creating the printing string manually.
您正在手动创建打印字符串。
When you write
当你写
"Processing trade with id: {} and symbol : {} ", id, symbol
-------^id------------^symbol---------
In the second way before printing internally slf4j
maintaind and generate a new string again with concatenation (Haven't check the source code,may be a StringBuilder
).
第二种方式在打印之前内部slf4j
维护并用连接再次生成一个新字符串(没有查过源代码,可能是一个StringBuilder
)。
The {}
called as place holders and replace by the args passed by you.
该{}
称为占位并通过你传递的ARGS更换。
This form avoids superfluous string concatenation when the logger is disabled for the DEBUG level. However, this variant incurs the hidden (and relatively small) cost of creating an Object[] before invoking the method, even if this logger is disabled for DEBUG. The variants taking one and two arguments exist solely in order to avoid this hidden cost.
当记录器在调试级别被禁用时,这种形式避免了多余的字符串连接。但是,此变体在调用该方法之前会产生创建 Object[] 的隐藏(且相对较小)成本,即使此记录器已为 DEBUG 禁用。采用一个和两个参数的变体的存在仅仅是为了避免这种隐藏的成本。
Read how to use the format :How to use java.String.format in Scala?
回答by Santosh
So what is the difference between these two statement, slf4j will create String at rum time or not ?
那么这两个语句之间有什么区别,slf4j 是否会在朗姆酒时间创建 String 呢?
The strings will be created anyways irrespective of whether you use log4j or sl4j. sl4j is offering a convenience of place holder.
无论您使用 log4j 还是 sl4j,都会创建字符串。sl4j 提供了方便的占位符。
回答by Erdin? Ta?k?n
It is about String concatenation. First line always make String concat that expensive operation, second line is not concat if log level is not match debug. I am not sure only presume, matching log level second option can be better performance because of internal StringBuilder usage.
它是关于字符串连接的。第一行总是使 String concat 进行昂贵的操作,如果日志级别与调试不匹配,则第二行不会连接。我不确定是否只是假设,由于内部 StringBuilder 的使用,匹配日志级别的第二个选项可以获得更好的性能。
回答by dezzer10
The difference is increase of performance, in log4j the string is concatenated every time the line is evaluated even if log level is lower than debug so the string will never be used.
不同之处在于性能的提高,在 log4j 中,即使日志级别低于调试,每次评估行时都会连接字符串,因此永远不会使用该字符串。
slf4j, the string and parameters are passed through to the logger which only substitutes them if the log message is actually to be used.
在 slf4j 中,字符串和参数被传递给记录器,记录器仅在实际使用日志消息时才替换它们。
Imagine code with debug statements every few lines, when in production and debug is disabled that is a huge amount of string manipulation that will never be used.
想象一下每隔几行带有调试语句的代码,当在生产中和调试被禁用时,这是大量永远不会使用的字符串操作。
回答by Byorn
SLF4J is basically an abstraction layer. It is not a logging implementation. It means that if you're writing a library and you use SLF4J, you can give that library to someone else to use and they can choose which logging implementation to use with SLF4J e.g. log4j or the Java logging API. It helps prevent projects from being dependent on lots of logging APIs just because they use libraries that are dependent on them.
SLF4J 基本上是一个抽象层。它不是日志记录实现。这意味着如果您正在编写一个库并且您使用 SLF4J,您可以将该库提供给其他人使用,他们可以选择与 SLF4J 一起使用的日志记录实现,例如 log4j 或 Java 日志记录 API。它有助于防止项目仅仅因为使用依赖于它们的库而依赖于大量日志记录 API。
回答by Sagar Mhatre
With Log4j2 API, we can have logger.info("String: {} int : {}.", "Hello, World ", 10);
使用 Log4j2 API,我们可以有 logger.info("String: {} int : {}.", "Hello, World ", 10);