ecs-logging-java icon indicating copy to clipboard operation
ecs-logging-java copied to clipboard

Update spring boot logback config properties

Open tayweekiam opened this issue 5 months ago • 0 comments

Software Details

Issue

I was trying to modify the rolling policy of ecs-file-appender.xml via application.properties after including it in my logback config file.

logback-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <include resource="co/elastic/logging/logback/boot/ecs-file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ECS_JSON_FILE"/>
    </root>
</configuration>

Following the spring boot appendix, I used the following properties in my application.properties file.

application.properties:

...

logging.logback.rollingpolicy.max-file-size=5MB
logging.logback.rollingpolicy.max-history=30

However, the properties were not applied.

I tried the following properties as well and the result was the same.

application.properties:

...

logging.file.max-size=5MB
logging.file.max-history=30

Resolution

After looking at the file-appender.xml from Spring Boot, I realise the property names were different and updated my logback config to use the new property names.

logback-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

    <!-- Use custom config instead of including from ecs-file-appender.xml -->
    <springProperty name="SERVICE_NAME" source="spring.application.name"/>
    <appender name="ECS_JSON_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>${FILE_LOG_THRESHOLD}</level>
        </filter>
        <encoder class="co.elastic.logging.logback.EcsEncoder">
            <serviceName>${SERVICE_NAME:-spring-boot-application}</serviceName>
        </encoder>
        <file>${LOG_FILE}.json</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOGBACK_ROLLINGPOLICY_FILE_NAME_PATTERN:-${LOG_FILE}.json.%d{yyyy-MM-dd}.%i.gz}</fileNamePattern>
            <cleanHistoryOnStart>${LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
            <maxFileSize>${LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE:-10MB}</maxFileSize>
            <maxHistory>${LOGBACK_ROLLINGPOLICY_MAX_HISTORY:-7}</maxHistory>
            <totalSizeCap>${LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP:-0}</totalSizeCap>
        </rollingPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="ECS_JSON_FILE"/>
    </root>
</configuration>

Using the updated config and the following application.properties:

...

logging.logback.rollingpolicy.max-file-size=5MB
logging.logback.rollingpolicy.max-history=30

The configuration was successful.

Did I make a mistake initially or are the property names in ecs-file-appender.xml not compatible with my Spring Boot version?

tayweekiam avatar Sep 05 '24 08:09 tayweekiam