Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spring-boot-project/spring-boot-docs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ task runLoggingFormatExample(type: org.springframework.boot.build.docs.Applicati
mainClass = "org.springframework.boot.docs.features.logexample.MyApplication"
args = ["--spring.main.banner-mode=off", "--server.port=0", "--spring.application.name=myapp"]
output = file("$buildDir/example-output/logging-format.txt")
expectedLogging = "Started MyApplication in "
expectedLogging = "Started MyApplication \"myapp\" in "
normalizeTomcatPort()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private CharSequence getStartingMessage() {
StringBuilder message = new StringBuilder();
message.append("Starting");
appendAotMode(message);
appendApplicationClassName(message);
appendApplicationName(message);
appendApplicationVersion(message);
appendJavaVersion(message);
Expand All @@ -84,6 +85,7 @@ private CharSequence getRunningMessage() {
private CharSequence getStartedMessage(Startup startup) {
StringBuilder message = new StringBuilder();
message.append(startup.action());
appendApplicationClassName(message);
appendApplicationName(message);
message.append(" in ");
message.append(startup.timeTakenToStarted().toMillis() / 1000.0);
Expand All @@ -101,6 +103,10 @@ private void appendAotMode(StringBuilder message) {
}

private void appendApplicationName(StringBuilder message) {
append(message, "\"", "\"", () -> this.environment.getProperty("spring.application.name"));
}

private void appendApplicationClassName(StringBuilder message) {
append(message, "",
() -> (this.sourceClass != null) ? ClassUtils.getShortName(this.sourceClass) : "application");
}
Expand Down Expand Up @@ -137,10 +143,15 @@ private void appendJavaVersion(StringBuilder message) {
}

private void append(StringBuilder message, String prefix, Callable<Object> call) {
append(message, prefix, call, "");
append(message, prefix, "", call);
}

private void append(StringBuilder message, String prefix, String suffix, Callable<Object> call) {
append(message, prefix, suffix, call, "");
}

private void append(StringBuilder message, String prefix, Callable<Object> call, String defaultValue) {
private void append(StringBuilder message, String prefix, String suffix, Callable<Object> call,
String defaultValue) {
Object result = callIfPossible(call);
String value = (result != null) ? result.toString() : null;
if (!StringUtils.hasLength(value)) {
Expand All @@ -150,6 +161,7 @@ private void append(StringBuilder message, String prefix, Callable<Object> call,
message.append((!message.isEmpty()) ? " " : "");
message.append(prefix);
message.append(value);
message.append(suffix);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ void setUp() {
this.environment = new MockEnvironment();
this.environment.setProperty("spring.application.version", "1.2.3");
this.environment.setProperty("spring.application.pid", "42");
this.environment.setProperty("spring.application.name", "spring-boot");
}

@Test
void startingFormat() {
given(this.log.isInfoEnabled()).willReturn(true);
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
then(this.log).should()
.info(assertArg(
(message) -> assertThat(message.toString()).contains("Starting " + getClass().getSimpleName()
+ " v1.2.3 using Java " + System.getProperty("java.version") + " with PID 42 (started by "
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
.info(assertArg((message) -> assertThat(message.toString())
.contains("Starting " + getClass().getSimpleName() + " \"spring-boot\"" + " v1.2.3 using Java "
+ System.getProperty("java.version") + " with PID 42 (started by "
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
}

@Test
Expand All @@ -66,21 +67,33 @@ void startingFormatWhenVersionIsNotAvailable() {
given(this.log.isInfoEnabled()).willReturn(true);
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
then(this.log).should()
.info(assertArg(
(message) -> assertThat(message.toString()).contains("Starting " + getClass().getSimpleName()
+ " using Java " + System.getProperty("java.version") + " with PID 42 (started by "
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
.info(assertArg((message) -> assertThat(message.toString())
.contains("Starting " + getClass().getSimpleName() + " \"spring-boot\"" + " using Java "
+ System.getProperty("java.version") + " with PID 42 (started by "
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
}

@Test
void startingFormatWhenPidIsNotAvailable() {
this.environment.setProperty("spring.application.pid", "");
given(this.log.isInfoEnabled()).willReturn(true);
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
then(this.log).should()
.info(assertArg((message) -> assertThat(message.toString())
.contains("Starting " + getClass().getSimpleName() + " \"spring-boot\"" + " v1.2.3 using Java "
+ System.getProperty("java.version") + " (started by " + System.getProperty("user.name")
+ " in " + System.getProperty("user.dir") + ")")));
}

@Test
void startingFormatWhenApplicationNameIsNotAvailable() {
this.environment.setProperty("spring.application.name", "");
given(this.log.isInfoEnabled()).willReturn(true);
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
then(this.log).should()
.info(assertArg(
(message) -> assertThat(message.toString()).contains("Starting " + getClass().getSimpleName()
+ " v1.2.3 using Java " + System.getProperty("java.version") + " (started by "
+ " v1.2.3 using Java " + System.getProperty("java.version") + " with PID 42 (started by "
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));
}

Expand All @@ -92,10 +105,9 @@ void startingFormatInAotMode() {
new StartupInfoLogger(getClass(), this.environment).logStarting(this.log);
then(this.log).should()
.info(assertArg((message) -> assertThat(message.toString())
.contains("Starting AOT-processed " + getClass().getSimpleName() + " v1.2.3 using Java "
+ System.getProperty("java.version") + " with PID 42 (started by "
.contains("Starting AOT-processed " + getClass().getSimpleName() + " \"spring-boot\""
+ " v1.2.3 using Java " + System.getProperty("java.version") + " with PID 42 (started by "
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")")));

}
finally {
System.clearProperty("spring.aot.enabled");
Expand All @@ -108,25 +120,25 @@ void startedFormat() {
new StartupInfoLogger(getClass(), this.environment).logStarted(this.log, new TestStartup(1345L, "Started"));
then(this.log).should()
.info(assertArg((message) -> assertThat(message.toString()).matches("Started " + getClass().getSimpleName()
+ " in \\d+\\.\\d{1,3} seconds \\(process running for 1.345\\)")));
+ " \"spring-boot\"" + " in \\d+\\.\\d{1,3} seconds \\(process running for 1.345\\)")));
}

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

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

static class TestStartup extends Startup {
Expand Down