Java Logger-运行时未反映配置更改
我有两个用于日志记录的配置文件, 配置1.1属性和 配置2.2属性Java Logger-运行时未反映配置更改,java,logging,java.util.logging,Java,Logging,Java.util.logging,我有两个用于日志记录的配置文件, 配置1.1属性和 配置2.2属性 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
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
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
当我加载config1.properties并记录一些内容时,格式是正确的,但在加载第二个配置文件之后,这些更改不会反映出来。这是我的密码:
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中设置配置,将消息记录在两行中,但消息仍显示在一行中
你知道为什么新配置没有生效吗?我确信我的配置文件是正确的,因为我尝试在config1之前加载config2,这在两行中显示了我记录的消息
以下是记录的结果:
[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
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
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
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
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
查看
Logger.getLogger(字符串名称)
的文档
上面说
如果创建了一个新的记录器,其日志级别将根据
LogManager配置,它将配置为也发送
将输出记录到其父处理程序。它将在
LogManager全局命名空间
因此,即使设置了新的配置属性,您的记录器实例也具有旧的配置
再次调用下面的行,尝试获取新实例
logger = Logger.getLogger("new Name");
可能您必须以不同的方式更改输入参数名称。或者它将返回旧的记录器对象
编辑
这里是我尝试的示例代码
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class LoggingTest {
public static void main(String[] args) {
System.setProperty("java.util.logging.config.file", "config1.properties");
Logger logger = Logger.getLogger(LoggingTest.class.getSimpleName());
logger.info("Message 1");
System.setProperty("java.util.logging.config.file", "config2.properties");
LogManager logManager = LogManager.getLogManager();
try {
logManager.readConfiguration();//logManager.readConfiguration(new FileInputStream(new File("config2.properties")));
} catch (IOException ex) {
Logger.getLogger(LoggingTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(LoggingTest.class.getName()).log(Level.SEVERE, null, ex);
}
logger = Logger.getLogger("NewLogger");
logger.info("Message 2");
}
}
查看
Logger.getLogger(字符串名称)
的文档
上面说
如果创建了一个新的记录器,其日志级别将根据
LogManager配置,它将配置为也发送
将输出记录到其父处理程序。它将在
LogManager全局命名空间
因此,即使设置了新的配置属性,您的记录器实例也具有旧的配置
再次调用下面的行,尝试获取新实例
logger = Logger.getLogger("new Name");
可能您必须以不同的方式更改输入参数名称。或者它将返回旧的记录器对象
编辑
这里是我尝试的示例代码
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class LoggingTest {
public static void main(String[] args) {
System.setProperty("java.util.logging.config.file", "config1.properties");
Logger logger = Logger.getLogger(LoggingTest.class.getSimpleName());
logger.info("Message 1");
System.setProperty("java.util.logging.config.file", "config2.properties");
LogManager logManager = LogManager.getLogManager();
try {
logManager.readConfiguration();//logManager.readConfiguration(new FileInputStream(new File("config2.properties")));
} catch (IOException ex) {
Logger.getLogger(LoggingTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(LoggingTest.class.getName()).log(Level.SEVERE, null, ex);
}
logger = Logger.getLogger("NewLogger");
logger.info("Message 2");
}
}
这对我很有用:
Test.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");
}
}
配置1.1属性
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
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
配置2.2属性
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
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 Test.javajava测试
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>
2014年1月13日晚上8:51:20测试干线
信息:信息1
2014-01-13T20:51:20
1389664280170
1.
试验
信息
试验
主要的
10
信息2
这对我很有用:
Test.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");
}
}
配置1.1属性
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
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
配置2.2属性
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
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 Test.javajava测试
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>
2014年1月13日晚上8:51:20测试干线
信息:信息1
2014-01-13T20:51:20
1389664280170
1.
试验
信息
试验
主要的
10
信息2
Hi,我试过这个,结果也一样。请检查我上面的新代码,我已经添加了changes@ShehryarFarooq,您应该在读取配置后添加它,将其放在logManager.readConfiguration()之后@ShehryarFarooq,而不是logManger.readConfiguration();将其替换为logManager.readConfiguration(新文件inputstream(新文件(“config2.properties”));在提问之前,我已经尝试过了,我会再次尝试,结果相同:(,第二条记录的消息没有以我配置的方式显示(显示在两行中)您好,我已经尝试了这个,同样的结果。请检查我的新代码上面,我已经添加了changes@ShehryarFarooq,您应该在读取配置后添加它,将其放在logManager.readConfiguration()之后;@ShehryarFarooq,而不是logManger.readConfiguration();将其替换为logManager.readConfiguration(新文件InputStream(新文件(“config2.properties”));在提问之前,我已经尝试过了,我将再次尝试,结果相同:(,第二条记录的消息没有以我配置的方式显示(显示在两行中)请检查我的原始答案,我建议您尝试使用不同的配置文件,您能帮我找出formatter.format属性未更新的原因吗?我的做法与您没有太大区别。您每次尝试以相同的方式创建记录器如何?在您的情况下:logger.getLogger(this.getClass().getSimpleName());好的,不是这样的..你能看到我的例子有效吗?它应该有你所需要的一切。我会从有效的例子开始,并尝试将其转换为你想要的。我给出的内容非常少,所以应该很简单。我从你的例子开始,它工作得很好,但只要我尝试更改formatter.format属性,它不起作用,请检查我的原始答案,查看我根据您的答案修改的属性文件。您能确认这一正确答案是否有效吗?我使用的是Java6。在我看来,您使用的是Java7的一个功能,您可以在logger config xml中设置格式字符串。我注意到的一点是,示例我在其他地方发现的s(例如)该模式似乎是在SimpleLoggingFormatter上设置格式,而不是像您在ConsoleHandler.formatter上那样设置格式——因此可能需要在那里进行检查。我建议您退回到使用Java API来获得您喜欢的格式差异,而不是XML配置。请检查我的原始答案,我已经提出了一个差异erent配置文件供您尝试,您能帮我找出formatter.format属性未更新的原因吗?我的做法与您没有太大不同。您每次尝试以相同的方式创建记录器如何?在您的示例中:logger.getLogger(this.getClass().getSimpleName());好的,不是这样的..你能看到我的例子有效吗?它应该有你所需要的一切。我会从有效的例子开始,并尝试将其转换为你想要的。我给出的内容非常少,所以应该很简单。我从你的例子开始,它工作得很好,但只要我尝试更改formatter.format财产