1. 程式人生 > >spring-boot結合logback日誌框架

spring-boot結合logback日誌框架

Spring-Boot預設集成了backlog日誌框架,無需在載入額外的jar包。

只需要在application.properties檔案中新增配置即可(非預設bocklog.xml方式):

#backlog setting
logging.config=logback.xml  

logback.xml內容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <!-- 控制檯設定 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- encoder 預設配置為PatternLayoutEncoder --> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender
>
<!-- * 萬用字元 設定log列印級別 對所有類有效TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF--> <root level="DEBUG"> <appender-ref ref="STDOUT" /> </root> </configuration>

當前僅當只是配置控制檯輸出方式,需要配置File,已經File策略的請觀看博主的該文章介紹。

測試類:

@Controller
@RequestMapping(“test”)
public class TestController {
public static Logger logger = LoggerFactory.getLogger(TestController.class);
@RequestMapping(“”)
public void test() {
logger.debug(“test”);
System.out.println(“呼叫了Test”);
}
}

關於Spring-Boot給出的配置方式如下:

# LOGGING
logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback

logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions.

logging.file= # Log file name. For instance `myapp.log`

logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG`

logging.path= # Location of the log file. For instance `/var/log`

logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup.

logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup.

logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup.

logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.

有興趣的同學可以去研究下!

PS:最近做一個專案,發現打包成war部署到伺服器上面的時候整合的logback日誌打印不出來。原因logging.config指定的路徑檔案一直識別出來講backlog.xml改名為logback-spring.xml放在resources下面即可。