參考:https://stackoverflow.com/questions/19518843/logback-configuration-via-jvm-argumentjava
logback相關配置,其中${LOG_HOME}表示爲環境變量,在java啓動應用程序時,經過java -DLOG_HOME="路徑"來指定的,能夠動態去設置它的地址。windows
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <property name="logPath" value="${LOG_HOME-/home}/logs" /> <!-- 日誌打印的格式 --> <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> %d - %msg%n </pattern> </layout> </appender> <!-- info 狀態下的日誌 --> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> <encoder> <pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} %10logger [%thread] : %msg%n </pattern> </encoder> <!--滾動策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${logPath}/info.%d.log</fileNamePattern> <maxHistory>7</maxHistory> </rollingPolicy> </appender> <!-- 錯誤級別的日誌文件 --> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <encoder> <pattern> %d{yyyy-MM-dd HH:mm:ss.SSS} %10logger [%thread] : %msg%n </pattern> </encoder> <!--滾動策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${logPath}/error.%d.log</fileNamePattern> <maxHistory>7</maxHistory> </rollingPolicy> </appender> <root level="info"> <appender-ref ref="consoleLog" /> <appender-ref ref="fileInfoLog" /> <appender-ref ref="fileErrorLog" /> </root> </configuration>
上面代碼中${LOG_HOME-/home}/logs
表示,若是沒有傳入環境變量LOG_HOME,而後就使用後面的/home路徑,對於windows系統來講,/home表示當前jar包所在的盤符。app
java -Dlogback_home="c:\\logs" -jar eureka.jar
這樣就把日誌文件存儲到了C盤logs目錄了。jvm