Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update spring boot logback config properties #276

Open
tayweekiam opened this issue Sep 5, 2024 · 1 comment · May be fixed by #297
Open

Update spring boot logback config properties #276

tayweekiam opened this issue Sep 5, 2024 · 1 comment · May be fixed by #297
Labels
agent-java community Issues and PRs created by the community

Comments

@tayweekiam
Copy link

tayweekiam commented Sep 5, 2024

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?

@github-actions github-actions bot added agent-java community Issues and PRs created by the community triage Issues and PRs that need to be triaged labels Sep 5, 2024
@JonasKunz
Copy link
Contributor

Thanks for reporting this. Looks like indeed the property names have changed in recent spring versions. #297 adds backwards-compatible support for the new logging.logback.rollingpolicy.* options

@JonasKunz JonasKunz removed the triage Issues and PRs that need to be triaged label Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-java community Issues and PRs created by the community
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants