1. 程式人生 > >SpringBoot2.0-新增log4j日誌

SpringBoot2.0-新增log4j日誌

目錄

由於SpringBoot的預設日誌是Logback,但是許多學過SSM框架的朋友都習慣用log4j,因為這是MyBatis的預設日誌,所以這一篇講解在gradle下如何為SpringBoot配置log4j日誌

一、修改build.gradle

首先排除預設日誌

configurations {
	//remove default logger
	all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}

然後新增log4j依賴

// log4j2
	compile ("org.springframework.boot:spring-boot-starter-web") {
		exclude module: 'org.springframework.boot:spring-boot-starter-logging'
	}
	compile ('org.springframework.boot:spring-boot-starter-log4j2')

二、新增log4j2.properties

# Configure logging for testing: optionally with log file
log4j.rootLogger=debug, stdout,D,E
# log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender  
log4j.appender.D.File = C://logs/e3-manager-service-debug.log
log4j.appender.D.Append = true  
log4j.appender.D.Threshold = DEBUG   
log4j.appender.D.layout = org.apache.log4j.PatternLayout  
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n  

log4j.appender.E = org.apache.log4j.DailyRollingFileAppender  
log4j.appender.E.File =C://logs/e3-manager-service-error.log
log4j.appender.E.Append = true  
log4j.appender.E.Threshold = ERROR   
log4j.appender.E.layout = org.apache.log4j.PatternLayout  
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

log4j配置細節可以參考其他部落格

配置完成後,執行SpringBoot出現以下熟悉的資訊,表明專案已經換成了log4j日誌了