【问题标题】:Dropwizard custom logger does not dump logs into the fileDropwizard 自定义记录器不会将日志转储到文件中
【发布时间】:2016-03-23 16:57:23
【问题描述】:

我需要为我的应用程序设置最大文件大小,但目前我使用的是 Dropwizard 核心版本 0.8.4,它的文件附加程序不支持此功能。

所以我通过编写一个自定义附加程序来像下面这样接近,因为现在更新到最新的 dropwizard(支持我的需要)版本不是一个选项。

   private void initLogging(Configuration configuration) throws JoranException {
    final File logDir = new File("/tmp/enforcer");
    final File logFile = new File(logDir, "wallet.log");

    final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
    rollingFileAppender.setFile(logFile.getAbsolutePath());
    rollingFileAppender.setName("com.documents4j.logger.server.file");
    rollingFileAppender.setContext(loggerContext);

    FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy();
    fixedWindowRollingPolicy.setFileNamePattern(logFile.getAbsolutePath() +"%i.gz");
    fixedWindowRollingPolicy.setMaxIndex(7);
    fixedWindowRollingPolicy.setContext(loggerContext);
    fixedWindowRollingPolicy.setParent(rollingFileAppender);
    fixedWindowRollingPolicy.start();

    SizeBasedTriggeringPolicy<ILoggingEvent> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
    sizeBasedTriggeringPolicy.setMaxFileSize("2KB");
    sizeBasedTriggeringPolicy.setContext(loggerContext);
    sizeBasedTriggeringPolicy.start();



    rollingFileAppender.setRollingPolicy(fixedWindowRollingPolicy);
    rollingFileAppender.setTriggeringPolicy(sizeBasedTriggeringPolicy);

    rollingFileAppender.start();

    System.out.println("Logging: The log is written to " + logFile);
    final ch.qos.logback.classic.Logger log = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
    log.setLevel(Level.DEBUG);
    log.addAppender(rollingFileAppender);
}


 @Override
public void run(Configuration configuration, Environment environment) throws Exception
{

   initLogging(configuration);
}

我的 yaml 文件配置是

logging:
  level: INFO
  org.springframework.retry.support.RetryTemplate: DEBUG
  appenders:
  - type: file
  currentLogFilename: /tmp/enforcer.log
  threshold: ALL
  archive: true
  archivedLogFilenamePattern: /tmp/enforcer-%d.log
  archivedFileCount: 5
  timeZone: UTC
  logFormat: '%-5level [%date{yyyy-MM-dd HH:mm:ss,SSS}] [%X{realdocRequestId}] %logger{15}: %m%n'

现在,当我运行我的应用程序时,我注意到,即使在特定目录中创建了自定义日志文件 (/tmp/enforcer/wallet.log),但实际日志并未转储,即 wallet.log 文件大小为 0 kb,其中yaml中配置的日志文件已创建,大小为一定kb,并随着日志事件的产生而不断增加。

我无法弄清楚我做错了什么,我们将不胜感激。

【问题讨论】:

  • 所有 dropwizard 版本都支持自定义记录器。你只需要创建它们并将它们告诉 DW。我在这里演示了如何做到这一点:stackoverflow.com/questions/27483442/… 您不需要覆盖工厂。您只需要创建一个新的 Logger impl,对其进行正确注释并将其添加到 META-INF 位置,以便 DW 可以找到并初始化它。
  • @pandaadb 感谢您的回答,这有帮助,但我想知道为什么我的代码会失败??我在这里缺少什么??

标签: java logging dropwizard


【解决方案1】:

您的记录器不会记录任何内容,因为您从未为其设置编码器。在以下位置设置断点:

OutputStreamAppender#start() 你会看到没有编码器。

添加后即可使用:

        LayoutWrappingEncoder<ILoggingEvent> layoutEncoder = new LayoutWrappingEncoder<>();
        DropwizardLayout formatter = new DropwizardLayout(loggerContext, TimeZone.getDefault());
        formatter.setPattern("[%level] [%h] %d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z', UTC} [%thread] [%logger] %msg%n");
        layoutEncoder.setLayout(formatter);
        formatter.start();
        rollingFileAppender.setEncoder(layoutEncoder);

当然,您可以定义您喜欢的任何格式(和格式化程序)。

但请记住,这是您尝试实现的目标的一个相当老套的示例。您现在在代码中有 2 个点可以配置日志记录(DW 和您自己的)。您已经配置了 yaml 以及您自己的日志记录。我建议投入一些工作并添加一个您可以使用的合适的 AppenderFactory。

希望对你有帮助,

阿图尔

【讨论】:

    猜你喜欢
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2017-07-10
    • 2015-12-15
    • 2021-07-09
    • 2022-06-16
    • 2014-02-21
    相关资源
    最近更新 更多