java 在静态和非静态上下文中创建 SLF4J 记录器的开销是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10345109/
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
What's the overhead of creating a SLF4J loggers in static vs. non-static contexts?
提问by Kawu
I've always used the following pattern to construct (SLF4J) loggers:
我一直使用以下模式来构建(SLF4J)记录器:
private static final Logger log = LoggerFactory.getLogger(MyClass.class);
This has worked so far, but I was wondering about the static
context at some point and the need to pass in the concrete class literal all the time instead of just using a non-static logger like
到目前为止,这已经奏效,但我想知道static
某个时候的上下文,以及是否需要一直传入具体的类文字,而不是只使用像这样的非静态记录器
private final Logger log = LoggerFactory.getLogger(getClass());
This has basically been asked (and answered) before here for LOG4J
这在 LOG4J 之前基本上已经被问过(和回答)过
Should logger be private static or not
and here
和这里
Should be logger always final and static?
I realize final
is basically mandatory, so I'm left wondering how high the overhead of using SLF4J's in non-static context actually is.
我意识到final
基本上是强制性的,所以我想知道在非静态上下文中使用 SLF4J 的开销到底有多大。
Q:
问:
Is there any significant practicaloverhead of using
是否有任何重大的实际使用开销
private final Logger log = LoggerFactory.getLogger(getClass());
over
超过
private static final Logger log = LoggerFactory.getLogger(MyClass.class);
in the average(web) app? (no need to "discuss" high-end, heavy-load webapps here)
在普通(网络)应用程序中?(此处无需“讨论”高端、重载的 webapps)
Note, I'm ultimately planning to use an even nicer approach using CDI to obtain an SLF4J logger like
请注意,我最终计划使用更好的方法使用 CDI 来获得 SLF4J 记录器,例如
@Inject private final Logger log;
as described here http://www.seamframework.org/Weld/PortableExtensionsPackage#H-TtLoggerttInjection, but I need to know about the logger caching first.
如此处所述http://www.seamframework.org/Weld/PortableExtensionsPackage#H-TtLoggerttInjection,但我需要先了解记录器缓存。
Sub question: is it even possible to use?:
子问题:甚至可以使用吗?:
@Inject private static final Logger log;
(just beginning with CDI to be honest)
(老实说,刚刚从 CDI 开始)
采纳答案by Ceki
The overhead for non-static (instance) logger variables should be negligibleunless many, say 10000 or more, instantiations occur. The key word here is negligible. If many (>10000) objects are instantiated, the impact will probably be measurable but still be low.
非静态(实例)记录器变量的开销应该可以忽略不计,除非发生很多(比如 10000 次或更多)实例化。这里的关键词可以忽略不计。如果实例化了许多 (>10000) 个对象,则影响可能是可衡量的,但仍然很低。
More specifically, an instance logger increases the memory footprint by one reference (64 bits) per object instance. On the CPU side, the cost is one hash look up per instance, i.e. the cost of looking up the appropriate logger in a hash table (small). Again, both costs should be negligible unless many many objects are created.
更具体地说,实例记录器将每个对象实例的内存占用增加一个引用(64 位)。在 CPU 方面,成本是每个实例进行一次哈希查找,即在哈希表中查找适当的记录器的成本(小)。同样,除非创建许多对象,否则这两种成本都应该可以忽略不计。
This question is also discussed in the SLF4J FAQ.
SLF4J FAQ 中也讨论了这个问题。
回答by Christian Schneider
I am not sure about the exact overhead when using LoggerFactory but I doubt it will affect your application performance. So simply use static or non static as you see fit.
我不确定使用 LoggerFactory 时的确切开销,但我怀疑它会影响您的应用程序性能。因此,只需根据您的需要使用静态或非静态。
What should be the benefit of using @Inject. The LoggerFactory already provides and abstraction from the concrete impl. In any case it will be a lot slower than the LoggerFactory.
使用@Inject 应该有什么好处。LoggerFactory 已经提供了具体实现的抽象。在任何情况下,它都会比 LoggerFactory 慢很多。
The syntax is more concise when you use @Inject that is true. But imagine you use the class in a test. Then you have to setup the injection to get logging. With the normal LoggerFactory it also works nicely in tests. If java had a generic mechanism for @Inject it would work great but as it is the setup is more difficult.
当您使用 @Inject 时,语法更简洁,这是真的。但是想象一下你在测试中使用这个类。然后您必须设置注入以获取日志记录。使用普通的 LoggerFactory,它在测试中也能很好地工作。如果 java 有一个通用的 @Inject 机制,它会工作得很好,但因为它的设置更困难。