在运行时设置 java.util.logging.config.file

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13306014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:13:07  来源:igfitidea点击:

Setting java.util.logging.config.file at runtime

javaloggingruntimejava.util.logging

提问by radlan

I am trying to set the java util logging config file at runtime to avoid having to set it as a VM parameter. But this just doesn't work. Whenever I am trying to reread the configuration, logging is disabled at all.

我试图在运行时设置 java util 日志配置文件,以避免将其设置为 VM 参数。但这根本行不通。每当我尝试重新读取配置时,日志记录都被禁用。

See the following code snippet:

请参阅以下代码片段:

package test;

import java.io.FileInputStream;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class A {
  private static final Logger LOGGER= Logger.getLogger(A.class.getName());

  public static void main(String[] args) throws Exception {
    System.out.println("--- start");
    LOGGER.log(Level.SEVERE, "SEVERE 1");
    LOGGER.log(Level.FINEST, "FINEST 1");
    LogManager.getLogManager().readConfiguration();
    LOGGER.log(Level.SEVERE, "SEVERE 2");
    LOGGER.log(Level.FINEST, "FINEST 2");
    LogManager.getLogManager().readConfiguration(new FileInputStream("/tmp/logging.properties"));
    LOGGER.log(Level.SEVERE, "SEVERE 3");
    LOGGER.log(Level.FINEST, "FINEST 3");
    System.out.println("--- end");
  }
}

This is the output if I run the class without any VM argument:

如果我在没有任何 VM 参数的情况下运行该类,则这是输出:

--- start
09.11.2012 09:59:25 test.A main
SCHWERWIEGEND: SEVERE 1
09.11.2012 09:59:25 test.A main
SCHWERWIEGEND: SEVERE 2
--- end

As you can see, only the SEVERE levels are logged, as this is the default of the JREs logging.properties. Calling LogManager#readConfiguration()doesn't change anything. But when trying to read the configuration from my logging.properties, absolutely nothing is logged. There is no difference in calling LogManager#readConfiguration(InputStream)or setting the java.util.logging.config.fileproperty and calling LogManager#readConfiguration().

如您所见,只记录了 SEVERE 级别,因为这是 JRE 的 logging.properties 的默认值。打电话LogManager#readConfiguration()改变不了任何事情。但是当试图从我的 logging.properties 读取配置时,绝对没有记录任何内容。调用LogManager#readConfiguration(InputStream)或设置java.util.logging.config.file属性与调用LogManager#readConfiguration().

Now see the next output, when I run the same code with the VM property -Djava.util.logging.config.file=/tmp/logging.properties:

现在查看下一个输出,当我使用 VM 属性运行相同的代码时-Djava.util.logging.config.file=/tmp/logging.properties

--- start
2012-11-09 10:03:44.0838 SEVERE  [test.A#main()] - SEVERE 1
2012-11-09 10:03:44.0843 FINEST  [test.A#main()] - FINEST 1
--- end

As you can see, both the SEVERE and the FINEST levels are logged and they are logged in a different format. Both is specified in my custom logging.properties. But logging stops here after calling LogManager#readConfiguration()! This is different from the example above and I don't understand it. Also, just as in the example above, calling LogManager#readConfiguration(InputStream)doesn't work.

如您所见,SEVERE 和 FINEST 级别都被记录,并且它们以不同的格式记录。两者都在我的 custom 中指定logging.properties。但是在调用后记录停止在这里LogManager#readConfiguration()!这与上面的示例不同,我不明白。此外,就像上面的例子一样,调用LogManager#readConfiguration(InputStream)不起作用。

So what is the problem? According to the javadocsetting the java.util.logging.config.file property at runtime should work. Also both readConfiguration() methods should work as I expect. So what is the problem?

那么问题出在哪里呢?根据javadoc设置,运行时的 java.util.logging.config.file 属性应该可以工作。此外,两个 readConfiguration() 方法都应该按我的预期工作。那么问题出在哪里呢?

采纳答案by Udo Klimaschewski

Probably a problem with your logging properties. I noticed that I had to use both level specifications in the config (root and console) to get the result.
Maybe your root logger level is below FINEST, e.g. INFO(.level=INFO).
Or not set at all, in which case I suppose it to be INFO.

可能是您的日志记录属性有问题。我注意到我必须在配置(根和控制台)中使用两个级别规范才能获得结果。
也许您的根记录器级别低于FINEST,例如INFO( .level=INFO)。
或者根本没有设置,在这种情况下,我认为它是INFO.

I ran your code with the following logging.properties:

我使用以下 logging.properties 运行您的代码:

handlers=java.util.logging.ConsoleHandler
.level=FINEST
java.util.logging.ConsoleHandler.level=FINEST

Without specifying the -Djava.util.logging.config.file=/tmp/logging.propertiesoutput was:

没有指定-Djava.util.logging.config.file=/tmp/logging.properties输出是:

--- start
09.11.2012 14:25:49 testing.Scribble main
SCHWERWIEGEND: SEVERE 1
09.11.2012 14:25:49 testing.Scribble main
SCHWERWIEGEND: SEVERE 2
09.11.2012 14:25:49 testing.Scribble main
SCHWERWIEGEND: SEVERE 3
09.11.2012 14:25:49 testing.Scribble main
AM FEINSTEN: FINEST 3
--- end

Looks correct! (My test class is called testing.Scribble, thats the only difference)

看起来正确!(我的测试类叫做 testing.Scribble,这是唯一的区别)

Using -Djava.util.logging.config.file=/tmp/logging.propertiesoutput is:

使用-Djava.util.logging.config.file=/tmp/logging.properties输出是:

--- start
09.11.2012 14:31:06 testing.Scribble main
SCHWERWIEGEND: SEVERE 1
09.11.2012 14:31:06 testing.Scribble main
AM FEINSTEN: FINEST 1
09.11.2012 14:31:06 testing.Scribble main
SCHWERWIEGEND: SEVERE 2
09.11.2012 14:31:06 testing.Scribble main
AM FEINSTEN: FINEST 2
09.11.2012 14:31:06 testing.Scribble main
SCHWERWIEGEND: SEVERE 3
09.11.2012 14:31:06 testing.Scribble main
AM FEINSTEN: FINEST 3
--- end

Looks also correct!

看起来也没错!