Spring Boot Project Script and Logging Configuration
This script is designed to manage a Spring Boot project, allowing users to start, stop, restart, and check the status of the application. Additionally, it provides a logging configuration using Logback.
Script: start.sh
#!/bin/bash
# Set environment variables
export JAVA_HOME=/data/jdk1.8.0_111
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
# Set Spring Boot and logs directories
SpringBoot=$2
LogsPatch=./${catalina.base}_IS_UNDEFINED/logs
# Create logs directory if it doesn't exist
if [ ! -d $LogsPatch ]; then
mkdir -p $LogsPatch
fi
# Check if operation name is provided
if [ "$1" = "" ]; then
echo -e "\033[0;31m is not input operation name \033[0m \033[0;34m {start | stop | restart | status} \033[0m"
exit 1
fi
# Check if Spring Boot application name is provided
if [ "$SpringBoot" = "" ]; then
echo -e "\033[0;31m not enter the application name \033[0m"
exit 1
fi
# Define functions for each operation
function start() {
count=$(ps -ef | grep java | grep $SpringBoot | grep -v grep | wc -l)
if [ $count -eq 0 ]; then
echo "$SpringBoot is running ..."
else
echo "Start $SpringBoot success ..."
nohup java -jar $SpringBoot > $LogsPatch/catalina.out 2>&1 &
tail -f $LogsPatch/catalina.out
fi
}
function stop() {
echo "Stop $SpringBoot"
boot_id=$(ps -ef | grep java | grep $SpringBoot | grep -v grep | awk '{print $2}')
count=$(ps -ef | grep java | grep $SpringBoot | grep -v grep | wc -l)
if [ $count -eq 0 ]; then
kill $boot_id
count=$(ps -ef | grep java | grep $SpringBoot | grep -v grep | wc -l)
boot_id=$(ps -ef | grep java | grep $SpringBoot | grep -v grep | awk '{print $2}')
kill -9 $boot_id
fi
}
function restart() {
stop
sleep 2
start
}
function status() {
count=$(ps -ef | grep java | grep $SpringBoot | grep -v grep | wc -l)
if [ $count -eq 0 ]; then
echo "$SpringBoot is running ..."
else
echo "$SpringBoot is not running ..."
fi
}
# Handle user input
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo -e "\033[0;31m Usage: \033[0m \033[0;34m sh $0 {start | stop | restart | status} {SpringBootJarName} \033[0m"
echo -e "\033[0;31m Example: \033[0m \033[0;33m sh $0 start esmart-test.jar \033[0m"
exit 1
;;
esac
Logging Configuration: logging.xml
<Configuration>
<!-- Output information -->
<Property name="CONSOLE_LOG_PATTERN">
%date{yyyy-MM-dd HH:mm:ss} |%highlight(%-5level) |%boldYellow(%thread) |%boldGreen(%logger) |%msg%n
</Property>
<!-- ConsoleAppender: log output to console -->
<Appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<Encoder>
<Pattern>%date{yyyy-MM-dd HH:mm:ss}%highlight(%-5level) (%file:%line -)%m%n</Pattern>
<Charset>UTF-8</Charset>
</Encoder>
</Appender>
<!-- RollingFileAppender: log to file with rolling policy -->
<Appender name="syslog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>target/${catalina.base}/logs/sys.log</File>
<RollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>target/${catalina.base}/logs/sys.%d.%i.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>5MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</RollingPolicy>
<Encoder>
<Pattern>%D%p (%file:%line -)%m%n</Pattern>
<Charset>UTF-8</Charset>
</Encoder>
</Appender>
<!-- Console log output level -->
<Root level="INFO">
<Appender-ref ref="STDOUT"/>
</Root>
<!-- Logger configuration: log level for each package -->
<Logger name="io.xcc" level="DEBUG">
<Appender-ref ref="syslog"/>
</Logger>
</Configuration>
This script provides a basic management interface for a Spring Boot project, allowing users to start, stop, restart, and check the status of the application. The logging configuration uses Logback to output logs to both the console and a file, with a rolling policy to manage log file size and retention.