您好,登錄后才能下訂單哦!
這篇文章主要介紹“SpringBoot項目中怎么保存控制臺日志”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“SpringBoot項目中怎么保存控制臺日志”文章能幫助大家解決問題。
情景:SpringBoot項目中需要把控制臺日志保存下來。
實現方法:
配置如下:
logging: config: classpath:logback-spring-prod.xml
這個文件是記錄控制臺打印日志保存的路徑配置如下:
<?xml version="1.0" encoding="UTF-8"?> <!-- 分級別異步文件日志輸出配置 --> <configuration> <!-- 日志級別 --> <property name="logLevel" value="INFO"></property> <!-- 日志地址 --> <property name="logPath" value="/usr/server/shop/logs/"></property> <property name="logPathDebug" value="/usr/server/shop/logs/debug/"></property> <property name="logPathInfo" value="/usr/server/shop/logs/info/"></property> <property name="logPathWarn" value="/usr/server/shop/logs/warn/"></property> <property name="logPathError" value="/usr/server/shop/logs/error/"></property> <!-- 最大保存時間 --> <property name="maxHistory" value="30"/> <!-- 異步緩沖隊列的深度,該值會影響性能.默認值為256 --> <property name="queueSize" value="512"></property> <!--日志文件名前綴--> <property name="name" value="shop_prod"></property> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%black(%contextName -) %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{60}) - %gray(%msg%n)</pattern> </encoder> </appender> <appender name="FILE_DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <file>${logPathDebug}/${name}_debug.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${logPathDebug}/${name}_debug.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>50MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </encoder> </appender> <appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <file>${logPathInfo}/${name}_info.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${logPathInfo}/${name}_info.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>50MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </encoder> </appender> <appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <file>${logPathWarn}/${name}_warn.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${logPathWarn}/${name}_warn.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>50MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </encoder> </appender> <appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <file>${logPathError}/${name}_error.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${logPathError}/${name}_error.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxHistory>${maxHistory}</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>50MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern> </encoder> </appender> <appender name="ASYNC_LOG_DEBUG" class="ch.qos.logback.classic.AsyncAppender"> <!-- 不丟失日志.默認的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 --> <discardingThreshold>0</discardingThreshold> <!-- 更改默認的隊列的深度,該值會影響性能.默認值為256 --> <queueSize>${queueSize}</queueSize> <appender-ref ref="FILE_DEBUG"/> </appender> <appender name="ASYNC_LOG_INFO" class="ch.qos.logback.classic.AsyncAppender"> <!-- 不丟失日志.默認的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 --> <discardingThreshold>0</discardingThreshold> <!-- 更改默認的隊列的深度,該值會影響性能.默認值為256 --> <queueSize>${queueSize}</queueSize> <appender-ref ref="FILE_INFO"/> </appender> <appender name="ASYNC_LOG_WARN" class="ch.qos.logback.classic.AsyncAppender"> <!-- 不丟失日志.默認的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 --> <discardingThreshold>0</discardingThreshold> <!-- 更改默認的隊列的深度,該值會影響性能.默認值為256 --> <queueSize>${queueSize}</queueSize> <appender-ref ref="FILE_WARN"/> </appender> <appender name="ASYNC_LOG_ERROR" class="ch.qos.logback.classic.AsyncAppender"> <!-- 不丟失日志.默認的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 --> <discardingThreshold>0</discardingThreshold> <!-- 更改默認的隊列的深度,該值會影響性能.默認值為256 --> <queueSize>${queueSize}</queueSize> <appender-ref ref="FILE_ERROR"/> </appender> <root level="${logLevel}"> <!-- appender referenced after it is defined --> <appender-ref ref="STDOUT"/> <appender-ref ref="ASYNC_LOG_DEBUG"/> <appender-ref ref="ASYNC_LOG_INFO"/> <appender-ref ref="ASYNC_LOG_WARN"/> <appender-ref ref="ASYNC_LOG_ERROR"/> </root> </configuration>
注意:
<property name="logPath" value="/usr/server/shop/logs/"></property>
中日志存放的路徑在/usr/server/shop/logs/下存放。
這個問題相對很簡單,只需要完成兩個步驟
在pom中需要引用lombok依賴,版本跟隨你的springboot夫版本即可
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <contextName>StudentContext</contextName> <property name="log.path" value="./logs" /> <!--輸出到控制臺 --> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- 全部日志 --> <appender name="all" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/student/student.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>60</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n</pattern> <charset>UTF-8</charset> </encoder> </appender> <!-- 輸出到warn --> <appender name="warn" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/student/student.warn.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>60</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n</pattern> <charset>UTF-8</charset> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印WARN日志 --> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <!-- 輸出到error --> <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log.path}/student/student.error.%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>60</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{76}.%M\(%line\) - %msg%n</pattern> <charset>UTF-8</charset> </encoder> <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印ERROR日志 --> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> </appender> <root level="info"> <appender-ref ref="console" /> <appender-ref ref="all" /> <appender-ref ref="warn" /> <appender-ref ref="error" /> </root> </configuration>
log4j輸出日志的文件,將其放在resources下即可,注意fileNamePattern這個標簽中的東西換成標識你微服務作用的,比如我寫的學生管理系統,所以用的是student
logging: level: com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver: WARN config: classpath:logback-common.xml
關于“SpringBoot項目中怎么保存控制臺日志”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。