1. 程式人生 > >springboot配置logback日誌

springboot配置logback日誌

yml配置 jar包同級目錄下新建config資料夾 將yml檔案拷貝到config資料夾中

日誌配置: logging: config: C:\Users\ttg\Desktop\log-service\config\logback-${spring.profiles.active}.xml path: C:\Users\ttg\Desktop root: info com.ttg: info logback檔案配置 將logback配置檔案拷貝到config資料夾中

<?xml version="1.0" encoding="utf-8" ?> ${CONSOLE_LOG_PATTERN} UTF-8 DEBUG
<file>${LOG_PATH}/${APP_ID}/access.log</file>
<!-- 配置日誌所生成的目錄以及生成檔名的規則 在logs/mylog-2017-06-31.0.log.zip -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <fileNamePattern>${LOG_PATH}/${APP_ID}/%d{yyyy-MM-dd}/access.log.%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
    <!-- 如果按天來回滾,則最大儲存時間為365天,365天之前的都將被清理掉 -->
    <maxHistory>30</maxHistory>
    <!-- 日誌總儲存量為10GB -->
    <totalSizeCap>1GB</totalSizeCap>
    <timeBasedFileNamingAndTriggeringPolicy
            class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <!--檔案達到 最大128MB時會被壓縮和切割 -->
        <maxFileSize>10 KB</maxFileSize>
    </timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
    <!-- pattern節點,用來設定日誌的輸入格式 -->
    <pattern>${FILE_LOG_PATTERN}</pattern>
    <!-- 記錄日誌的編碼:此處設定字符集 - -->
    <charset>UTF-8</charset>
</encoder>
ERROR ACCEPT DENY ${LOG_PATH}/${APP_ID}/access_error.log ${LOG_PATH}/${APP_ID}/%d{yyyy-MM-dd}/access_error.log.%d{yyyy-MM-dd}.zip 10 ${FILE_LOG_PATTERN} UTF-8 0 512 0 512 啟動命令 指定環境啟動 java -jar log-service-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev 啟動指令碼 #!/usr/bin/env bash ##service name APP_NAME=admin SERVICE_NAME=$APP_NAME-service SERVICE_DIR=/app/www/javaweb/appJFT/services/pay-center/$SERVICE_NAME JAR_NAME=$SERVICE_NAME\.jar PID=$SERVICE_NAME\.pid LOG_DIR=/app/logs/appJFT/services/log SERVICE_LOG=$SERVICE_NAME\.log cd "$SERVICE_DIR" || exit -1 if [ -z "$SERVICEPIDFILE" ]; then SERVICEPIDFILE="$SERVICE_DIR/$PID" else # ensure it exists, otw stop will fail mkdir -p "$( dirname "$SERVICEPIDFILE" ) " fi if [ ! -d "$_LOG_DIR" ];then mkdir -p "$LOG_DIR" fi _LOG_OUT="$LOG_DIR/$SERVICE_LOG"

case “$1” in start) echo -n “Starting S

ERVICENAME...&quot;if[f&quot;SERVICE_NAME ... &quot; if [ -f &quot;SERVICEPIDFILE” ]; then if kill -0 cat "$SERVICEPIDFILE" > /dev/null 2>&1; then echo SERVICENAMEalreadyrunningasprocesscat&quot;SERVICE_NAME already running as process `cat &quot;
SERVICEPIDFILE"`. exit 0 fi fi

	if [ ! -f "$JAR_NAME" ];then

echo $JAR_NAME is not exists !!! exit 1 else #nohup java -Xms256m -Xmx512 -jar $JAR_NAME > $LOG 2>&1 >/dev/null & nohup java -jar JARNAME&gt;&quot;JAR_NAME &gt; &quot;_LOG_OUT" 2>&1 < /dev/null & if [ ?eq0]thencase&quot;? -eq 0 ] then case &quot;OSTYPE" in solaris) /bin/echo “!c&quot;&gt;&quot;{!}\\c&quot; &gt; &quot;SERVICEPIDFILE” ;; *) /bin/echo -n !&gt;&quot;! &gt; &quot;SERVICEPIDFILE" ;; esac if [ $? -eq 0 ]; then sleep 1 echo STARTED echo “start $SERVICE_NAME success !!!” else echo FAILED TO WRITE PID echo “start $SERVICE_NAME failed !!!” exit 1 fi else echo SERVER DID NOT START echo “start $SERVICE_NAME failed !!!” exit 1 fi fi ;; stop) echo -n “Stopping SERVICENAME...&quot;if[!f&quot;SERVICE_NAME ... &quot; if [ ! -f &quot;SERVICEPIDFILE” ] then echo “no $SERVICE_NAME to stop (could not find file $SERVICEPIDFILE)” else kill (cat&quot;(cat &quot;SERVICEPIDFILE") rm “$SERVICEPIDFILE” echo STOPPED fi exit 0 ;;

restart)
	shift
	echo "stop $SERVICE_NAME ..."
	$0 stop
	sleep 3
	echo "start $SERVICE_NAME ..."
	$0 start
	if [ $? -eq 0 ];then
	echo "restart $SERVICE_NAME success !!!"
	else
	echo "restart $SERVICE_NAME FAIL !!!"
	exit 1

fi exit 0 ;;

*)
	echo "Usage: $0 {start|stop|restart}" >&2

esac 問題處理 1.logback配置檔案使用include包含注意點 包含檔案必須用標籤作為頂級父標籤 在classpath目錄下 指定任意目錄 2.jar包壓縮 配置檔案處理 說明:java目錄下的xml檔案進行壓縮,排除resources目錄下的配置檔案,將這些檔案拷貝到jar的同級目錄config目錄下 src/main/java /*.xml true src/main/resources /.yml **/.xml