在 JAR 中停用 Geotools INFO 日志

Deactivate Geotools INFO logs in JAR

我正在使用 Geotools(版本 19.2)获取一些功能。为了停用 Geotools 日志记录(<严重),我尝试了两件事:

  1. 据我了解记录文档 (documentation page):

Logging.getLogger("org.geotools").setLevel(Level.SEVERE);

  1. 正在加载自定义 logging.properties 配置

配置(logging.properties)如下所示:

# standard log level
.level = WARNING

handlers = java.util.logging.ConsoleHandler

## limit the messages that are printed on the console to >= WARNING!
java.util.logging.ConsoleHandler.level = WARNING
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.encoding = Cp850

# do not use logging files!
java.util.logging.FileHandler.level = OFF

## just output geotools logs with level SEVERE!
org.geotools.level = SEVERE

然后我使用此代码加载配置:

LogManager.getLogManager().readConfiguration(MyMainClass.class.getClassLoader().getResourceAsStream("logging.properties"));

如果我在 Eclipse 中 运行 我的程序,使用这两种方法我得到 没有日志输出。如果我 运行 我的程序作为 JAR 文件,我会得到以下 不需要的日志输出 :

Nov. 08, 2018 9:48:13 VORM. org.geotools.jdbc.JDBCDataStore getAggregateExpression INFO: Visitor class org.geotools.feature.visitor.CountVisitor has no aggregate attribute.

INFO 日志位于 JDBCDataStore 中的 private Expression getAggregateExpression(FeatureVisitor visitor)(参见 GitHub

知道为什么日志记录配置对生成的 JAR 不起作用吗?

使用 JConsole(在 answer on Suppress java util logging from 3rd party jar 之后)我发现我的日志记录配置被我的 Java 安装的 logging.properties 文件覆盖了。

我试图在我的主要方法中更改日志记录配置,但这是错误的。

在单独的 class 中初始化我的自定义 logging.properties 并指定所需的系统 属性 解决了问题!

  1. 自定义初始化class

    public class CustomLoggingPropertiesLoader {

    // Initialize the global LogManager
    public CustomLoggingPropertiesLoader() {
        try {
            LogManager.getLogManager().readConfiguration(CustomLoggingPropertiesLoader.class.getClassLoader().getResourceAsStream("logging.properties"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    }

  2. 使用系统属性 ("java.util.logging.config.class")

java -Djava.util.logging.config.class=de.example.logging.CustomLoggingPropertiesLoader -classpath [myclasspath]