gpt4 book ai didi

java - 非静态日志能说得过去吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:36:22 25 4
gpt4 key购买 nike

我是使用 Apache 公共(public)日志记录完成日志记录的代码库的唯一维护者。

所有类都包含这两个导入:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

然后很多类都包含这样的非静态日志实例化:

/** The log. */
private Log log = LogFactory.getLog(Xyz.class);

这有道理吗?

我可以安全地将所有这些更改为静态调用吗?

编辑 关于它(显然)可以派上用场的特殊情况:我的问题确实更多“整个代码库中的非静态日志是否合理?”

最佳答案

这取决于。这是来自 documentation :

Note that for application code, declaring the log member as "static" is more efficient as one Log object is created per class, and is recommended. However this is not safe to do for a class which may be deployed via a "shared" classloader in a servlet or j2ee container or similar environment. If the class may end up invoked with different thread-context-classloader values set then the member must not be declared static. The use of "static" should therefore be avoided in code within any "library" type project.

关于java - 非静态日志能说得过去吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4817491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com