【问题标题】:Flogger [google] how to config log format and log fileFlogger [google] 如何配置日志格式和日志文件
【发布时间】:2019-06-08 04:10:45
【问题描述】:

我想将异常记录到日志文件而不是控制台。 如何配置 Flogger 日志记录。

package renameform;
import com.google.common.flogger.FluentLogger;

public class TestFlogger {
    private static final FluentLogger logger = FluentLogger.forEnclosingClass();
    public static void main(String[] args) {
        try {
            int a=2/0;
        }catch(Exception exception)
        {
            logger.atInfo().withCause(exception).log("Log message with: %s", 200);
        }   

    }

}

输出:

May 30, 2019 2:16:00 PM renameform.TestFlogger main
INFO: Log message with: 200
java.lang.ArithmeticException: / by zero
    at renameform.TestFlogger.main(TestFlogger.java:8)

需要有关将日志记录到文本文件以及如何格式化的信息。

【问题讨论】:

    标签: java logging


    【解决方案1】:

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

    您可以参考Java Logging - how to redirect output to a custom log file for a logger? 作为配置文件处理程序的示例。配置文件在Java Logging Overview中解释。

    【讨论】:

      猜你喜欢
      • 2022-11-15
      • 2019-08-12
      • 2011-05-15
      • 1970-01-01
      • 2010-09-13
      • 2016-06-30
      • 2016-10-24
      • 2020-08-11
      • 1970-01-01
      相关资源
      最近更新 更多