java - 如何更改 Google Flogger(Java 流畅日志记录 API)的日期格式?

标签 java logging lombok java.util.logging

我正在尝试 Lombok @Flogger 功能,该功能应该在使用 @Flogger 注释类时添加以下内容。

private static final com.google.common.flogger.FluentLogger log = com.google.common.flogger.FluentLogger.forEnclosingClass();

请参阅有关它的小文档。 https://projectlombok.org/features/log

我将以下日志添加到方法中:

log.atInfo().log("This is my info message");

我在控制台中看到的输出是:

Mar 21, 2019 7:35:05 PM net.domain.Class myMethod
INFO: This is my info message

我更喜欢 24 小时时间格式的“YYYY-MM-DD HH:MM:SS.mmm”。有办法配置这个吗?我不必使用 Lombok 注释,它看起来更简单。

此外,我无法找到这篇文章的鞭打者标签。

最佳答案

从输出来看,它看起来像 flogger正在使用 SimpleFormatter从七月开始。格式通过设置system property or define the key in the logging.properties来控制。 。格式化程序参数在SimpleFormatter::format中描述。方法。请记住,文档中的参数相差 1,因此日期参数实际上是 %1

日期格式的语法在 java.util.Formatter 中描述。 。

这里是sample test program可用于确保您的模式在运行时应用时正确编译。一种可行的模式是:%1$tF %1$tT.%1$tL %2$s%n%4$s: %5$s%6$s%n

import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;

public class Main {
    public static void main(String[] args) throws Exception {
        final String format = "%1$tF %1$tT.%1$tL %2$s%n%4$s: %5$s%6$s%n";
        final String key = "java.util.logging.SimpleFormatter.format";
        test(format);
        test(System.getProperty(key, format));
        test(LogManager.getLogManager().getProperty(key));
    }

    private static void test(String format) {
        if (format != null) {
            LogRecord record = new LogRecord(Level.INFO, "");
            System.out.println(String.format(format,
                             new java.util.Date(record.getMillis()),
                             record.getSourceClassName(),
                             record.getLoggerName(),
                             record.getLevel().getLocalizedName(),
                             record.getMessage(),
                             String.valueOf(record.getThrown())));
        } else {
            System.out.println("null format");
        }
    }
}

打印内容:

2019-03-21 21:51:08.604 null
INFO: null

关于java - 如何更改 Google Flogger(Java 流畅日志记录 API)的日期格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55291331/

相关文章:

java - 将类对象注入(inject)另一个类中,而不将其添加到构造函数中

java - 如何将插件(从现有 jar 创建)转换为 Maven

ruby-on-rails - 在 Heroku Scheduler 上以分离的方式运行 Rails Rake 任务以捕获 Papertrail 中的日志输出

java - 如何格式化此字符串 "2015-06-22T09:40:30+01:00"in dd/MM/yyyy at hh :mm format?

java - 从JACKSON 1.9->2.1升级输出大量调试日志

生成解析器的 Java 日志框架?

java - 如何让gradle javadoc plugin honor lombok处理?

java - Lombok + Jackson => MismatchedInputException

java - 如何根据 ArrayList 包含的元素的属性返回 ArrayList 中的索引

java - onKeyDown 堆栈溢出错误 Android