SpringShell 應(yīng)用啟動時, 默認會輸出Spring Shell 的啟動信息. 對于一個專業(yè)的shell來講, 輸出Spring相關(guān)的啟動信息, 肯定是不合適的, 我們需要輸出我們系統(tǒng)相關(guān)的信息.
1. SpringShell 默認啟動信息
SpringShell 的啟動信息一共包括三部分: SpringBoot banner, SpringBppt啟動類日志, shell命令提示符.
# SpringBoot banner
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.2.RELEASE)
# SpringBooot 啟動類日志
2019-01-25 16:05:59.260 INFO 14751 --- [ main] o.z.l.s.shell.SpringShellApplication : Starting SpringShellApplication v0.0.1-SNAPSHOT on zongf-E570 with PID 14751 (/data/idea/learn-spring/spring-shell/target/spring-shell-0.0.1-SNAPSHOT.jar started by zongf in /data/idea/learn-spring/spring-shell/target)
2019-01-25 16:05:59.265 INFO 14751 --- [ main] o.z.l.s.shell.SpringShellApplication : No active profile set, falling back to default profiles: default
2019-01-25 16:06:00.561 INFO 14751 --- [ main] o.z.l.s.shell.SpringShellApplication : Started SpringShellApplication in 1.79 seconds (JVM running for 2.263)
# shell 命令提示符
shell:>
2. 自定義啟動信息
對于SpringShell 應(yīng)用默認的三部分啟動信息, 我們需要分別進行處理.
2.1 自定義banner
- 由于SpringShell 應(yīng)用本身就是一個SpringBoot 應(yīng)用, 因此借助于SpringBoot應(yīng)用自定義Banner方案即可實現(xiàn).
- 修改SpringBoot應(yīng)用banner 只需要在resources文件夾中添加banner.txt文件即可, 可以自定義banner內(nèi)容, 樣式, 顏色等, 具體文件內(nèi)容格式非本博客核心內(nèi)容, 故筆者不在此詳述.
- 筆者指定banner 內(nèi)容, 字體顏色, 版本號等信息.
- 文字banner生成網(wǎng)站:http://patorjk.com/software/taag/#p=display&f=Slant&t=MYSHELL
${AnsiColor.BRIGHT_YELLOW} ${AnsiStyle.BOLD}
Welcom to :
__ _____ _______ __ __________ __
/ |/ /\ \/ / ___// / / / ____/ / / /
/ /|_/ / \ /\__ \/ /_/ / __/ / / / /
/ / / / / /___/ / __ / /___/ /___/ /___
/_/ /_/ /_//____/_/ /_/_____/_____/_____/
Version: ${application.version}
Author: zongf
Date: 2019-01-26
2.2 自定義系統(tǒng)日志
SpringBoot啟動類日志級別為info, 我們只需要將啟動類日志提升為warn即可. 直接修改application.yml 文件即可. 筆者修改為:
# pattern 并沒有實際用處, 這是筆者測試時用的.
logging:
pattern:
console: "%date{yyyy-MM-dd HH:mm:ss.SSS}[%level][%thread]-%msg%n"
level:
org.zongf.learn.spring.shell.SpringShellApplication: warn
2.3 自定義命令提示符
SpringShell 自定義命令提示符在筆者之前的博客中已經(jīng)提及了, 新增自定義命令提示符組件即可.
@Component
public class CustomPromptProvider implements PromptProvider {
@Override
public AttributedString getPrompt() {
// 獲取主機名稱
String hostName = getHostName();
// 設(shè)置命令提示符文字
String promot = "spring@" + hostName + "> ";
// 設(shè)置命令提示符字體樣式
AttributedStyle promotStyle = AttributedStyle.BOLD.foreground(AttributedStyle.GREEN);
// 返回命令提示符
return new AttributedString(promot, promotStyle);
}
/**
* @Description: 獲取主機名稱
* @return: String 主機名稱
* @author: zongf
* @time: 2019-01-26 08:58:45
*/
private String getHostName(){
String hostName = "";
try {
InetAddress inetAddress = InetAddress.getLocalHost();
hostName = inetAddress.getHostName();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return hostName;
}
}
3. 啟動SpringShell 應(yīng)用
重新打包后啟動應(yīng)用, 會發(fā)現(xiàn)啟動信息完全實現(xiàn)了定制化.
$ ./bin/spring-shell.sh
Welcom to :
__ _____ _______ __ __________ __
/ |/ /\ \/ / ___// / / / ____/ / / /
/ /|_/ / \ /\__ \/ /_/ / __/ / / / /
/ / / / / /___/ / __ / /___/ /___/ /___
/_/ /_/ /_//____/_/ /_/_____/_____/_____/
Version: 0.0.1-SNAPSHOT
Author: zongf
Date: 2019-01-26
spring@zongf-E570> add 1 2
3