1. 程式人生 > >spring boot (四) 日誌的使用

spring boot (四) 日誌的使用

Spring Boot支援 Java Utile Logging、Log4J、Log4J2和Logback作為日誌框架,無論使用哪種框架,Spring Boot都為當前使用日誌框架及檔案輸出做好了配置。
預設情況下,Spring Boot使用LogBack作為預設日誌框架,輸出格式的檔案是logback.cml

application.properties配置檔案中加入配置

###日誌檔案####
logging.file=/export/log

###配置日誌檔案,按照格式loggin.level.包名=級別###
logging.level.org.springframework.web
=DEBUG ###或者直接配置根級別### logging.level.root=DEBUG

spring boot 中使用log4j
在classpa中加入log4j.properties配置檔案

log4j.rootLogger=INFO,ServerDailyRollingFile,stdout
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern=’.’yyyy-MM-dd_HH
log4j.appender.ServerDailyRollingFile.File=log/log4j.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n
log4j.appender.ServerDailyRollingFile.Append=true

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%F:%L] %-5p :: %m%n

加入log4j的依賴

log4j
log4j

spring boot 中使用slf4j

首先加入log4j+slf4j的jar依賴

<dependency>
  <groupId
>
log4j</groupId> <artifactId>log4j</artifactId> </dependency> <!--日誌輸出使用slf4j,與logback為日誌實現類,只能同時出現一個--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.21</version> </dependency> <!--排除多餘依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> <exclusion> <artifactId>log4j-over-slf4j</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency>
# 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.