简体   繁体   English

Spring Boot logback.xml 创建 .tmp 文件

[英]Spring Boot logback.xml creating .tmp Files

I am trying to persist the logs of a Spring Boot Application, however, since the logs generated are large I am trying to use the logback.xml to roll the file greater than 350MB into a compressed file.我试图保留 Spring Boot 应用程序的日志,但是,由于生成的日志很大,我试图使用 logback.xml 将大于 350MB 的文件滚动到压缩文件中。

I am able to roll a couple of MB's per day but midway the service starts writing to a temp file.我每天可以滚动几个 MB,但在中途服务开始写入临时文件。 I have tried both "TimeBasedRollingPolicy" and "Size AndTimeBasedRollingPolicy" with Triggering Policy of "SizeAndTimeBasedFNATP" but the results are unchanged.我已经尝试了“TimeBasedRollingPolicy”和“Size AndTimeBasedRollingPolicy”以及“SizeAndTimeBasedFNATP”的触发策略,但结果没有改变。 The .tmp files are generated every time. .tmp 文件每次都会生成。

My Logback.xml looks like this:我的 Logback.xml 看起来像这样:

 <?xml version="1.0" encoding="UTF-8"?>

<configuration>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>/home/xyz/logs/ProdLog.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>/home/xyz/logs/log_%d{yyyy-MM-dd}_%i.log.zip</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 350MB -->
                <maxFileSize>350MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>5</maxHistory>
            <!--<maxFileSize>350MB</maxFileSize>-->
        </rollingPolicy>
        <encoder>
            <pattern>%date [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
        %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
        </Pattern>
        </layout>
    </appender>
    <root level="INFO">
        <appender-ref ref="FILE"/>
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

在此处输入图片说明

I see that the ticket for logback .tmp file issue is marked closed on Jira.我看到 logback .tmp 文件问题的票证在 Jira 上标记为已关闭。 Could someone help with what needs to be modified here to avoid generating the temp files?有人可以帮忙解决这里需要修改的内容以避免生成临时文件吗?

I´m having the same issue using logback 1.2.3, apparently the bug is fixed in 1.3.0 version but I´ve found the lines of code in charge of generating those tmp files and managed to avoid it.我在使用 logback 1.2.3 时遇到了同样的问题,显然该错误已在 1.3.0 版本中修复,但我找到了负责生成这些 tmp 文件的代码行并设法避免了它。

This is code from TimeBasedRollingPolicy.java:这是来自 TimeBasedRollingPolicy.java 的代码:

public void rollover() throws RolloverFailure {

    // when rollover is called the elapsed period's file has
    // been already closed. This is a working assumption of this method.

    String elapsedPeriodsFileName = timeBasedFileNamingAndTriggeringPolicy.getElapsedPeriodsFileName();

    String elapsedPeriodStem = FileFilterUtil.afterLastSlash(elapsedPeriodsFileName);

    if (compressionMode == CompressionMode.NONE) {
        if (getParentsRawFileProperty() != null) {
            renameUtil.rename(getParentsRawFileProperty(), elapsedPeriodsFileName);
        } // else { nothing to do if CompressionMode == NONE and parentsRawFileProperty == null }
    } else {
        if (getParentsRawFileProperty() == null) {
            compressionFuture = compressor.asyncCompress(elapsedPeriodsFileName, elapsedPeriodsFileName, elapsedPeriodStem);
        } else {
            compressionFuture = renameRawAndAsyncCompress(elapsedPeriodsFileName, elapsedPeriodStem);
        }
    }

    if (archiveRemover != null) {
        Date now = new Date(timeBasedFileNamingAndTriggeringPolicy.getCurrentTime());
        this.cleanUpFuture = archiveRemover.cleanAsynchronously(now);
    }
}

Future<?> renameRawAndAsyncCompress(String nameOfCompressedFile, String innerEntryName) throws RolloverFailure {
    String parentsRawFile = getParentsRawFileProperty();
    String tmpTarget = nameOfCompressedFile + System.nanoTime() + ".tmp";
    renameUtil.rename(parentsRawFile, tmpTarget);
    return compressor.asyncCompress(tmpTarget, nameOfCompressedFile, innerEntryName);
}

As you can see here, if you set a "fileName" into the appender, that method is called and generates a tmp file (which is not bad).正如你在这里看到的,如果你在 appender 中设置了一个“fileName”,这个方法就会被调用并生成一个 tmp 文件(这还不错)。 I think the problem comes when there are threads that don´t stop logging on that tmp file, so at the end there are threads writing on tmp file and others writing on new "fileName" file.我认为问题出现在有线程不停止登录该 tmp 文件时,所以最后有线程在 tmp 文件上写入,而其他线程在新的“fileName”文件上写入。

If you just set fileNamePattern tag and not the file tag, then no tmp files should be generated.如果您只是设置 fileNamePattern 标签而不是文件标签,则不应生成 tmp 文件。

I hope this helps you!我希望这可以帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring boot logback.xml 外化 - Spring boot logback.xml externalisation spring-boot logback.xml属性,具体取决于配置文件 - spring-boot logback.xml property depending on profile 使用Apache CXF的Spring Boot不使用logback.xml - Spring boot with apache cxf not using logback.xml spring 中的 logback.xml 启动不打印调试消息 - logback.xml in spring boot not printing debug messages Spring Boot:如何根据当前环境或spring配置文件使用自定义logback.xml - Spring Boot: How to use custom logback.xml depending on current environment or spring profile 如何将 spring 引导应用程序指向外部 jar 的 logback.xml - how to point spring boot application to external jar's logback.xml 如何在 Spring Boot 中将父项目的 gradle 属性传递给 logback.xml? - How to pass gradle property of parent project to logback.xml in Spring Boot? 如何在 spring 启动应用程序中通过 logback.xml 为两个附加程序设置两个不同的日志级别? - How to set up two different log levels for two appenders via logback.xml in spring boot app? 在不使用 logback.xml 文件的情况下,在 spring boot 中使用 MDC 或任何过滤器屏蔽日志消息中的密码 - Mask the passwords in log messages using MDC or Any Filters in spring boot without using logback.xml file 使用application.properties将Spring Boot中logback.xml的位置外部化 - Externalized the location of logback.xml in Spring Boot using application.properties
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM