logback.xml動態配置程序路徑的操作

1.普通java程序

如下配置的logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
 
  <property name="LOGS_HOME" value="/opt/program/app/logback-demo/logs"/>
  <!-- 該值由啟動程序時候動態傳入 -->
  <property name="SUB_LOG_HOME" value="" />
  <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{25} - %msg%n"/>
  <property name="LOG_CHARSET" value="UTF-8"/>
 
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>${LOG_PATTERN}</pattern>
      <charset>${LOG_CHARSET}</charset>
    </encoder>
  </appender>
 
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOGS_HOME}/${SUB_LOG_HOME}/logback-demo.log</file>
    <encoder>
      <pattern>${LOG_PATTERN}</pattern>
      <charset>${LOG_CHARSET}</charset>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>${LOGS_HOME}/${SUB_LOG_HOME}/logback-demo-%d{yyyy-MM-dd}.log</fileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
  </appender>
 
  <appender name="ERRORFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOGS_HOME}/${SUB_LOG_HOME}/logback-demo.error.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>${LOGS_HOME}/${SUB_LOG_HOME}/logback-demo-%d{yyyy-MM-dd}.error.log</fileNamePattern>
      <maxHistory>20</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>${LOG_PATTERN}</pattern>
      <immediateFlush>true</immediateFlush>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>ERROR</level>
      <onMatch>ACCEPT</onMatch>
      <onMismatch>DENY</onMismatch>
    </filter>
  </appender>
 
  <root level="info">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="FILE"/>
    <appender-ref ref="ERRORFILE"/>
  </root>
 
</configuration>

property標簽由name和value組成,上邊定義的name可以在下邊通過${name}引用。

value的值可以直接在xml中寫死,也可以通過程序啟動時候通過jvm虛擬機參數配置來傳入,比如此處是-DSUB_LOG_HOME=xxx。

idea中配置:

2.spring boot應用

