Skip to content

Commit cd77cfc

Browse files
committed
Add configurable startup time format
Introduce a new configuration property 'spring.main.log-startup-time-format' to control how application startup times are displayed in logs. This addresses concerns about backward compatibility with existing log parsing tools. The property supports two formats: - 'decimal' (default): Displays time in seconds with millisecond precision (e.g., "3.456 seconds"). This maintains backward compatibility. - 'human-readable': Displays time in a more intuitive way using appropriate units (e.g., "1 minute 30 seconds" or "1 hour 15 minutes"). By defaulting to the existing decimal format, this change ensures no breaking changes for existing deployments while providing an opt-in mechanism for users who prefer more readable startup time logs. Signed-off-by: Huang Xiao <[email protected]>
1 parent 29d0299 commit cd77cfc

File tree

3 files changed

+165
-6
lines changed

3 files changed

+165
-6
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,32 @@ private CharSequence getStartedMessage(Startup startup) {
8787
message.append(startup.action());
8888
appendApplicationName(message);
8989
message.append(" in ");
90-
message.append(startup.timeTakenToStarted().toMillis() / 1000.0);
91-
message.append(" seconds");
90+
message.append(formatDuration(startup.timeTakenToStarted().toMillis()));
9291
Long uptimeMs = startup.processUptime();
9392
if (uptimeMs != null) {
94-
double uptime = uptimeMs / 1000.0;
95-
message.append(" (process running for ").append(uptime).append(")");
93+
message.append(" (process running for ").append(formatDuration(uptimeMs)).append(")");
9694
}
9795
return message;
9896
}
9997

98+
private String formatDuration(long millis) {
99+
StartupTimeFormat format = getStartupTimeFormat();
100+
return format.format(millis);
101+
}
102+
103+
private StartupTimeFormat getStartupTimeFormat() {
104+
String formatProperty = this.environment.getProperty("spring.main.log-startup-time-format");
105+
if (formatProperty != null) {
106+
try {
107+
return StartupTimeFormat.valueOf(formatProperty.toUpperCase().replace('-', '_'));
108+
}
109+
catch (IllegalArgumentException ex) {
110+
// Invalid format, use default
111+
}
112+
}
113+
return StartupTimeFormat.DECIMAL;
114+
}
115+
100116
private void appendAotMode(StringBuilder message) {
101117
append(message, "", () -> AotDetector.useGeneratedArtifacts() ? "AOT-processed" : null);
102118
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot;
18+
19+
import java.time.Duration;
20+
21+
/**
22+
* Format styles for displaying application startup time in logs.
23+
*
24+
* @author Huang Xiao
25+
* @since 3.5.0
26+
*/
27+
public enum StartupTimeFormat {
28+
29+
/**
30+
* Decimal format displays time in seconds with millisecond precision (e.g., "3.456
31+
* seconds"). This is the default format and maintains backward compatibility with
32+
* existing log parsing tools.
33+
*/
34+
DECIMAL {
35+
36+
@Override
37+
public String format(long millis) {
38+
return String.format("%.3f seconds", millis / 1000.0);
39+
}
40+
41+
},
42+
43+
/**
44+
* Human-readable format displays time in a more intuitive way using appropriate units
45+
* (e.g., "1 minute 30 seconds" or "1 hour 15 minutes"). Times under 60 seconds still
46+
* use the decimal format for consistency.
47+
*/
48+
HUMAN_READABLE {
49+
50+
@Override
51+
public String format(long millis) {
52+
Duration duration = Duration.ofMillis(millis);
53+
long seconds = duration.getSeconds();
54+
if (seconds < 60) {
55+
return String.format("%.3f seconds", millis / 1000.0);
56+
}
57+
long hours = duration.toHours();
58+
int minutes = duration.toMinutesPart();
59+
int secs = duration.toSecondsPart();
60+
if (hours > 0) {
61+
return String.format("%d hour%s %d minute%s", hours, (hours != 1) ? "s" : "", minutes,
62+
(minutes != 1) ? "s" : "");
63+
}
64+
return String.format("%d minute%s %d second%s", minutes, (minutes != 1) ? "s" : "", secs,
65+
(secs != 1) ? "s" : "");
66+
}
67+
68+
};
69+
70+
/**
71+
* Format the given duration in milliseconds according to this format style.
72+
* @param millis the duration in milliseconds
73+
* @return the formatted string
74+
*/
75+
public abstract String format(long millis);
76+
77+
}

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

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void startedFormat() {
109109
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log, new TestStartup(1345L, "Started"));
110110
then(this.log).should()
111111
.info(assertArg((message) -> assertThat(message.toString()).matches("Started " + getClass().getSimpleName()
112-
+ " in \\d+\\.\\d{1,3} seconds \\(process running for 1.345\\)")));
112+
+ " in \\d+\\.\\d{1,3} seconds \\(process running for 1\\.345 seconds\\)")));
113113
}
114114

