Dropwizard 自定义记录器不会将日志转储到文件中
Dropwizard custom logger does not dump logs into the file
我需要为我的应用程序设置最大文件大小,但目前我使用的是 Dropwizard 核心版本 0.8.4,它的文件附加程序不支持此功能。
所以我通过编写自定义 appender 作为更新到最新的 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,并随着日志事件的生成而继续增加。
我无法弄清楚我做错了什么,将不胜感激。
您的记录器不会记录任何内容,因为您从未为其设置编码器。在以下位置设置断点:
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。
希望对您有所帮助,
亚瑟
我需要为我的应用程序设置最大文件大小,但目前我使用的是 Dropwizard 核心版本 0.8.4,它的文件附加程序不支持此功能。
所以我通过编写自定义 appender 作为更新到最新的 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,并随着日志事件的生成而继续增加。
我无法弄清楚我做错了什么,将不胜感激。
您的记录器不会记录任何内容,因为您从未为其设置编码器。在以下位置设置断点:
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。
希望对您有所帮助,
亚瑟