spring boot應用中將logback-spring.xml放置在resources目錄,內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://ch.qos.logback/xml/ns/logback" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback http://ch.qos.logback/xml/ns/logback/logback.xsd"
        debug="true" scan="false" scanPeriod="30 seconds">
 
  <springProperty scope="context" name="logPath" source="logback.logPath"/>
  <springProperty scope="context" name="subLogPath" source="logback.subLogPath"/>
 
  <!--spring-boot-1.5.10.RELEASE.jar!/org/springframework/boot/logging/logback/defaults.xml-->
  <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
  <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
  <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
 
  <property name="LOG_HOME" value="${logPath}"/>
  <!-- 該值由啟動程序時候動態傳入 -->
  <property name="SUB_LOG_HOME" value="${subLogPath}" />
  <property name="LOG_NAME" value="data-middleware-server-realtime"/>
  <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39} %line){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
  <property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{25} %line - %msg%n"/>
 
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>${CONSOLE_LOG_PATTERN}</pattern>
    </encoder>
  </appender>
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/${SUB_LOG_HOME}/${LOG_NAME}.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
      <fileNamePattern>${LOG_HOME}/${SUB_LOG_HOME}/${LOG_NAME}.log-%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
      <maxHistory>20</maxHistory>
      <maxFileSize>100MB</maxFileSize>
    </rollingPolicy>
    <encoder>
      <pattern>${FILE_LOG_PATTERN}</pattern>
      <immediateFlush>true</immediateFlush>
    </encoder>
  </appender>
  <appender name="ERRORFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/${SUB_LOG_HOME}/${LOG_NAME}.error.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
      <fileNamePattern>${LOG_HOME}/${SUB_LOG_HOME}/${LOG_NAME}.error.log-%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
      <maxHistory>20</maxHistory>
      <maxFileSize>100MB</maxFileSize>
    </rollingPolicy>
    <encoder>
      <pattern>${FILE_LOG_PATTERN}</pattern>
      <immediateFlush>true</immediateFlush>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>ERROR</level>
      <onMatch>ACCEPT</onMatch>
      <onMismatch>DENY</onMismatch>
    </filter>
  </appender>
 
  <appender name="REALTIME_IMPORT_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_HOME}/${SUB_LOG_HOME}/import/${LOG_NAME}.log</file>
    <encoder>
      <pattern>${FILE_LOG_PATTERN}</pattern>
      <immediateFlush>true</immediateFlush>
    </encoder>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>${LOG_HOME}/${SUB_LOG_HOME}/import/${LOG_NAME}-%d{yyyy-MM-dd}.log</fileNamePattern>
      <maxHistory>7</maxHistory>
    </rollingPolicy>
  </appender>
 
  <logger name="REALTIME_IMPORT_LOGGER" level="INFO" additivity="false">
    <appender-ref ref="REALTIME_IMPORT_APPENDER"/>
  </logger>
 
  <logger name="org.apache.zookeeper.ZooKeeper" level="WARN" additivity="true"></logger>
  <logger name="org.apache.zookeeper.ClientCnxn" level="WARN" additivity="true"></logger>
 
  <root level="DEBUG">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="FILE"/>
    <appender-ref ref="ERRORFILE"/>
  </root>
</configuration>

該配置文件中除瞭使用<property>還使用瞭<springProperty>其中source為引用的spring配置中的變量,比如此處是logback.logPath和logback.subLogPath這兩個值的傳入可以通過spring boot配置文件傳入,也可以通過程序啟動時候動態傳入,比如–logback.subLogPath=xxx

補充:logback.xml 自動讀取日志存儲路徑

文件的命名和加載順序有關logback.xml早於application.yml加載,logback-spring.xml晚於application.yml加載

如果logback配置需要使用application.yml中的屬性,需要命名為logback-spring.xml

logback使用application.yml中的屬性

使用springProperty才可使用application.yml中的值 可以設置默認值

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <!--
  說明:
    1. 文件的命名和加載順序有關
      logback.xml早於application.yml加載,logback-spring.xml晚於application.yml加載
      如果logback配置需要使用application.yml中的屬性,需要命名為logback-spring.xml
    2. logback使用application.yml中的屬性
      使用springProperty才可使用application.yml中的值 可以設置默認值
  -->
  <springProperty scope="context" name="logName" source="ruoyi.logPath" defaultValue="default"/>
  <property name="log.path" value="${logName}" />
  <!-- 日志存放路徑 -->
<!--  <property name="log.path" value="D:/zmSmartClassroom/logs"/>-->
  <!--  <property name="log.path" value="/home/zmSmartClassroom/logs"/>-->
  <!-- 日志輸出格式 -->
  <property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
  <!-- 控制臺輸出 -->
  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>${log.pattern}</pattern>
    </encoder>
  </appender>
  <!-- 系統日志輸出 -->
  <appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${log.path}/sys-info.log</file>
    <!-- 循環政策:基於時間創建日志文件 -->
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- 日志文件名格式 -->
      <fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- 日志最大的歷史 60天 -->
      <maxHistory>60</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>${log.pattern}</pattern>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <!-- 過濾的級別 -->
      <level>INFO</level>
      <!-- 匹配時的操作:接收(記錄) -->
      <onMatch>ACCEPT</onMatch>
      <!-- 不匹配時的操作:拒絕(不記錄) -->
      <onMismatch>DENY</onMismatch>
    </filter>
  </appender>
  <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${log.path}/sys-error.log</file>
    <!-- 循環政策:基於時間創建日志文件 -->
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- 日志文件名格式 -->
      <fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- 日志最大的歷史 60天 -->
      <maxHistory>60</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>${log.pattern}</pattern>
    </encoder>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <!-- 過濾的級別 -->
      <level>ERROR</level>
      <!-- 匹配時的操作:接收(記錄) -->
      <onMatch>ACCEPT</onMatch>
      <!-- 不匹配時的操作:拒絕(不記錄) -->
      <onMismatch>DENY</onMismatch>
    </filter>
  </appender>
  <!-- 用戶訪問日志輸出 -->
  <appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${log.path}/sys-user.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- 按天回滾 daily -->
      <fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern>
      <!-- 日志最大的歷史 60天 -->
      <maxHistory>60</maxHistory>
    </rollingPolicy>
    <encoder>
      <pattern>${log.pattern}</pattern>
    </encoder>
  </appender>
  <!-- 系統模塊日志級別控制 -->
  <logger name="com.ruoyi" level="info"/>
  <!-- Spring日志級別控制 -->
  <logger name="org.springframework" level="warn"/>
  <root level="info">
    <appender-ref ref="console"/>
  </root>
  <!--系統操作日志-->
  <root level="info">
    <appender-ref ref="file_info"/>
    <appender-ref ref="file_error"/>
  </root>
  <!--系統用戶操作日志-->
  <logger name="sys-user" level="info">
    <appender-ref ref="sys-user"/>
  </logger>
</configuration>

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: