Search code examples
log4jdropwizardasyncappender

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


In my DW app I am trying to make the logging to the file and console asynchronous. I found that I can use the AsyncAppender, but is that already configured in DropWizard or do I need to enable it, if so how do I configure the logger to use the AsyncAppender

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

My config.yaml is looks like this

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  

I am using DropWizard 1.0.5.


Solution

  • The DefaultLoggingFactory in dropwizard is used by default for logging purposes.

    Which as you can see here makes use of the AsyncLoggingEventAppenderFactory using the AsyncAppenderBase to build the appenders. The documentation of ch.qos.logback.core.AsyncAppenderBase states that:

    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.

    Now to your question, but is that already configured in DropWizard or do I need to enable it?

    I would say, you don't need to enable it explicitly to configure async appending of the logs. The AsyncLoggingEventAppenderFactory shall take care of it.