gpt4 book ai didi

java - 如何在控制台中为整个项目设置可见的另一级别日志?

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:53 25 4
gpt4 key购买 nike

我有一个小型 JavaFX 应用程序,不想使用 java.util.logging。我已将所需信息记录到 Level.INFO(当前可见)和 Level.FINE(当前不可见)级别。当我尝试通过 Intellij Idea 编译项目时,我在控制台中只看到 Level.INFO 日志。

代码示例。

public class Main extends Application {
private static final Logger logger = Logger.getLogger(Main.class.getName());
@Override
public void start(Stage primaryStage) throws Exception {
logger.log(Level.INFO, "App started. Message's visible");
logger.log(Level.FINE, "Message's not visible");
}
}

如何更改控制台默认可见级别,而无需为 Handler 代码的每个记录器行编写?也许根目录中有一些配置文件?

日志的编写风格为grwww answer here ,但没有提及任何有关更改可见级别的内容。

最佳答案

阅读Java Logging Overview由甲骨文公司提供。

在代码中配置内联记录器

import javafx.application.Application;
import javafx.stage.Stage;

import java.util.logging.*;

public class Main extends Application {
private static final Logger logger = Logger.getLogger(Main.class.getName());

@Override
public void start(Stage primaryStage) throws Exception {
logger.log(Level.INFO, "App started. Message's visible");
logger.log(Level.FINE, "Message's not visible");
}

public static void main(String[] args) {
logger.setLevel(Level.FINE);

Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.FINE);
logger.addHandler(consoleHandler);

launch(args);
}
}

请注意,级别是在记录器和处理程序上配置的。

有关如何执行此操作的信息来自:

使用文件或类配置记录器

请注意,我上面提供的示例适用于基于内联代码的方法。还可以使用日志记录配置类或外部文件(或类路径资源)来定义日志记录配置。为此,请参阅 LogManager文档和之前链接的 Java 日志记录概述。

The LogManager defines two optional system properties that allow control over the initial configuration:

  • "java.util.logging.config.class"
  • "java.util.logging.config.file"

These two properties may be specified on the command line to the "java" command.

If the "java.util.logging.config.class" property is set, then the property value is treated as a class name. The given class will be loaded, an object will be instantiated, and that object's constructor is responsible for reading in the initial configuration. (That object may use other system properties to control its configuration.) The alternate configuration class can use readConfiguration(InputStream) to define properties in the LogManager.

If "java.util.logging.config.class" property is not set, then the "java.util.logging.config.file" system property can be used to specify a properties file (in java.util.Properties format). The initial logging configuration will be read from this file.

If neither of these properties is defined then the LogManager uses its default configuration. The default configuration is typically loaded from the properties file "lib/logging.properties" in the Java installation directory.

关于java - 如何在控制台中为整个项目设置可见的另一级别日志?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36225549/

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