gpt4 book ai didi

java - Dropwizard 自定义记录器不会将日志转储到文件中

转载 作者:行者123 更新时间:2023-12-01 10:09:59 24 4
gpt4 key购买 nike

我需要为我的应用程序设置最大文件大小,但目前我使用的是 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,并随着日志事件的生成而不断增加。

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

最佳答案

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

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。

希望有帮助,

阿图尔

关于java - Dropwizard 自定义记录器不会将日志转储到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36184286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com