spring-projects / spring-projects/spring-boot

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

Open
#16,231 18 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: pending-design-work type: enhancement
Dominant language
Java
Stars
81.5k
Forks
42.7k
Avg merge
2d 4h
Merged PRs (30d)
65

Description

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.

https://github.com/spring-projects/spring-boot/blob/c115ee1550ecf4bc8164e59a4f69b59addd50cc6/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java#L105-L108

https://github.com/spring-projects/spring-boot/blob/c115ee1550ecf4bc8164e59a4f69b59addd50cc6/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java#L117-L122

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).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java, especially the linked handling of logging.path and logging.file. Review the supplied logback-base.xml and logback-spring.xml examples to understand the naming requirement. Done means logging.path can be shared while each service writes to its own predictable log file without an explicitly configured logging.file.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
observability
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.