Dropwizard 中文件和控制台的默认日志记录是否使用 AsyncAppender?

Is the default logging to File and Console in Dropwizard use AsyncAppender?

在我的 DW 应用程序中,我试图使日志记录到文件和控制台异步。我发现我可以使用 AsyncAppender,但是它是否已经在 DropWizard 中配置或者我是否需要启用它,如果是这样我如何配置记录器以使用 AsyncAppender

Logger root = (Logger)   LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
AsyncAppender fileAppender = (AsyncAppender) root.getAppender("async-file-appender");

我的 config.yaml 是这样的

server:
  minThreads: 512
  type: default
supportedCarParcFile: /opt/foo/my_app/config/my-app.json

logging:  
  appenders:  
    -  
      threshold: INFO  
      type: console  
    -  
      archivedFileCount: 7  
      archivedLogFilenamePattern: /opt/foo/my_app/logs/my-app-%d.log.gz  
      currentLogFilename: /opt/foo/my_app/logs/my-app.log  
      threshold: INFO  
      timeZone: CST  
      type: file  
    -  
      archivedFileCount: 7  
      archivedLogFilenamePattern: /opt/foo/my_app/logs/my-app_error-%d.log.gz  
      currentLogFilename: /opt/foo/my_app/logs/my-app_error.log  
      threshold: ERROR  
      timeZone: CST  
      type: file  
  loggers:  
    metrics:  
      additive: true  
      appenders:  
        -  
          archivedFileCount: 10  
          archivedLogFilenamePattern: /opt/foo/my_app/logs/metrics-%d.log.gz  
          currentLogFilename: /opt/foo/my_app/logs/metrics.log  
          type: file  
      level: INFO  

我正在使用 DropWizard 1.0.5

默认情况下使用 dropwizard 中的 DefaultLoggingFactory 进行日志记录。

作为you can see here makes use of the AsyncLoggingEventAppenderFactory using the AsyncAppenderBase 构建附加程序。 ch.qos.logback.core.AsyncAppenderBase 的文档指出:

This appender and derived classes, log events asynchronously. In order to avoid loss of logging events, this appender should be closed. It is the user's responsibility to close appenders, typically at the end of the application lifecycle.

This appender buffers events in a BlockingQueue. Worker thread created by this appender takes events from the head of the queue, and dispatches them to the single appender attached to this appender.

现在回答你的问题,但它是否已在 DropWizard 中配置或我是否需要启用它

我会说,您不需要显式启用它来配置日志的异步附加。 AsyncLoggingEventAppenderFactory 会处理的。