问题描述
我总是使用以下模式构建(SLF4J)记录器:
private static final Logger log = LoggerFactory。 getLogger(MyClass.class);
到目前为止这已经奏效,但我想知道 static
某些时候的上下文以及需要一直传递具体的类文字,而不是仅使用非静态记录器,如
private final Logger log = LoggerFactory.getLogger(getClass());
这里基本上已经问过(并回答过)LOG4J
此处
我意识到最终
基本上是强制性的,所以我想知道在非静态环境中使用SLF4J的开销实际上有多高。
问:
使用
<$是否存在重大实际开销p $ p>
private final Logger log = LoggerFactory.getLogger(getClass());
over
private static final Logger log = LoggerFactory.getLogger(MyClass.class); 平均(网络)应用中的
? (这里不需要讨论高端,重载的webapps)
注意,我最终计划使用更好的方法使用CDI获取SLF4J记录器,如
@Inject private final Logger log;
如此处所述,但我需要先了解记录器缓存。
子问题:是否可以使用?:
@Inject private static final Logger登录;
(说实话,只是从CDI开始)
非静态(实例)记录器变量的开销应该可忽略,除非发生许多(例如10000或更多)实例化。这里的关键词可以忽略不计。如果实例化了很多(> 10000个)对象,则影响可能是可测量的但仍然很低。
更具体地说,实例记录器将内存占用增加一个引用(每个对象实例64位)。在CPU方面,成本是每个实例的一个散列查找,即在散列表(小)中查找适当的记录器的成本。同样,除非创建了许多对象,否则这两种成本都应该可以忽略不计。
这个问题也在。
I've always used the following pattern to construct (SLF4J) loggers:
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
private final Logger log = LoggerFactory.getLogger(getClass());
This has basically been asked (and answered) before here for 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.
Q:
Is there any significant practical overhead 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)
Note, I'm ultimately planning to use an even nicer approach using CDI to obtain an SLF4J logger like
@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.
Sub question: is it even possible to use?:
@Inject private static final Logger log;
(just beginning with CDI to be honest)
The overhead for non-static (instance) logger variables should be negligible unless 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.
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.
This question is also discussed in the SLF4J FAQ.
这篇关于在静态和非静态上下文中创建SLF4J记录器的开销是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!