javaspring-bootlogbackspring-boot-actuatorrollingfileappender

Is there a way to use the default Springboot "/actuator/logfile" endpoint when using Logback's RollingFileAppender?


The Springboot REST server I'm working on is logging files using Logback's RollingFileAppender and SizeAndTimeBasedRollingPolicy.

I'd like the "logfile" endpoint of spring's actuator to return the log from the most recent file, however the file names change based on the given file name pattern.

Is there a way for the logfile actuator to access a logfile, other than by using the file or path given in application.properties?


Solution

  • In your logback configuration in section "FILE" appender configuration, you have to had something like that:

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">   
        <file>logs/logfile.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>logs/logfile.%d{yyyy-MM-dd}_%i.log</fileNamePattern>
    
            <!-- keep 30 days' worth of history capped at 2GB total size -->
            <maxHistory>30</maxHistory>
            <maxFileSize>1GB</maxFileSize>
            <totalSizeCap>2GB</totalSizeCap>
    
        </rollingPolicy>
    </appender>
    

    Your recent log file will logfile.log, which you could define in app.properties like:

    endpoints.logfile.external-file=logs/logfile.log
    

    and all logfiles wich was rolled back due to size limit will be renamed depends on pattern.