gpt4 book ai didi

java - 如何在 logback 中滚动应用程序关闭时的日志文件?

转载 作者:行者123 更新时间:2023-12-01 18:40:07 34 4
gpt4 key购买 nike

我有一个市场数据 Spring Boot 应用程序,每天运行并在晚上自行关闭。应用程序关闭后,我想处理记录的市场数据消息。为了做到这一点,我正在寻找一种能够在关闭而不是启动时滚动日志文件的方法,以便我的后处理应用程序能够以一致的方式读取文件。

目前,我已经创建了一个自定义触发策略,并使用以下 logback 配置来发送与市场数据相关的消息

@NoAutoStart
public class CustomTriggerPolicy<E> extends SizeAndTimeBasedFNATP<E> {
@Override
public boolean isTriggeringEvent(File activeFile, E event) {
boolean triggeringEvent = super.isTriggeringEvent(activeFile, event);
if(!triggeringEvent) {
elapsedPeriodsFileName = getCurrentPeriodsFileNameWithoutCompressionSuffix();
}
return triggeringEvent;
}
}

logback.xml

<configuration>
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/>
<shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook">
<delay>60000</delay>
</shutdownHook>
<appender name="MKT-DATA-FIX-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/mkt-data-fix.log</file>
<append>false</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%m%n</Pattern>
</encoder>

<!-- rollover daily -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/archived/foo/mkt-data-fix.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="com.mkt.data.CustomTriggerPolicy">
<maxFileSize>100GB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
</configuration>

SpringBootApplication.class(主类)

@PreDestroy
public void shutdown() {
manualLogRollover();
}

public void manualLogRollover() {
try {
Set<Appender<ILoggingEvent>> loggingAppenders = AppenderUtils.getLoggingAppenders();
AppenderUtils.rolloverAppenders(loggingAppenders);
} catch (Exception e) {
log.warn("Log rollover failed", e);
}
}

AppenderUtils.java

public class AppenderUtils {
public static Set<Appender<ILoggingEvent>> getLoggingAppenders() {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
Set<Appender<ILoggingEvent>> fileLogAppenders = new HashSet<>();
for(Logger logger : context.getLoggerList()) {
for(Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders(); index.hasNext();) {
Appender<ILoggingEvent> appender = index.next();
if(appender instanceof RollingFileAppender) {
if(appender.getName().equals("MKT-DATA-FIX-LOG") ||
appender.getName().equals("MKT-DATA-FIX-EVENT-LOG")) {
fileLogAppenders.add(appender);
}
}
}
}
return fileLogAppenders;
}

public static void rolloverAppenders(Set<Appender<ILoggingEvent>> appenders) {
for (Appender<ILoggingEvent> appender : appenders) {
if (appender instanceof RollingFileAppender) {
((RollingFileAppender<ILoggingEvent>) appender).rollover();
}
}
}
}

到目前为止我已经做了以下事情;

  1. 尝试在关闭时手动翻转而不使用 DelayingShutdownHook,这最终会创建一个 .tmp 文件并且无法干净地完成翻转过程。
  2. 添加一个延迟 1 分钟的延迟关闭钩子(Hook),但似乎 logback Compressor 的 asyncCompress 方法返回一个 Future,该 Future 在 30 秒后超时,因此我收到以下错误
18:16:58,872 |-ERROR in c.q.l.core.rolling.TimeBasedRollingPolicy@398887205 - Timeout while waiting for compression job to finish java.util.concurrent.TimeoutException
at java.util.concurrent.TimeoutException
at at java.util.concurrent.FutureTask.get(FutureTask.java:205)
at at ch.qos.logback.core.rolling.TimeBasedRollingPolicy.waitForAsynchronousJobToStop(TimeBasedRollingPolicy.java:137)
at at ch.qos.logback.core.rolling.TimeBasedRollingPolicy.stop(TimeBasedRollingPolicy.java:129)
at at ch.qos.logback.core.rolling.RollingFileAppender.stop(RollingFileAppender.java:152)
at at ch.qos.logback.core.spi.AppenderAttachableImpl.detachAndStopAllAppenders(AppenderAttachableImpl.java:107)
at at ch.qos.logback.classic.Logger.detachAndStopAllAppenders(Logger.java:206)
at at ch.qos.logback.classic.Logger.recursiveReset(Logger.java:331)iam
at at ch.qos.logback.classic.Logger.recursiveReset(Logger.java:338)
at at ch.qos.logback.classic.Logger.recursiveReset(Logger.java:338)
at at ch.qos.logback.classic.Logger.recursiveReset(Logger.java:338)
at at ch.qos.logback.classic.Logger.recursiveReset(Logger.java:338)
at at ch.qos.logback.classic.LoggerContext.reset(LoggerContext.java:223)
at at ch.qos.logback.classic.LoggerContext.stop(LoggerContext.java:348)
at at ch.qos.logback.core.hook.ShutdownHookBase.stop(ShutdownHookBase.java:39)
at at ch.qos.logback.core.hook.DelayingShutdownHook.run(DelayingShutdownHook.java:57)
at at java.lang.Thread.run(Thread.java:748)

