- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,有一次我将 java.util.logging
的根 Logger 及其所有处理程序设置为 Level.FINEST
,如下所示:
Logger.getLogger("").setLevel(Level.FINEST);
for (Handler handler : Logger.getLogger("").getHandlers()) {
handler.setLevel(Level.FINEST);
}
稍后我将创建一个类 Logger,如下所示:
private static final Logger JAVA_LOG = Logger.getLogger(MyClass.class.getName());
现在这个记录器级别是 INFO
(这似乎是 JUL 的默认值),而它的根记录器级别已正确设置为 FINEST
。不应该为根记录器设置级别也为根记录器的所有子记录器设置级别吗?我在检索类 Logger 时做错了什么吗?
最佳答案
不应该为根 Logger 设置 Level 也为根 Logger 的所有子记录器设置 Level 吗?
不,显然您正在创建一个新的 Logger 对象。 Javadoc for getLogger状态:
If a new logger is created its log level will be configured based on the LogManager configuration and it will configured to also send logging output to its parent's Handlers.
请注意,即使之前有同名的 Logger 对象,您也不能依赖 getLogger
返回相同的对象,因为如果没有强对象,它可能已被垃圾回收对象的引用。
如果您的目标是在您的项目中创建具有相同日志级别的所有 Logger 对象,您不应该以编程方式而是通过配置来实现。这就是 java.util.logging
派上用场的地方。总体优势是您可以在不同的日志级别运行您的应用程序,甚至无需更改一行代码。
只需编辑您的配置文件(本质上就是上面提到的 LogManager 配置
)。它称为 logging.properties
并且位于您的 JRE 目录中。您可以在那里编辑最后一段并配置项目范围或类缩小的日志级别:
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
myProject.MyClass.level = FINE
myProject.MyOtherNotSoImportantClass.level = INFO
mySecondProject.MainClass.level = NONE
一个更好的解决方案是将 logging.properties
复制到您的项目目录并使用
java -Djava.util.logging.config.file=./src/test/resources/logging.properties
关于java.util.logging.Logger 与根 Logger 具有不同的级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54786116/
作为一个新人中的菜鸡,最近在看一个项目的代码其中有这么一段看得我很是费解 public class AssembleMsgService { private static Logger logger
我已更改 Gradle 构建脚本以使用 Spring 2.0.1 而不是 1.5.9。当我运行 gradle build 时,出现错误:找不到符号 Logger logger = Logger.get
这是我的示例代码: public class Logs { private static Logs logHandler; public static Logs handler() {
我目前正在使用 Java 日志记录。我有一个记录器 cwh.A 和 cwh.B。是否可以通过 Java 从记录器 cwh 获取这些记录器,而不知道它们的名称? 最佳答案 可以通过日志管理器获取所有记录
在我的应用程序中,有一次我将 java.util.logging 的根 Logger 及其所有处理程序设置为 Level.FINEST,如下所示: Logger.getLogger("").setLe
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 7 年前。 Improv
我刚刚将 Java 插件升级到 1.7.0_25,这导致我几年前编写的一个小程序出现 fatal error (此后一直在维护)。 在小程序的 init() 方法的早期,我调用了 logger = L
logger.debug 和 logger.info 有什么区别? logger.debug什么时候打印? 最佳答案 我建议你看看这篇名为 "Short Introduction to log4j"
Fixed I tried reinstalling the plugin but that didn't work so i completely cleared my inteljii idea
我定义了一个记录器实例如下: private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); 我有一个要记录的
我正在 Windows 下使用 Python 3.4.2。就我而言, import logging logger = logging.getLogger('logger') logger.setLev
我目前正在使用 C# 开发一个新的 Prism 项目,并想在我的 Bootstrapper 类中创建一个 NLog Logger。不幸的是,无论我做什么,它都拒绝构建,并告诉我... 'Logger.
我已经有几年没有使用 Log4J 了。我现在正在使用 org.jboss.logging 包在 JBoss 5 下开发一个应用程序。当我以前这样做时,将 logger.info() 消息包含在日志记录
ActiveRecord::Base.logger = Logger.new(STDOUT) 的 redis 等价物是什么?我想看看redis在做什么。 最佳答案 $redis.client.logg
我正在尝试使用 log4js 创建日志文件。完成所有配置后运行 Node app.js 时,我收到以下错误。 错误: TypeError: logger.setLevel is not a funct
我在一个拥有大约 50 多名开发人员的 IT 部门工作。它曾经有大约 100 多名开发人员,但由于经济衰退而被削减。 当我们的部门规模扩大时,我们雄心勃勃地努力建立一个特殊的架构小组。 这个小组决定做
我正在尝试找出跨多个模块使用 python 日志记录的最佳实践。我在这里看到:http://docs.python.org/2/howto/logging#logging-from-multiple-
我想将 Logger 注入(inject)到在 WildFly 10 上运行的 Maven 动态 Web 项目中的 bean 中。 我在 POM 中添加了依赖项。 WildFly 中提供了范围:
Java 9 引入了一个新的记录器,即 java.lang.System.Logger但我们总是有java.util.logging.Logger . 此记录器有什么新功能以及它有何改进? 最佳答案
我在 eclipse Mars 中配置了我的环境,但是 log4j 出现错误。我使用 spring 4 和 Maven 作为依赖管理器。部分代码: Eclipse 在这一行显示了一个错误,等于后的第二
我是一名优秀的程序员,十分优秀!