在我的 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。
最佳答案
DefaultLoggingFactory
dropwizard 中默认用于日志记录目的。
如 you can see here利用AsyncLoggingEventAppenderFactory
使用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
将负责处理它。
关于log4j - Dropwizard 中默认记录到文件和控制台是否使用 AsyncAppender?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45945693/