Skip to content

Commit 68d42d5

Browse files
committed
Added spring.application.name to StartupInfoLogger
1 parent ba0ef11 commit 68d42d5

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private CharSequence getStartingMessage() {
6464
StringBuilder message = new StringBuilder();
6565
message.append("Starting");
6666
appendAotMode(message);
67+
appendApplicationClassName(message);
6768
appendApplicationName(message);
6869
appendApplicationVersion(message);
6970
appendJavaVersion(message);
@@ -84,6 +85,7 @@ private CharSequence getRunningMessage() {
8485
private CharSequence getStartedMessage(Startup startup) {
8586
StringBuilder message = new StringBuilder();
8687
message.append(startup.action());
88+
appendApplicationClassName(message);
8789
appendApplicationName(message);
8890
message.append(" in ");
8991
message.append(startup.timeTakenToStarted().toMillis() / 1000.0);
@@ -101,6 +103,10 @@ private void appendAotMode(StringBuilder message) {
101103
}
102104

103105
private void appendApplicationName(StringBuilder message) {
106+
append(message, "\"", "\"", () -> this.environment.getProperty("spring.application.name"));
107+
}
108+
109+
private void appendApplicationClassName(StringBuilder message) {
104110
append(message, "",
105111
() -> (this.sourceClass != null) ? ClassUtils.getShortName(this.sourceClass) : "application");
106112
}
@@ -137,10 +143,15 @@ private void appendJavaVersion(StringBuilder message) {
137143
}
138144

139145
private void append(StringBuilder message, String prefix, Callable<Object> call) {
140-
append(message, prefix, call, "");
146+
append(message, prefix, "", call);
147+
}
148+
149+
private void append(StringBuilder message, String prefix, String suffix, Callable<Object> call) {
150+
append(message, prefix, suffix, call, "");
141151
}
142152

143-
private void append(StringBuilder message, String prefix, Callable<Object> call, String defaultValue) {
153+
private void append(StringBuilder message, String prefix, String suffix, Callable<Object> call,
154+
String defaultValue) {
144155
Object result = callIfPossible(call);
145156
String value = (result != null) ? result.toString() : null;
146157
if (!StringUtils.hasLength(value)) {
@@ -150,6 +161,7 @@ private void append(StringBuilder message, String prefix, Callable<Object> call,
150161
message.append((!message.isEmpty()) ? " " : "");
151162
message.append(prefix);
152163
message.append(value);
164+
message.append(suffix);
153165
}
154166
}
155167

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,18 @@ void setUp() {
4747
this.environment = new MockEnvironment();
4848
this.environment.setProperty("spring.application.version", "1.2.3");
4949
this.environment.setProperty("spring.application.pid", "42");
50+
this.environment.setProperty("spring.application.name", "spring-boot");
5051
}
5152

5253
@Test
5354
void startingFormat() {
5455
given(this.log.isInfoEnabled()).willReturn(true);
5556
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
5657
then(this.log).should()
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") + ")")));
58+
.info(assertArg((message) -> assertThat(message.toString())
59+
.contains("Starting " + getClass().getSimpleName() + " \"spring-boot\"" + " v1.2.3 using Java "
60+
+ System.getProperty("java.version") + " with PID 42 (started by "
61+
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
6162
}
6263

6364
@Test
@@ -66,21 +67,33 @@ void startingFormatWhenVersionIsNotAvailable() {
6667
given(this.log.isInfoEnabled()).willReturn(true);
6768
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
6869
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") + ")")));
70+
.info(assertArg((message) -> assertThat(message.toString())
71+
.contains("Starting " + getClass().getSimpleName() + " \"spring-boot\"" + " using Java "
72+
+ System.getProperty("java.version") + " with PID 42 (started by "
73+
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
7374
}
7475

7576
@Test
7677
void startingFormatWhenPidIsNotAvailable() {
7778
this.environment.setProperty("spring.application.pid", "");
7879
given(this.log.isInfoEnabled()).willReturn(true);
7980
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
81+
then(this.log).should()
82+
.info(assertArg((message) -> assertThat(message.toString())
83+
.contains("Starting " + getClass().getSimpleName() + " \"spring-boot\"" + " v1.2.3 using Java "
84+
+ System.getProperty("java.version") + " (started by " + System.getProperty("user.name")
85+
+ " in " + System.getProperty("user.dir") + ")")));
86+
}
87+
88+
@Test
89+
void startingFormatWhenApplicationNameIsNotAvailable() {
90+
this.environment.setProperty("spring.application.name", "");
91+
given(this.log.isInfoEnabled()).willReturn(true);
92+
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
8093
then(this.log).should()
8194
.info(assertArg(
8295
(message) -> assertThat(message.toString()).contains("Starting " + getClass().getSimpleName()
83-
+ " v1.2.3 using Java " + System.getProperty("java.version") + " (started by "
96+
+ " v1.2.3 using Java " + System.getProperty("java.version") + " with PID 42 (started by "
8497
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
8598
}
8699

@@ -92,10 +105,9 @@ void startingFormatInAotMode() {
92105
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
93106
then(this.log).should()
94107
.info(assertArg((message) -> assertThat(message.toString())
95-
.contains("Starting AOT-processed " + getClass().getSimpleName() + " v1.2.3 using Java "
96-
+ System.getProperty("java.version") + " with PID 42 (started by "
108+
.contains("Starting AOT-processed " + getClass().getSimpleName() + " \"spring-boot\""
109+
+ " v1.2.3 using Java " + System.getProperty("java.version") + " with PID 42 (started by "
97110
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
98-
99111
}
100112
finally {
101113
System.clearProperty("spring.aot.enabled");
@@ -108,25 +120,25 @@ void startedFormat() {
108120
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log, new TestStartup(1345L, "Started"));
109121
then(this.log).should()
110122
.info(assertArg((message) -> assertThat(message.toString()).matches("Started " + getClass().getSimpleName()
111-
+ " in \\d+\\.\\d{1,3} seconds \\(process running for 1.345\\)")));
123+
+ " \"spring-boot\"" + " in \\d+\\.\\d{1,3} seconds \\(process running for 1.345\\)")));
112124
}
113125

114126
@Test
115127
void startedWithoutUptimeFormat() {
116128
given(this.log.isInfoEnabled()).willReturn(true);
117129
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log, new TestStartup(null, "Started"));
118130
then(this.log).should()
119-
.info(assertArg((message) -> assertThat(message.toString())
120-
.matches("Started " + getClass().getSimpleName() + " in \\d+\\.\\d{1,3} seconds")));
131+
.info(assertArg((message) -> assertThat(message.toString()).matches(
132+
"Started " + getClass().getSimpleName() + " \"spring-boot\"" + " in \\d+\\.\\d{1,3} seconds")));
121133
}
122134

123135
@Test
124136
void restoredFormat() {
125137
given(this.log.isInfoEnabled()).willReturn(true);
126138
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log, new TestStartup(null, "Restored"));
127139
then(this.log).should()
128-
.info(assertArg((message) -> assertThat(message.toString())
129-
.matches("Restored " + getClass().getSimpleName() + " in \\d+\\.\\d{1,3} seconds")));
140+
.info(assertArg((message) -> assertThat(message.toString()).matches(
141+
"Restored " + getClass().getSimpleName() + " \"spring-boot\"" + " in \\d+\\.\\d{1,3} seconds")));
130142
}
131143

132144
static class TestStartup extends Startup {

0 commit comments

Comments
 (0)