115115
@Test
@@ -130,17 +130,83 @@ void restoredFormat() {
130130
.matches("Restored " + getClass().getSimpleName() + " in \\d+\\.\\d{1,3} seconds")));
131131
}
132132

133+
@Test
134+
void startedFormatWithHumanReadableMinutes() {
135+
this.environment.setProperty("spring.main.log-startup-time-format", "human-readable");
136+
given(this.log.isInfoEnabled()).willReturn(true);
137+
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log,
138+
new TestStartup(90000L, "Started", 90000L));
139+
then(this.log).should()
140+
.info(assertArg(
141+
(message) -> assertThat(message.toString()).isEqualTo("Started " + getClass().getSimpleName()
142+
+ " in 1 minute 30 seconds (process running for 1 minute 30 seconds)")));
143+
}
144+
145+
@Test
146+
void startedFormatWithHumanReadableHours() {
147+
this.environment.setProperty("spring.main.log-startup-time-format", "human-readable");
148+
given(this.log.isInfoEnabled()).willReturn(true);
149+
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log,
150+
new TestStartup(4500000L, "Started", 4500000L));
151+
then(this.log).should()
152+
.info(assertArg((message) -> assertThat(message.toString()).isEqualTo("Started "
153+
+ getClass().getSimpleName() + " in 1 hour 15 minutes (process running for 1 hour 15 minutes)")));
154+
}
155+
156+
@Test
157+
void startedFormatWithHumanReadableSingularUnits() {
158+
this.environment.setProperty("spring.main.log-startup-time-format", "human-readable");
159+
given(this.log.isInfoEnabled()).willReturn(true);
160+
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log,
161+
new TestStartup(61000L, "Started", 61000L));
162+
then(this.log).should()
163+
.info(assertArg((message) -> assertThat(message.toString()).isEqualTo("Started "
164+
+ getClass().getSimpleName() + " in 1 minute 1 second (process running for 1 minute 1 second)")));
165+
}
166+
167+
@Test
168+
void startedFormatWithHumanReadableZeroSeconds() {
169+
this.environment.setProperty("spring.main.log-startup-time-format", "human-readable");
170+
given(this.log.isInfoEnabled()).willReturn(true);
171+
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log,
172+
new TestStartup(300000L, "Started", 300000L));
173+
then(this.log).should()
174+
.info(assertArg(
175+
(message) -> assertThat(message.toString()).isEqualTo("Started " + getClass().getSimpleName()
176+
+ " in 5 minutes 0 seconds (process running for 5 minutes 0 seconds)")));
177+
}
178+
179+
@Test
180+
void startedFormatWithDefaultDecimalFormat() {
181+
given(this.log.isInfoEnabled()).willReturn(true);
182+
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log,
183+
new TestStartup(90000L, "Started", 90000L));
184+
then(this.log).should()
185+
.info(assertArg((message) -> assertThat(message.toString()).matches("Started " + getClass().getSimpleName()
186+
+ " in \\d+\\.\\d{1,3} seconds \\(process running for \\d+\\.\\d{1,3} seconds\\)")));
187+
}
188+
133189
static class TestStartup extends Startup {
134190

135-
private final long startTime = System.currentTimeMillis();
191+
private long startTime;
136192

137193
private final @Nullable Long uptime;
138194

139195
private final String action;
140196

141197
TestStartup(@Nullable Long uptime, String action) {
198+
this(uptime, action, null);
199+
}
200+
201+
TestStartup(@Nullable Long uptime, String action, @Nullable Long timeTaken) {
142202
this.uptime = uptime;
143203
this.action = action;
204+
if (timeTaken != null) {
205+
this.startTime = System.currentTimeMillis() - timeTaken;
206+
}
207+
else {
208+
this.startTime = System.currentTimeMillis();
209+
}
144210
started();
145211
}
146212

0 commit comments

Comments
 (0)