這兩天因為疫情,需要在家辦公,但是家里只有一台MacBook Pro。在使用MBP啟動SpringBoot項目的時候,發現root用戶沒有權限創建/data/這個文件位置。
百度了一下,第一種是在macOS里進入的recovery模式給root用戶解除權限限制,這邊就不說了,網上都有。第二種就是直接修改項目里日志的保存路徑。
首先修改logback.log的存放路徑。非常簡單,在項目的resource文件夾里新建一個logback-spring.xml(你可以把項目jar包里面的這個xml拷貝過來)
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <contextName>logback</contextName> <!-- 動態 獲取變量 name:變量名稱,class:動態獲取的類名 (類需要實現PropertyDefiner接口--> <define name="log.path" class="com.zto.titans.logging.configuration.DynamicProperty"> </define> <conversionRule conversionWord="traceId" converterClass="com.zto.titans.logging.cat.CatLogBackConvert" /> <!--輸出到控制台--> <!--<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] ${LOG_LEVEL_PATTERN:-%p} ${PID:- } [%traceId] [%t] [%logger] : %m%n</pattern> <charset>UTF-8</charset> </encoder> </appender>--> <!--自定義帶有開關的ConsoleAppender,輸出到控制台--> <appender name="consoleWithSwitch" class="com.zto.titans.logging.appender.ConsoleWithSwitchAppender"> <encoder> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] ${LOG_LEVEL_PATTERN:-%p} ${PID:- } [%traceId] [%t] [%logger] : %m%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <!--輸出到文件--> <appender name="file" class="com.zto.titans.logging.appender.AsyncFlushRollingFileAppender"> <file>${log.path}/logback.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/logback.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern> <maxHistory>7</maxHistory> <totalSizeCap>10GB</totalSizeCap> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>1gb</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <ImmediateFlush>false</ImmediateFlush> <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] ${LOG_LEVEL_PATTERN:-%p} ${PID:- } [%traceId] [%t] [%logger] : %m%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <appender name="catAppender" class="com.zto.titans.logging.cat.log.CatLogbackAppender"></appender> <springProfile name="default,dev"> <root level="info"> <appender-ref ref="consoleWithSwitch"/> <appender-ref ref="file"/> </root> </springProfile> <springProfile name="pro,prd,stg,test,uat,fit,fat,sit"> <root level="error"> <appender-ref ref="consoleWithSwitch"/> <appender-ref ref="catAppender"/> <appender-ref ref="file"/> </root> </springProfile> </configuration>
文件默認長這樣,然后你只需要將log.path中定義的類com.zto.titans.logging.configuration.DynamicProperty,自己新建一個類替代他,然后把getPropertyValue的返回值中的路徑改成自己想要的路徑就可以了。
關於CAT日志的輸出位置。
網上搜了好多,都是讓我保存在默認位置下。甚至打開源代碼看,報錯的位置路徑居然都是寫死的
。。。
但是最終在不懈努力下,我還是解決了這個問題。
答案就是在VM Option中加一個參數
-DCAT_HOME=/pathname/log
完美解決
開啟瘋狂加班模式