Skip to content

Ability to customize log file name when logging.path is set #16231

@xak2000

Description

@xak2000

With the current implementation, when logging.path is set to some value and logging.file is not set, the logging file name is always spring.log.

public void applyTo(Properties properties) {
put(properties, LoggingSystemProperties.LOG_PATH, this.path);
put(properties, LoggingSystemProperties.LOG_FILE, toString());
}

public String toString() {
if (StringUtils.hasLength(this.file)) {
return this.file;
}
return new File(this.path, "spring.log").getPath();
}

I want to be able to set logging.path for several microservices at once, using environment variable for example:

LOGGING_PATH=/var/log/services

This works, but all services start to write their logs into signle /var/log/services/spring.log file.

To prevent this, I added custom logback-spring.xml to each application and also one logback-base.xml into common module (that all microservices depend on).

I added these lines into logback-base.xml:

<included>
    <springProperty scope="context" name="APPLICATION_NAME" source="spring.application.name"/>
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${java.io.tmpdir:-/tmp}/services}/${APPLICATION_NAME:-service}.log}"/>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
</included>

This is how logback-spring.xml looks in each application:

<configuration>
    <include resource="logback-base.xml"/>
</configuration>

The idea is to store all logs in the same path, but each log file have it's own name, based on spring.application.name.

But this doesn't work when only logging.path is set, because LogFile class always explicitly sets LOG_FILE system property, using it's toString() method and adding spring.log as file name.

Maybe LogFile can be enhanced to allow to set some strategy that provides an ability to customize log file name (when it is not explicitly set)? Some LogFileNameCustomizer bean or something.

Or maybe at least we can make spring.application.name to be default log file name if this property is set and logging.file is not set? (this is breaking change though, but it can be hidden under some flag).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions