Springboot日誌默認使用的是logback,本文將介紹將springboot項目日誌修改成log4j。java
首先要將默認的日誌依賴排除,而後引用log4j,pom文件代碼以下:web
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dalaoyang</groupId> <artifactId>springboot_log4j</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot_log4j</name> <description>springboot_log4j</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupI> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> <version>1.3.2.RELEASE</version> <type>pom</type> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
引用log4j以後,在resources加入log4j配置便可,下面是本人以前項目的log4j.properties內容,代碼以下:spring
log4j.rootLogger=debug,CONSOLE,FILE ##輸出到控制檯 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=DEBUG log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p %x - %m%n log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.Encoding=gbk log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout ##輸出到日誌文件 每10M分紅一個日誌文件 log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender log4j.appender.FILE.File=/Users/dalaoyang/Downloads/log log4j.appender.FILE.DatePattern = '_'yyyy-MM-dd-HH-mm'.log' log4j.appender.FILE.MaxFileSize=10MB log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=%d%n%m%n
簡單講解一下:
CONSOLE裏面是輸出到控制檯的日誌
FILE是輸入到日誌文件的日誌,日誌是按照大小分的,最大爲10M,
每小時一個日誌文件。apache