下面这个例子来自SpringBoot源码,这里是要打印程序启动的时间这样的字符串,需要拼装的信息有 程序名字,启动时长,JVM时长。
private StringBuilder getStartedMessage(StopWatch stopWatch) {
StringBuilder message = new StringBuilder();
message.append("Started ");
message.append(getApplicationName());
message.append(" in ");
message.append(stopWatch.getTotalTimeSeconds());
try {
double uptime = ManagementFactory.getRuntimeMXBean().getUptime() / 1000.0;
message.append(" seconds (JVM running for " + uptime + ")");
}
catch (Throwable ex) {
// No JVM time available
}
return message;
}
最终拼出来的字符串效果为
Started SubscribeServerApplication in 23.103 seconds (JVM running for 27.933)