日志分割问题
SpringBoot版本使用2.3
maven打包
build部分代码如下
<build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-install-plugin</artifactId><version>3.1.0</version><configuration><skip>true</skip></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-jar-plugin</artifactId><version>3.2.0</version><configuration><archive><manifest><addClasspath>true</addClasspath><classpathPrefix>lib/</classpathPrefix><mainClass>com.test.Application</mainClass></manifest></archive><excludes><exclude>*.properties</exclude><exclude>*.sh</exclude><exclude>*.bat</exclude><exclude>*.xml</exclude></excludes></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-dependency-plugin</artifactId><executions><execution><id>copy</id><phase>package</phase><goals><goal>copy-dependencies</goal></goals><configuration><outputDirectory>${project.build.directory}/${project.artifactId}/lib</outputDirectory></configuration></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.8</version><executions><execution><id>deploy</id><phase>package</phase><goals><goal>run</goal></goals><configuration><target><copy todir = "${project.build.directory}/${project.build.finalName}" file = "${project.build.directory}/${project.build.finalName}.${project.packaging}"></copy><copy todir = "${project.build.directory}/${project.build.finalName}/config/" file = "${project.build.directory}/classes/application-dev.properties"></copy><move file="${project.build.directory}/${project.build.finalName}/config/application-dev.properties" tofile="${project.build.directory}/${project.build.finalName}/config/application.properties"/><copy todir = "${project.build.directory}/${project.build.finalName}/config/" file = "${project.build.directory}/classes/seata.conf"></copy><copy todir = "${project.build.directory}/${project.build.finalName}/config/" file = "${project.build.directory}/classes/logback-spring.xml"></copy><copy todir = "${project.build.directory}/${project.build.finalName}" file = "${project.basedir}/src/bin/start.sh"></copy><copy todir = "${project.build.directory}/${project.build.finalName}" file = "${project.basedir}/src/bin/stop.sh"></copy><copy todir = "${project.build.directory}/${project.build.finalName}" file = "${project.basedir}/src/bin/deploy.sh"></copy><copy todir = "${project.build.directory}/${project.build.finalName}" file = "${project.basedir}/src/bin/Dockerfile"></copy><copy todir = "${project.build.directory}/${project.build.finalName}" file = "${project.basedir}/src/bin/run.bat"></copy></target></configuration></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><skipTests>true</skipTests></configuration></plugin></plugins></build>
application.properties配置
部分关键配置如下:
logging.config=config/logback.xml
logging.level.root=ERROR
logging.level.com.test=INFO
logging.file.max-history=7
logging.file.max-size=100MB
编译后目录结构
├ config
│ ├ application.properties
│ ├ logback.xml
│ ├ seata.conf
├ lib
│ ├ accessors-smart-1.2.jar
│ ├ android-json-0.0.20131108.vaadin1.jar
│ ├ …
├ demo.jar
├ deploy.sh
├ Dockerfile
├ run.bat
├ start.sh
├ stop.sh
logback.xml配置
这使用的是自定义配置,主要修改这个文件即可,如果在logback中需要使用application.properties中配置信息,使用springProperty配置即可,source对应的就是application.properties中的配置项,xml中使用${LOG_SIZE}即可。
<?xml version="1.0" encoding="UTF-8"?>
<configuration><statusListener class="ch.qos.logback.core.status.NopStatusListener"/><property name="LOG_HOME" value="logs"/><property name="APP_NAME" value="demo"/><springProperty scope="context" name="LOG_SIZE" source="logging.file.max-size"/><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %msg [%thread]\(%c:%L\)%n</pattern></encoder></appender><appender name="FILELOG" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${LOG_HOME}/${APP_NAME}.log</file><rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"><fileNamePattern>${LOG_HOME}/${APP_NAME}.log.%d{yyyy-MM-dd}.%i.zip</fileNamePattern><maxFileSize>${LOG_SIZE}</maxFileSize><maxHistory>5</maxHistory><cleanHistoryOnStart>true</cleanHistoryOnStart></rollingPolicy><encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %msg [%thread]\(%c:%L\)%n</pattern></encoder></appender><appender name="ERRORLOG" class="ch.qos.logback.core.rolling.RollingFileAppender"><file>${LOG_HOME}/${APP_NAME}-err.log</file><rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"><fileNamePattern>${LOG_HOME}/${APP_NAME}-err.log.%d{yyyy-MM-dd}.%i.zip</fileNamePattern><maxFileSize>${LOG_SIZE}</maxFileSize><maxHistory>5</maxHistory><cleanHistoryOnStart>true</cleanHistoryOnStart></rollingPolicy><encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %msg [%thread]\(%c:%L\)%n</pattern></encoder><filter class="ch.qos.logback.classic.filter.LevelFilter"><level>ERROR</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter></appender><root level="ERROR"><appender-ref ref="STDOUT"/><appender-ref ref="FILELOG"/><appender-ref ref="ERRORLOG"/></root><logger name="com.test" level="INFO"/></configuration>
deploy.sh
#!/bin/shapp_name=demo
docker stop $app_name
docker rm -f $app_name
docker rmi $app_name:latest
docker build -t $app_name:latest .
docker run -d --network=host --name $app_name \
-v /data/app/$app_name/logs:/opt/app/logs \
-v /data/app/$app_name/config:/opt/app/config \
$app_name:latest
Dockerfile
FROM adoptopenjdk/openjdk8:aarch64-ubuntu-jre8u352-b05-ea-nightly
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN mkdir -p /opt/app/logs
RUN mkdir -p /opt/app/config
RUN mkdir -p /opt/app/lib
WORKDIR /opt/appADD lib/ /opt/app/lib
COPY demo.jar /opt/app/demo.jarCMD java -Xms4G -Xmx4G -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/opt/app/logs/demo.hprof -jar /opt/app/demo.jar --spring.config.location=/opt/app/config/ > /opt/app/logs/demo.log 2>&1#CMD while true; do sleep 1; doneEXPOSE 8080
start.sh
#!/bin/shSH_DIR=$(cd `dirname $0`;pwd)
java_home=env|grep "JAVA_HOME";
latestjar=`ls $SH_DIR -lt | grep '.jar'| awk '{print $9}' | sed -n '1p'`
echo 'Start:'$latestjar
process_exists=`ps -ef|grep $latestjar|grep -v grep|awk '{print $2}'`
if [ -n "${process_exists}" ];thenkill -9 ${process_exists}
fi
if [ ! -n "$java_home" ]; thencd $SH_DIR/nohup java -Xms100m -Xmx2g -jar $SH_DIR/$latestjar --spring.config.location=$SH_DIR/config/ > /dev/null 2>&1 &echo "Application is started";cd -;exit;
elsejava -version;if [ $? -eq 0 ]; thencd $SH_DIR/nohup java -Xms100m -Xmx2g -jar $SH_DIR/$latestjar --spring.config.location=$SH_DIR/config/ > /dev/null 2>&1 &elseecho "This system has no jdk";cd -;exit;fi
fi
stop.sh
#!/bin/sh
SH_DIR=$(cd `dirname $0`;pwd)
java_home=env|grep "JAVA_HOME";
latestjar=`ls $SH_DIR -lt | grep '.jar'| awk '{print $9}' | sed -n '1p'`
echo 'Stop:'$latestjar
process_exists=`ps -ef|grep $latestjar|grep -v grep|awk '{print $2}'`
if [ -n "${process_exists}" ];thenkill -9 ${process_exists}
fi
echo "Application stop sucess"