我别无选择。有谁知道如何实现我在这里寻找的东西?

最佳答案

我想我已经明白了。我必须定义自己的触发和滚动策略来滚动附加程序并同步压缩文件。

我不需要在我的主类中将翻转作为 @PreDestroy 的一部分。

@NoAutoStart
public class CustomTriggerPolicy<E> extends SizeAndTimeBasedFNATP<E> {
@Override
public boolean isTriggeringEvent(File activeFile, E event) {
boolean triggeringEvent = super.isTriggeringEvent(activeFile, event);
if(!triggeringEvent) {
elapsedPeriodsFileName = getCurrentPeriodsFileNameWithoutCompressionSuffix();
}
return triggeringEvent;
}
}
import ch.qos.logback.core.rolling.RolloverFailure;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import ch.qos.logback.core.rolling.helper.*;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
* Custom rolling policy where we trigger rollback at shutdown.
*/
public class MktDataRollingPolicy<E> extends TimeBasedRollingPolicy<E> {

private static final String LOGBACK_STATUS_FILE_NAME = "logback-status.log";
private Compressor compressor;
private RenameUtil renameUtil = new RenameUtil();
private String basePath;
private String logbackStatusFilePath;

FileNamePattern zipEntryFileNamePattern;

@Override
public void start() {
basePath = System.getProperty("logback.statusLog", "/home/master/logs/mkt-data-log");
logbackStatusFilePath = basePath + File.separator + LOGBACK_STATUS_FILE_NAME;
super.start();
// set the LR for our utility object
renameUtil.setContext(this.context);

compressor = new Compressor(compressionMode);
compressor.setContext(context);

if (compressionMode == CompressionMode.ZIP) {
String zipEntryFileNamePatternStr = transformFileNamePattern2ZipEntry(fileNamePatternStr);
zipEntryFileNamePattern = new FileNamePattern(zipEntryFileNamePatternStr, context);
}
Runtime.getRuntime().addShutdownHook(new Thread(new ShutdownHookRunnable()));
}

@Override
public void rollover() throws RolloverFailure {
String elapsedPeriodsFileName = getTimeBasedFileNamingAndTriggeringPolicy().getElapsedPeriodsFileName();
if (elapsedPeriodsFileName != null) {
if (compressionMode == CompressionMode.NONE) {
if (getParentsRawFileProperty() != null) {
renameUtil.rename(getParentsRawFileProperty(), elapsedPeriodsFileName);
} // else { nothing to do if CompressionMode == NONE and parentsRawFileProperty == null }
} else {
String elapsedPeriodStem = FileFilterUtil.afterLastSlash(elapsedPeriodsFileName);
if (getParentsRawFileProperty() == null) {
compressor.compress(elapsedPeriodsFileName, elapsedPeriodsFileName, elapsedPeriodStem);
} else {
if (isLogFileEmpty()) {
addInfo("Skipping compression for empty file: " + getParentsRawFileProperty());
return;
}
renameRawAndCompress(elapsedPeriodsFileName, elapsedPeriodStem);
}
}
} else {
String msg = String.format("ElapsedPeriodsFileName is null. Skipping rollover for file: %s",
getParentsRawFileProperty());
addWarn(msg);
File statusFile = new File(logbackStatusFilePath);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(statusFile, true))) {
bw.append(msg);
bw.append("\n");
} catch (IOException e) {
System.out.println("Failed to write to status file: " + logbackStatusFilePath);
e.printStackTrace();
}
}
}


private boolean isLogFileEmpty() {
File f = new File(getParentsRawFileProperty());
return f.exists() && f.length() == 0;
}

private String transformFileNamePattern2ZipEntry(String fileNamePatternStr) {
String slashified = FileFilterUtil.slashify(fileNamePatternStr);
return FileFilterUtil.afterLastSlash(slashified);
}

void renameRawAndCompress(String nameOfCompressedFile, String innerEntryName) throws RolloverFailure {
String parentsRawFile = getParentsRawFileProperty();
String tmpTarget = nameOfCompressedFile + System.nanoTime() + ".tmp";
renameUtil.rename(parentsRawFile, tmpTarget);
compressor.compress(tmpTarget, nameOfCompressedFile, innerEntryName);
}

class ShutdownHookRunnable implements Runnable {
@Override
public void run() {
try {
addInfo("Calling rollover inside shutdown hook");
rollover();
} catch (Exception e) {
addError("Failed to rollover", e);
}
}
}
}

logback.xml

<configuration>
<appender name="MKT-DATA-FIX-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/mkt-data-fix.log</file>
<append>false</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%m%n</Pattern>
</encoder>

<!-- rollover daily -->
<rollingPolicy class="com.myapp.logback.MktDataRollingPolicy">
<fileNamePattern>${LOG_HOME}/archived/foo/mkt-data-fix.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="com.mkt.data.CustomTriggerPolicy">
<maxFileSize>100GB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
</configuration>

关于java - 如何在 logback 中滚动应用程序关闭时的日志文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59951149/

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