Skip to content

Commit 1669268

Browse files
committed
Read PID and version used in startup logging from the environment
See gh-41604
1 parent 7920e37 commit 1669268

File tree

3 files changed

+77
-22
lines changed

3 files changed

+77
-22
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public ConfigurableApplicationContext run(String... args) {
336336
afterRefresh(context, applicationArguments);
337337
startup.started();
338338
if (this.logStartupInfo) {
339-
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), startup);
339+
new StartupInfoLogger(this.mainApplicationClass, environment).logStarted(getApplicationLog(), startup);
340340
}
341341
listeners.started(context, startup.timeTakenToStarted());
342342
callRunners(context, applicationArguments);
@@ -404,6 +404,7 @@ private void prepareContext(DefaultBootstrapContext bootstrapContext, Configurab
404404
bootstrapContext.close(context);
405405
if (this.logStartupInfo) {
406406
logStartupInfo(context.getParent() == null);
407+
logStartupInfo(context);
407408
logStartupProfileInfo(context);
408409
}
409410
// Add boot specific singleton beans
@@ -633,14 +634,27 @@ protected void applyInitializers(ConfigurableApplicationContext context) {
633634
/**
634635
* Called to log startup information, subclasses may override to add additional
635636
* logging.
636-
* @param isRoot true if this application is the root of a context hierarchy
637+
* @param context the application context
638+
* @since 3.4.0
637639
*/
638-
protected void logStartupInfo(boolean isRoot) {
640+
protected void logStartupInfo(ConfigurableApplicationContext context) {
641+
boolean isRoot = context.getParent() == null;
639642
if (isRoot) {
640-
new StartupInfoLogger(this.mainApplicationClass).logStarting(getApplicationLog());
643+
new StartupInfoLogger(this.mainApplicationClass, context.getEnvironment()).logStarting(getApplicationLog());
641644
}
642645
}
643646

647+
/**
648+
* Called to log startup information, subclasses may override to add additional
649+
* logging.
650+
* @param isRoot true if this application is the root of a context hierarchy
651+
* @deprecated since 3.4.0 for removal in 3.6.0 in favor of
652+
* {@link #logStartupInfo(ConfigurableApplicationContext)}
653+
*/
654+
@Deprecated(since = "3.4.0", forRemoval = true)
655+
protected void logStartupInfo(boolean isRoot) {
656+
}
657+
644658
/**
645659
* Called to log active profile information.
646660
* @param context the application context

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,8 +23,8 @@
2323
import org.springframework.aot.AotDetector;
2424
import org.springframework.boot.SpringApplication.Startup;
2525
import org.springframework.boot.system.ApplicationHome;
26-
import org.springframework.boot.system.ApplicationPid;
2726
import org.springframework.context.ApplicationContext;
27+
import org.springframework.core.env.Environment;
2828
import org.springframework.core.log.LogMessage;
2929
import org.springframework.util.Assert;
3030
import org.springframework.util.ClassUtils;
@@ -41,8 +41,11 @@ class StartupInfoLogger {
4141

4242
private final Class<?> sourceClass;
4343

44-
StartupInfoLogger(Class<?> sourceClass) {
44+
private final Environment environment;
45+
46+
StartupInfoLogger(Class<?> sourceClass, Environment environment) {
4547
this.sourceClass = sourceClass;
48+
this.environment = environment;
4649
}
4750

4851
void logStarting(Log applicationLog) {
@@ -62,7 +65,7 @@ private CharSequence getStartingMessage() {
6265
message.append("Starting");
6366
appendAotMode(message);
6467
appendApplicationName(message);
65-
appendVersion(message, this.sourceClass);
68+
appendApplicationVersion(message);
6669
appendJavaVersion(message);
6770
appendPid(message);
6871
appendContext(message);
@@ -106,8 +109,12 @@ private void appendVersion(StringBuilder message, Class<?> source) {
106109
append(message, "v", () -> source.getPackage().getImplementationVersion());
107110
}
108111

112+
private void appendApplicationVersion(StringBuilder message) {
113+
append(message, "v", () -> this.environment.getProperty("spring.application.version"));
114+
}
115+
109116
private void appendPid(StringBuilder message) {
110-
append(message, "with PID ", ApplicationPid::new);
117+
append(message, "with PID ", () -> this.environment.getProperty("spring.application.pid"));
111118
}
112119

113120
private void appendContext(StringBuilder message) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/StartupInfoLoggerTests.java

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,10 +17,11 @@
1717
package org.springframework.boot;
1818

1919
import org.apache.commons.logging.Log;
20+
import org.junit.jupiter.api.BeforeEach;
2021
import org.junit.jupiter.api.Test;
2122

2223
import org.springframework.boot.SpringApplication.Startup;
23-
import org.springframework.boot.system.ApplicationPid;
24+
import org.springframework.mock.env.MockEnvironment;
2425

2526
import static org.assertj.core.api.Assertions.assertThat;
2627
import static org.mockito.ArgumentMatchers.assertArg;
@@ -39,27 +40,60 @@ class StartupInfoLoggerTests {
3940

4041
private final Log log = mock(Log.class);
4142

43+
private MockEnvironment environment;
44+
45+
@BeforeEach
46+
void setUp() {
47+
this.environment = new MockEnvironment();
48+
this.environment.setProperty("spring.application.version", "1.2.3");
49+
this.environment.setProperty("spring.application.pid", "42");
50+
}
51+
4252
@Test
4353
void startingFormat() {
4454
given(this.log.isInfoEnabled()).willReturn(true);
45-
new StartupInfoLogger(getClass()).logStarting(this.log);
55+
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
4656
then(this.log).should()
47-
.info(assertArg((message) -> assertThat(message.toString())
48-
.contains("Starting " + getClass().getSimpleName() + " using Java " + System.getProperty("java.version")
49-
+ " with PID " + new ApplicationPid() + " (started by " + System.getProperty("user.name")
50-
+ " in " + System.getProperty("user.dir") + ")")));
57+
.info(assertArg(
58+
(message) -> assertThat(message.toString()).contains("Starting " + getClass().getSimpleName()
59+
+ " v1.2.3 using Java " + System.getProperty("java.version") + " with PID 42 (started by "
60+
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
61+
}
62+
63+
@Test
64+
void startingFormatWhenVersionIsNotAvailable() {
65+
this.environment.setProperty("spring.application.version", "");
66+
given(this.log.isInfoEnabled()).willReturn(true);
67+
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
68+
then(this.log).should()
69+
.info(assertArg(
70+
(message) -> assertThat(message.toString()).contains("Starting " + getClass().getSimpleName()
71+
+ " using Java " + System.getProperty("java.version") + " with PID 42 (started by "
72+
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
73+
}
74+
75+
@Test
76+
void startingFormatWhenPidIsNotAvailable() {
77+
this.environment.setProperty("spring.application.pid", "");
78+
given(this.log.isInfoEnabled()).willReturn(true);
79+
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
80+
then(this.log).should()
81+
.info(assertArg(
82+
(message) -> assertThat(message.toString()).contains("Starting " + getClass().getSimpleName()
83+
+ " v1.2.3 using Java " + System.getProperty("java.version") + " (started by "
84+
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
5185
}
5286

5387
@Test
5488
void startingFormatInAotMode() {
5589
System.setProperty("spring.aot.enabled", "true");
5690
try {
5791
given(this.log.isInfoEnabled()).willReturn(true);
58-
new StartupInfoLogger(getClass()).logStarting(this.log);
92+
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
5993
then(this.log).should()
6094
.info(assertArg((message) -> assertThat(message.toString())
61-
.contains("Starting AOT-processed " + getClass().getSimpleName() + " using Java "
62-
+ System.getProperty("java.version") + " with PID " + new ApplicationPid() + " (started by "
95+
.contains("Starting AOT-processed " + getClass().getSimpleName() + " v1.2.3 using Java "
96+
+ System.getProperty("java.version") + " with PID 42 (started by "
6397
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
6498

6599
}
@@ -71,7 +105,7 @@ void startingFormatInAotMode() {
71105
@Test
72106
void startedFormat() {
73107
given(this.log.isInfoEnabled()).willReturn(true);
74-
new StartupInfoLogger(getClass()).logStarted(this.log, new TestStartup(1345L, "Started"));
108+
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log, new TestStartup(1345L, "Started"));
75109
then(this.log).should()
76110
.info(assertArg((message) -> assertThat(message.toString()).matches("Started " + getClass().getSimpleName()
77111
+ " in \\d+\\.\\d{1,3} seconds \\(process running for 1.345\\)")));
@@ -80,7 +114,7 @@ void startedFormat() {
80114
@Test
81115
void startedWithoutUptimeFormat() {
82116
given(this.log.isInfoEnabled()).willReturn(true);
83-
new StartupInfoLogger(getClass()).logStarted(this.log, new TestStartup(null, "Started"));
117+
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log, new TestStartup(null, "Started"));
84118
then(this.log).should()
85119
.info(assertArg((message) -> assertThat(message.toString())
86120
.matches("Started " + getClass().getSimpleName() + " in \\d+\\.\\d{1,3} seconds")));
@@ -89,7 +123,7 @@ void startedWithoutUptimeFormat() {
89123
@Test
90124
void restoredFormat() {
91125
given(this.log.isInfoEnabled()).willReturn(true);
92-
new StartupInfoLogger(getClass()).logStarted(this.log, new TestStartup(null, "Restored"));
126+
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log, new TestStartup(null, "Restored"));
93127
then(this.log).should()
94128
.info(assertArg((message) -> assertThat(message.toString())
95129
.matches("Restored " + getClass().getSimpleName() + " in \\d+\\.\\d{1,3} seconds")));

0 commit comments

Comments
 (0)