gpt4 book ai didi

Java Logger - 配置更改未反射(reflect)在运行时

转载 作者:行者123 更新时间:2023-12-01 18:37:50 24 4
gpt4 key购买 nike

我有 2 个用于日志记录的配置文件,config1.properties 和配置2.属性

当我加载 config1.properties 并记录某些内容时,格式是正确的,但紧接着,当我加载第二个配置文件时,更改没有反射(reflect)出来。这是我的代码:

System.setProperty("java.util.logging.config.file", "config1.properties");
logger = Logger.getLogger(this.getClass().getSimpleName());
logger.info("Message 1");
System.setProperty("java.util.logging.config.file", "config2.properties");
LogManager logManager = LogManager.getLogManager();
logManager.readConfiguration();
logger = Logger.getLogger("NewLogger");
logger.info("Message 2");

我已在 config2.properties 中设置配置以将消息记录在 2 行中,但该消息仍然显示在一行中。

有什么想法为什么新配置没有生效吗?我确信我的配置文件是正确的,因为我尝试在 config1 之前加载 config2,并且这在 2 行中显示了我记录的消息。

这是记录的结果:

[01-13-2014 16:48:56:186] LoggerUnitTest INFO: Message 1
[01-13-2014 16:48:56:195] LoggerUnitTest INFO: Message 2

它应该显示为:

[01-13-2014 16:48:56:186] LoggerUnitTest INFO: Message 1
[01-13-2014 16:48:56:195] LoggerUnitTest INFO:

消息2

以下是我正在使用的配置文件:

config1.properties

handlers=java.util.logging.ConsoleHandler

.level= FINE

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.formatter.format = [%1$tm-%1$td-%1$tY %1$tk:%1$tM:%1$tS:%1$tL] %4$s: %5$s%6$s%n

config2.properties

handlers=java.util.logging.ConsoleHandler

.level= FINE

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
# Note that this line is different from the line in config1
java.util.logging.ConsoleHandler.formatter.format = [%1$tm-%1$td-%1$tY %1$tk:%1$tM:%1$tS:%1$tL] %n %4$s: %5$s%6$s%n

最佳答案

这对我有用:

测试.java

import java.util.logging.LogManager;
import java.util.logging.Logger;

public class Test {
public static void main(String[] args) throws Exception {
System.setProperty("java.util.logging.config.file", "config1.properties");
Logger logger = Logger.getLogger(Test.class.getSimpleName());
logger.info("Message 1");
System.setProperty("java.util.logging.config.file", "config2.properties");
LogManager logManager = LogManager.getLogManager();
logManager.readConfiguration();
logger = Logger.getLogger(Test.class.getSimpleName());
logger.info("Message 2");
}
}

config1.properties

handlers=java.util.logging.ConsoleHandler

.level= FINE

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

config2.properties

handlers=java.util.logging.ConsoleHandler

.level= FINE

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.XMLFormatter

javac 测试.java
java测试

Jan 13, 2014 8:51:20 PM Test main
INFO: Message 1
<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2014-01-13T20:51:20</date>
<millis>1389664280170</millis>
<sequence>1</sequence>
<logger>Test</logger>
<level>INFO</level>
<class>Test</class>
<method>main</method>
<thread>10</thread>
<message>Message 2</message>
</record>

关于Java Logger - 配置更改未反射(reflect)在运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21103960/

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