案例:web
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class HelloWorld { public static void main(String[] args) { Logger logger = LoggerFactory.getLogger(HelloWorld.class); logger.info("Hello World"); } }
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency>
排除spring 使用的commons-loggingspring
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency>
@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { private Logger logger = LoggerFactory.getLogger(ApplicationTests.class); @Test public void logTest() { logger.trace("--This is trace log"); logger.debug("-- --This is debug log"); logger.info("-- -- --This is info log"); logger.warn("-- -- -- --This is warn log"); logger.error("-- -- -- -- --This is error log"); } }
運行結果:
: -- -- --This is info log
: -- -- -- --This is warn log
: -- -- -- -- --This is error logapache
#設置日誌有顏色的輸出 spring: output: ansi: enabled: always #日誌配置 logging: #日誌級別設置 level: #指定包生效 com.lvmama: debug #日誌文件路徑(當前工程根目錄下spring.log) path: /log/springboot
logging.file logging.file.max-size logging.file.max-history logging.path logging.pattern.console logging.pattern.dateformat logging.pattern.file logging.pattern.level PID
When possible, we recommend that you use the -spring variants for your logging configuration (for example, logback-spring.xml rather than logback.xml). If you use standard configuration locations, Spring cannot completely control log initialization.
在resource目錄下添加指定的log.xml,spring boot默認只用自定義的日誌配置,如logback在resource目錄下添加logback.xml或logback-spring.xml便可。不過官網推薦使用logback-spring的方式,由於能夠使用<springProfile>高級特性,動態使用日誌配置。springboot
Because the standard logback.xml configuration file is loaded too early, you cannot use extensions in it. You need to either use logback-spring.xmlor define a logging.config property.
測試:app
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <springProfile name="dev"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} ---dev--- [%thread] - %-5level %logger{50} - %msg%n</pattern> </springProfile> <springProfile name="!dev"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] - %-5level %logger{50} - %msg%n</pattern> </springProfile> </layout> </appender>
mvn spring-boot:run框架
控制檯輸出:spring-boot
2018-05-20 17:55:44.708 - [main] - INFO com.lvmama.SpringBootInstaller - Starting SpringBootInstaller on 28H5EO7ZV2V7ELK with PID 4288 (D:\workspace\spring-boot\target\classes started by Administrator in D:\workspace\spring-boot) 2018-05-20 17:55:44.732 - [main] - DEBUG com.lvmama.SpringBootInstaller - Running with Spring Boot v1.5.14.BUILD-SNAPSHOT, Spring v4.3.17.RELEASE 2018-05-20 17:55:44.733 - [main] - INFO com.lvmama.SpringBootInstaller - No active profile set, falling back to default profiles: default 2018-05-20 17:55:45.224 - [background-preinit] - INFO org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 5.3.6.Final 2018-05-20 17:55:48.158 - [main] - INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"] 2018-05-20 17:55:48.236 - [main] - INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]
mvn spring-boot:run -Drun.profiles=dev //指定dev環境 測試
2018-05-20 17:59:25.500 ---dev--- [main] - INFO com.lvmama.SpringBootInstaller - Starting SpringBootInstaller on 28H5EO7ZV2V7ELK with PID 2880 (D:\workspace\spring-boot\target\classes started by Administrator in D:\workspace\spring-boot) 2018-05-20 17:59:25.505 ---dev--- [main] - DEBUG com.lvmama.SpringBootInstaller - Running with Spring Boot v1.5.14.BUILD-SNAPSHOT, Spring v4.3.17.RELEASE 2018-05-20 17:59:25.506 ---dev--- [main] - INFO com.lvmama.SpringBootInstaller - The following profiles are active: dev 2018-05-20 17:59:26.007 ---dev--- [background-preinit] - INFO org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 5.3.6.Final 2018-05-20 17:59:27.749 ---dev--- [main] - INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"] 2018-05-20 17:59:27.769 ---dev--- [main] - INFO org.apache.catalina.core.StandardService - Starting service [Tomcat]