spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.56k stars 40.55k forks source link

When using Logback, log messages appear in a single line when application name contains brackets #39564

Closed moonlight200 closed 6 months ago

moonlight200 commented 7 months ago

I recently upgraded to Spring Boot 3.2.2. After the update, all log messages appeared in one single line:


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.2.2)

2024-02-14T17:04:13.529+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:13.532+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:13.574+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:13.574+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.022+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.036+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.398+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.408+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.408+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.438+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.439+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.461+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.601+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.602+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.614+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.725+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.757+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.779+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.940+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:14.970+01:00  WARN 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:15.188+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:15.192+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:15.241+01:00  WARN 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:15.515+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:15.549+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:15.556+01:00  INFO 849908 --- [Test Name (dev] [  restartedMain]2024-02-14T17:04:16.024+01:00  INFO 849908 --- [Test Name (dev] [ionShutdownHook]2024-02-14T17:04:16.032+01:00  WARN 849908 --- [Test Name (dev] [ionShutdownHook]2024-02-14T17:04:16.033+01:00  INFO 849908 --- [Test Name (dev] [ionShutdownHook]2024-02-14T17:04:16.035+01:00  INFO 849908 --- [Test Name (dev] [ionShutdownHook]

The 3.2 Release Notes state, that the application name is now included in the log messages. I had my application name setup with a ... (dev) at the end which was the cause for this issue.

Observed Behavior

On a new spring boot 3.2.2 project (I used Kotlin for both, the source code and Gradle config):

Expected behavior

Regardless of the appearance of any of ([{}]) in the application name, the log output is formatted correctly

wilkinsona commented 7 months ago

Thanks for reporting this, @moonlight200. As far as I can tell, Logback does not support escaping of the characters that have special meaning to its support for variable substitution. This means that we'd have to replace the problematic characters ({, }, (, or )) with something else or omit them entirely. I think we'll need to discuss this one as a team to see if we can agree on something. If you have any suggestions based on your experience please do share them.

philwebb commented 7 months ago

I wonder if we could put the application name in a logback variable then use property{APPLICATION_NAME} in the console pattern.

moonlight200 commented 7 months ago

Thank you for the quick replies. From what I could see in the spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback resources the application name is inserted into the format string of the log message. Maybe there is a way to only place a reference in the format string pass the name as an input instead. It looks like the suggestion from @philwebb is about just that. I am not sure if or how this is doable, as I don't have much experience with the internals of logback or spring boot.

philwebb commented 7 months ago

@moonlight200 Out of interest, does setting logging.include-application-name to false "fix" the issue?

philwebb commented 7 months ago

We're considering failing fast with an exception then giving users the choice of either changing their application name or disabling the feature.

moonlight200 commented 7 months ago

With this config:

spring.application.name=Test Name (dev)
logging.include-application-name=false

Logging works fine:

2024-02-15T17:20:29.487+01:00  INFO 1302973 --- [  restartedMain] c.s.s.SpringLoggingTestApplicationKt     : No active profile set, falling back to 1 default profile: "default"
wilkinsona commented 6 months ago

The problem doesn't occur with Log4j2. With that in mind, this branch contains a fix that's Logback-specific.

The fix uses %property to retrieve LOGGED_APPLICATION_NAME. Unfortunately, %property doesn't support default values so it will evaluate to null when LOGGED_APPLICATION_NAME hasn't been set. To overcome this, %replace is used to turn null into an empty string. This works, but I am a little wary of it as it's implemented using a regular expression which may have some performance implications.

An alternative to this would be to use a custom converter for the logged application name, similar to what we're already doing for the correlation ID.

philwebb commented 6 months ago

We're going to look at a custom converter because we're worried about regex performance.