日志:
记录事件发生过程,方便问题追溯,问题查找,注意中文乱码及日志级别
jar包引入:
<!-- log4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</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-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
application.properties
#log4j
#logging.config=classpath:log4j.properties
log4j.properties
log4j.rootLogger=INFO, springboot, stdout
# Define all the appenders
# stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%c{1}] %m%n
log4j.appender.springboot=org.apache.log4j.DailyRollingFileAppender
log4j.appender.springboot.File=./logs/server.log
log4j.appender.springboot.Append=true
log4j.appender.springboot.DatePattern='.'yyyy-MM-dd
log4j.appender.springboot.layout=org.apache.log4j.PatternLayout
log4j.appender.springboot.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p [%c] %m%n
使用:
import org.apache.log4j.Logger;
private final Logger logger = Logger.getLogger(当前类名.class);
logger.error("操作失败,msg," + e.getMessage());