【问题标题】:Java Logger - Configuration changes not reflected on runtimeJava Logger - 配置更改未反映在运行时
【发布时间】:2014-01-14 00:19:39
【问题描述】:

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

当我加载 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 中设置了 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 logging java.util.logging


    【解决方案1】:

    这对我有用:

    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");
      }
    }
    

    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 Test.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>
    

    【讨论】:

    • 请查看我的原始答案,我提出了一个不同的配置文件供您尝试,您能帮我弄清楚为什么 formatter.format 属性没有更新吗?
    • 我的做法和你没有太大的不同。您如何尝试每次都以相同的方式创建记录器?在您的情况下: Logger.getLogger(this.getClass().getSimpleName());
    • 好的,不是这样。你能看到我的例子有效吗?它应该有你需要的一切。我将从有效的示例开始,并尝试将其更改为您所追求的。我给的东西很少,所以应该是直截了当的。
    • 我从您的示例开始,它运行良好,但是一旦我尝试更改 formatter.format 属性,它就不起作用,请检查我的原始答案以获取我拥有的属性文件根据您的答案修改。你能确认那个确切的方法是否有效吗?
    • 我为此使用了 Java6。在我看来,您使用的是 Java7 的功能,您可以在记录器配置 xml 中设置格式字符串。我注意到的一件事是,我在其他地方找到的示例(例如,svn.apache.org/repos/asf/river/jtsk/skunk/surrogate/testfiles/…)似乎是在 SimpleLoggingFormatter 上设置格式,而不是在 ConsoleHandler.formatter 上设置格式——所以可能需要在那里检查一下。我建议您回退到使用 Java API 来获取您喜欢的格式差异,而不是 XML 配置。
    【解决方案2】:

    查看Logger.getLogger(String name).documentation的文档

    它说

    如果创建了一个新的记录器,它的日志级别将基于 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");
    
        }
    }
    

    【讨论】:

    • 嗨,我试过了,结果一样。请检查我上面的新代码,我已经添加了更改
    • @ShehryarFarooq,你应该在阅读配置后添加,放在logManager.readConfiguration()之后;
    • @ShehryarFarooq,而不是 logManger.readConfiguration();将其替换为 logManager.readConfiguration(new FileInputStream(new File("config2.properties")));
    • 我已经试过了,在问这个问题之前,我会再试一次
    • 试过了,结果相同 :( ,第二条记录的消息没有按照我配置的方式显示(显示为 2 行)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    • 2014-11-26
    相关资源
    最近更新 更多