java.util.logging - 如何强制所有记录器遵守应用程序运行时中给定的格式? (HSQL数据库)

标签 java logging formatting hsqldb java.util.logging

在我的应用程序中,我想要某种格式化方式。 为此,我编写了一个 SingleLineFormatter extends Formatter

现在我尝试设置所有Logger来使用它,但无法弄清楚。

        LogManager.getLogManager().readConfiguration((InputStream) configIS);

        Collections.list(LogManager.getLogManager().getLoggerNames()).forEach(
            loggerName -> {
                List<Handler> handlers = Arrays.asList(Logger.getLogger(loggerName).getHandlers());
                System.out.println(" * Logger " + loggerName + ": " + handlers.size());
                handlers.forEach(handler -> System.out.println("   HF: " + handler.getFormatter()));
                //handlers.forEach(handler -> handler.setFormatter(new SingleLineFormatter()));
            }
        );/**/
        log.info("Logging test");

这会读取配置,并将其应用于找到的所有处理程序。

Oct 02, 2018 10:42:10 PM cz.dynawest.logging.LoggingUtils initLogging
INFO: Log config file not found: #/logging.properties  Using LoggingUtils' default.
 * Logger cz.dynawest.csvcruncher.App: 0
 * Logger global: 0
 * Logger cz.dynawest.logging.LoggingUtils: 0
2018-10-02 22:42:10 INFO cz.dynawest.logging.LoggingUtils initLogging:   Logging test
 * Logger : 2
   HF: cz.dynawest.logging.SingleLineFormatter@34c45dca
   HF: cz.dynawest.logging.SingleLineFormatter@52cc8049

但是,其余的Logger仍然使用它们配置的任何内容。可能只是 JUL 的默认设置。

Oct 02, 2018 10:42:10 PM org.hsqldb.persist.Logger logInfoEvent
INFO: checkpointClose start

我知道我可以通过 JVM 中的 JUL 的 logging.properties 来管理此操作。
但我想分发该应用程序,并希望所有日志消息的格式相同。

如何强制使用我的 Formatter 对发送至 JUL 的所有邮件进行格式化?

-Djava.util.logging.config.file=/path/to/app.properties,但这超出了我的特定情况的范围。 (无论如何都不起作用。)

更新: 第三方日志记录似乎没有经过 7 月。所以问题是 - 如果这是真的,我该如何配置其他框架?

这是正在加载到LogManager 的文件。有相当多的实验,所以并不是所有的都是正确的。

# Handlers
handlers = java.util.logging.ConsoleHandler java.util.logging.FileHandler

# Console
# The logging of the app actually reacts to this line.
java.util.logging.ConsoleHandler.formatter = cz.dynawest.logging.SingleLineFormatter
#java.util.logging.ConsoleHandler.formatter = cz.dynawest.logging.SimplestFormatter
java.util.logging.ConsoleHandler.level = ALL

# File
java.util.logging.FileHandler.level = ALL
java.util.logging.FileHandler.pattern = app.log
java.util.logging.FileHandler.formatter = cz.dynawest.logging.SingleLineFormatter
java.util.logging.FileHandler.limit = 0
java.util.logging.FileHandler.append = true


# Default global logging level.
.formatter = cz.dynawest.logging.SimplestFormatter
.level = INFO

#global.formatter = cz.dynawest.logging.SimplestFormatter
#root.formatter = cz.dynawest.logging.SimplestFormatter
#cz.dynawest.csvcruncher.App.formatter = cz.dynawest.logging.SimplestFormatter
#cz.dynawest.csvcruncher.App.handlers = java.util.logging.ConsoleHandler
#.useParentHandlers = false

# Various customizations.

org.apache.commons.beanutils.converters.level=INFO

最佳答案

In my app, I would like some way of formatting. For that purpose, I wrote a >SingleLineFormatter extends Formatter

自 JDK 7 起 java.util.logging.SimpleFormatter supports a one line format .

This reads the config, applies it to all handlers that are found.

JUL 仅在代码需要记录器时才加载处理程序。因此,由于记录器本身尚未创建,因此可能会错过处理程序。

How can I force all messages going to JUL to be formatted with my Formatter?

There is -Djava.util.logging.config.file=/path/to/app.properties, but that's out of scope for my particular case. (And doesn't work anyway.)

您已经在访问 LogManager,因此可以从 Java 设置属性,如下所示:

 private static void loadProperties() {
    Properties props = new Properties();
    props.put("java.util.logging.ConsoleHandler.formatter", "cz.dynawest.logging.SingleLineFormatter");
    props.put("org.apache.commons.beanutils.converters.level", "INFO");

    try(ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        props.store(out, "");
        LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(out.toByteArray()));
    } catch (IOException ioe) {
        throw new AssertionError(ioe);
    }
}

如果您运行的是 JDK 9 或更高版本,那么您应该使用 LogManager.updateConfiguration(java.util.function.Function)相反。

It looks like the third party logging doesn't go through JUL. So the question is - if that's true, how do I configure the other frameworks?

您必须确定框架是否相互路由。您只需更改将记录发布到输出源的框架的格式。

关于java.util.logging - 如何强制所有记录器遵守应用程序运行时中给定的格式? (HSQL数据库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52616158/

相关文章:

java - 抛出异常的单元测试

java - JFrame 未正确居中

python 瓶 : Can't connect to server or debug it

python - 在 Python 2.6 中使用 logging.SysLogHandler 时如何设置标识字符串?

java - 如何在访问器中动态定义键空间

java - HtmlUnit 的 HtmlPage.getElementById 似乎在多次调用后重新初始化 JavaScript

java - 停止 tomcat 写入 stdout 和 stderr

regex - 使用从正则表达式捕获的字符串来格式化日期时间

block 池的hadoop hdfs格式化出错失败

ruby - 将 JSON 字符串 append 到 Ruby 中已包含 JSON 的文件