SpringBoot輸出日誌到檔案

2021-10-04 21:15:44 字數 3617 閱讀 2653

2.2 logging.file.path

2.3 注意

3 使用xml擴充套件

4 logging裡的預設值

5 效果

指定日誌檔案的位置。

使用相對路徑,就會在專案根目錄下生成乙個lab.log檔案

logging

:file

:name

: lab.log

在專案根目錄下生成乙個logs資料夾,logs資料夾裡面生成乙個lab.log檔案。

logging

:file

:name

: logs/lab.log

使用絕對路徑,這樣寫會在d盤下建立乙個logs資料夾,logs資料夾裡面生成乙個lab.log檔案。

logging

:file

:name

: d:/logs/lab.log

使用相對路徑,會在專案根目錄下生成乙個logs資料夾,logs資料夾會有乙個spring.log檔案。

logging:

file:

path: logs

使用絕對路徑,會在d盤下生成乙個logs資料夾,logs資料夾會有乙個spring.log檔案。

logging

:file

:name

: d:/logs

logging.file.name和logging.file.path不要同時指定,只需要指定乙個。

相對路徑不指定碟符,直接在專案根目錄下生成

在resources目錄下建立logback-spring.xml配置檔案。

使用xml配置可以完成yml配置實現不了的功能,比如按天將日誌歸檔。

xml有多種命名形式,但官方推薦的命名是logback-spring.xml

<?xml version="1.0" encoding="utf-8"?>

scan

="true"

scanperiod

="60 seconds"

debug

="false"

>

>

logbackcontextname

>

name

="logging.pattern.console"

value

="%clr(%d}) %clr($) %clr($) %clr(---) %clr([%15.15t]) %clr(%-40.40logger) %clr(:) %m%n$"

/>

name

="logging.pattern.file"

value

="%d} $ $ --- [%t] %-40.40logger : %m%n$"

/>

conversionword

="clr"

converterclass

="org.springframework.boot.logging.logback.colorconverter"

/>

conversionword

="wex"

converterclass

="org.springframework.boot.logging.logback.whitespacethrowableproxyconverter"

/>

conversionword

="wex"

converterclass

="org.springframework.boot.logging.logback.extendedwhitespacethrowableproxyconverter"

/>

name

="console"

class

=>

class

="ch.qos.logback.classic.patternlayout"

>

>

$pattern

>

layout

>

>

name

="file"

class

=>

>

trueprudent

>

class

="ch.qos.logback.core.rolling.timebasedrollingpolicy"

>

>

logs/%d/%d.log

filenamepattern

>

>

365maxhistory

>

rollingpolicy

>

class

="ch.qos.logback.classic.filter.thresholdfilter"

>

>

errorlevel

>

filter

>

>

>

$pattern

>

encoder

>

>

name

="com.lpc"

level

="trace"

additivity

="true"

>

logger

>

level

="info"

>

ref=

"console"

/>

ref=

"file"

/>

root

>

configuration

>

其中,兩個的value屬性是yml的logging.pattern.console和logging.pattern.file的預設值貼上出來的。xml檔案放在了resources.config目錄下。日誌檔案輸出在專案根目錄下的logs資料夾下。

這個xml配置實現了:

控制台輸出日誌,同時將日誌儲存到檔案裡;

保持控制台日誌的彩色;

按月建日誌資料夾,按天建日誌檔案;

com.lpc包下的日誌從trace級別開始輸出,其他包(比如tomcat的日誌,springsecurity的日誌)從info級別開始輸出。跟原來yml裡面配置logging.level.com.lpc: trace的輸出結果保持一致。

本文由部落格**一文多發等運營工具平台 openwrite 發布

SpringBoot輸出日誌到檔案

指定日誌檔案的位置。使用相對路徑,就會在專案根目錄下生成乙個lab.log檔案 logging file name lab.log 在專案根目錄下生成乙個logs資料夾,logs資料夾裡面生成乙個lab.log檔案。logging file name logs lab.log 使用絕對路徑,這樣寫會...

springBoot輸出日誌到指定目錄

以輸出日誌檔案到d data log為例 logging.path d data log logging.config classpath logback spring.xml logging.level.root debug如果要輸出mybatis的sql,就加一行配置指定該目錄輸出為debug ...

Spring Boot 配置日誌輸出樣式

配置日誌輸出型別 logging.pattern.console boldmagenta d p highlight c l m n 配置全域性輸出級別 logging.level.root info 配置包的輸出級別 logging.level.org.springframework.web.se...